C++之析构函数

#include <iostream>
#include <string>


using namespace std;


struct Student
{
public:
Student(int,string,string);
~Student();
void display();
private:
int num;
string name;
string sex;
};


Student::Student(int n,string na,string s)
{
num=n;
name=na;
sex=s;
cout<<"Constructor called."<<num<<endl;
}


Student::~Student()
{
cout<<"Destructor called."<<num<<endl;
}


void Student::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}


int main()
{
Student stud1(101,"Wang","f"),stud2(102,"Li","m");
stud1.display();
stud2.display();

return 0;
}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80293299