#include <iostream>
#include <string.h>
using namespace std;
class Student
{
public:
Student(int n,string nam,char s)
{
num = n;
name = nam;
sex = s;
cout << "Constructor called" << num << endl;
}
~Student()
{
cout << "Destructor called" << num << endl;
}
void display()
{
cout << "num=" << num << endl;
cout << "name=" << name << endl;
cout << "sex=" << sex << endl;
}
private:
int num;
string name;
char sex;
};
int main()
{
Student stud1(10010,"Wang li",'f');
stud1.display();
Student stud2(10011,"Zhang fang",'m');
stud2.display();
cout << "Hello world!" << endl;
return 0;
}
在Code::Blocks里看结果,结合谭浩强C++程序设计第2版的内容理解。