构造函数与析构函数的程序
1 #include <string.h> 2 #include <iostream> 3 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 using namespace std; 6 7 class Student 8 { 9 public: 10 Student(int n,string name,char s) 11 { 12 num=n; 13 na=name; 14 sex=s; 15 cout<<"Constructor called."<<endl; 16 } 17 ~Student() 18 { 19 cout<<"Destructor called."<<endl; 20 } 21 void display() 22 { 23 cout<<"num:"<<num<<endl; 24 cout<<"name:"<<na<<endl; 25 cout<<"sex:"<<sex<<endl; 26 } 27 private: 28 int num; 29 string na; 30 char sex; 31 }; 32 33 int main(int argc, char** argv) { 34 Student stud1(10010,"Wang_li",'f'); 35 stud1.display(); 36 Student stud2(10011,"Zhang_fun",'m'); 37 stud2.display(); 38 return 0; 39 }