嘿..我真的不明白。我读了一篇关于 C++ 类的教程,但我没有得到一些信息:
在我见过的每个示例和教程中,函数从来都不是在类中编写的!例如,为什么要写这样一个类:
#include <iostream>
using namespace std;
class test
{
private:
int x, y;
public:
test (int, int);
int tester () {return x + y; }
};
test::test (int a, int b)
{
x = a;
y = b;
}
int main()
{
test atest (3, 2);
test atest2 (2, 6);
cout << "test1: " << atest.tester() << endl;
cout << "test2: " << atest2.tester() << endl;
return 0;
}
或者像这样:
#include <iostream>
using namespace std;
class test
{
private:
int x, y;
public:
void set_values (int,int);
int testfunc () {return x + y; }
};
void test::set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
test tester;
tester.set_values (3, 2);
cout << "test1: " << tester.testfunc() << endl;
return 0;
}
而不是简单地这样:
#include <iostream>
using namespace std;
class test
{
public:
int tester (int x, int y) { return x + y; }
};
int main()
{
test atest;
cout << atest.tester(3, 2) << endl;
return 0;
}
老实说,我就是不明白!
为什么我需要私人会员?
我应该何时以及如何使用析构函数?
我一般应该如何编写我的课程?
我在这里很困惑,我真的需要有人为我解决问题......谢谢