1、 char name10; char sex; class Student1: public Student public: void get_value_1() get_value(); cinageaddr; void display_1() coutage:age /引用派生类的私有成员,正确。address:addr /引用派生类的私有成员,正确。 private: int age; char addr30; ;int main() Student1 stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); retu
2、rn 0;2: private Student display();3:class Student /声明基类 /基类公用成员 void get_value(); void display( ); protected : /基类保护成员void Student:get_value()display( ) protected Student /声明一个保护派生类 void get_value_1(); void display1( );void Student1:get_value_1()display1( ) /引用基类的保护成员 /引用派生类的私有成员int main( ) /stud1是派
3、生类student1类的对象 /调用派生类对象stud1的公用成员函数 stud1.display1( );4: 解法一 public Student /声明一个公用派生类display1( ) /引用基类的保护成员,合法 /引用派生类的私有成员,合法 /调用派生类对象stud1的公用成员函数get_value_1 /调用派生类对象stud1的公用成员函数display1解法二 protected Student /声明一个公用派生类 stud1.get_value(); stud1.display( ); stud1.display1(); /合法。display1是派生类中的公用成员函数5
4、:class A /A为基类 void f1( ); int i; protected: void f2(); int j; int k;class B: public A /B为A的公用派生类 void f3( ); int m; int n;class C: public B /C为B的公用派生类 void f4(); int p;A a1; /a1是基类A的对象 B b1; /b1是派生类B的对象 C c1; /c1是派生类C的对象6:class A public A protected Bclass D: private C void f5(); int q; int r; D d1;
5、7: public: A()a=0;b=0; A(int i)a=i; A(int i,int j)a=i;b=j; void display()couta=a b=b; int a; int b;class B : B()c=0; B(int i):A(i)c=0; B(int i,int j):A(i,j)c=0; B(int i,int j,int k):A(i,j)c=k; void display1() c=c int c; B b1; B b2(1); B b3(1,3); B b4(1,3,5); b1.display1(); b2.display1(); b3.display1
6、(); b4.display1();8: A()coutconstructing A A()coutdestructing A B()coutconstructing B B()coutdestructing B class C : public B C()coutconstructing C C()coutdestructing C C c1;9:#includeclass Teacher Teacher(string nam,int a,char s,string tit,string ad,string t); void display(); string name; string ti
7、tle; string addr; string tel;Teacher:Teacher(string nam,int a,char s,string tit,string ad,string t): name(nam),age(a),sex(s),title(tit),addr(ad),tel(t) void Teacher:display()agetitle:titletel:telclass Cadre Cadre(string nam,int a,char s,string p,string ad,string t); string post;Cadre:Cadre(string na
8、m,int a,char s,string p,string ad,string t): name(nam),age(a),sex(s),post(p),addr(ad),tel(t)void Cadre:post:postclass Teacher_Cadre:public Teacher,public Cadre Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w); void show( ); float wage;Teacher_Cadre:Teacher_Cadre(
9、string nam,int a,char s,string t,string p,string ad,string tel,float w): Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) void Teacher_Cadre:show( ) Teacher:display();wages:wageclass Teacher /教师类 Teacher(int,char ,char); /声明构造函数 /声明输出函数 char name20;Teacher(int n,char nam,char s) /定义构造函数 num
10、=n; strcpy(name,nam); sex=s;display() /定义输出函数class BirthDate /生日类 BirthDate(int,int,int); void change(int,int,int); /声明修改函数 int year; int month; int day;BirthDate:BirthDate(int y,int m,int d) /定义构造函数 year=y; month=m; day=d;void BirthDate:birthday:month/dayyearchange(int y,int m,int d) /定义修改函数class P
11、rofessor:public Teacher /教授类 Professor(int,char ,char,int,int,int,float); float area; BirthDate birthday; /定义BirthDate类的对象作为数据成员Professor:Professor(int n,char nam20,char s,int y,int m,int d,float a): Teacher(n,nam,s),birthday(y,m,d),area(a) /定义构造函数void Professor:Teacher: birthday.display();area:area birthday.change(y,m,d);Professor prof1(3012,Zhang,1949,10,1,125.4); /定义Professor对象prof1endloriginal data: prof1.display(); /调用prof1对象的display函数new data: prof1.change(1950,6,1); /调用prof1对象的change函数
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1