例15.2 包含构造函数和析构函数的C++程序。
#include<string.h>
#include<iostream.h>
class stud //声明一个类
{private,// 私有部分
int num;
char name[10];
char sex ;
public,//公用部分
stud(int n,char nam[],char s ) //构造函数
{num=n;
strcpy(name,nam);
sex=s; }
~stud( ) //析构函数
{ }
void display( ) //成员函数,输出对象的数据
{cout<<"num,"<<num<<endl;
cout<<"name,"<<name<<endl;
cout<<"sex,"<<sex<<endl; }
};
void main( )
{
stud stud1(10010,"Wang-li",′f′),stud2(10011,"Zhang-fun",′m′); //建立两个对象
stud1.display( ); //输出学生1的数据
stud2.display( ); //输出学生2的数据
}