#include<iostream>
#include<string>
using namespace std;
class Student
{public:
Student(int n,string nam):num(n),name(nam){}//基类构造函数;
void display()
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
protected:
int num;
string name;
};
class Student1:public Student//public inheritance;
{public:
Student1(int n,string nam,int a):Student(n,name),age(a){}//派生类构造函数;
void display1()
{
display();
cout<<"age:"<<age<<endl;
}
private:
int age;
};
class Student2:public Student1//public inheritance;
{public:
Student2(int n,string nam,int a,char s):Student1(n,nam,a),sex(s){}//间接派生类构造函数;
void display2()
{
display1();
cout<<"sex:"<<sex<<endl;
}
private:
char sex;
};
int main()
{
Student2 stud(120,"yx",22,'m');// 为什么结果yx未出现?
stud.display2();
system("pause");
return 0;
}
[此贴子已经被作者于2007-3-12 10:35:43编辑过]