赞
踩
//公有继承, //基类 private->派生类 不可访问; 基类 public->派生类 public; 基类 protected->派生类 protected; #include<iostream> using namespace std; class Student{ public: void get_value(){ cin>>num>>name>>sex; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } private: int num; string name; char sex; }; class Student1:public Student{//公有继承 public: void get_value1(){ get_value(); //可以调用基类的 公有成员 cin>>age>>addr; } void display1(){ display(); /*cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;*/ //不可访问基类的私有成员 cout<<"age: "<<age<<endl; cout<<"addr: "<<addr<<endl; } private: int age; string addr; }; int main(){ Student1 stud; //stud.get_value(); //基类公有,在公有继承派生类中仍是公有,可以在类外调用 stud.get_value1(); stud.display1(); return 0; }
//私有继承, //基类 private->派生类 不可访问; 基类 public->派生类 private; 基类 protected->派生类 private; #include<iostream> using namespace std; class Student{ public: void get_value(){ cin>>num>>name>>sex; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } private: int num; string name; char sex; }; class Student1:private Student{//私有继承 public: void get_value1(){ get_value(); //可以调用基类的 公有成员 cin>>age>>addr; } void display1(){ display(); /*cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;*/ //不可访问基类的私有成员 cout<<"age: "<<age<<endl; cout<<"addr: "<<addr<<endl; } private: int age; string addr; }; int main(){ Student1 stud; stud.get_value1(); //stud.get_value(); //基类公有,在私有继承的派生类私有,不可在类外调用 stud.display1(); return 0; }
//保护继承, //基类 private->派生类 不可访问; 基类 public->派生类 protected; 基类 protected->派生类 protected; #include<iostream> using namespace std; class Student{ public: void get_value(){ cin>>num>>name>>sex; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } protected: //基类保护成员 int num; string name; char sex; }; class Student1:protected Student{//保护继承 public: void get_value1(){ get_value(); //可以访问基类的 公有成员,只能在类中使用 cin>>age>>addr; } void display1(){ //display(); cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;//可访问基类的保护成员,只能在类中使用 cout<<"age: "<<age<<endl; cout<<"addr: "<<addr<<endl; } private: int age; string addr; }; int main(){ Student1 stud; stud.get_value1(); //stud.get_value(); //基类公有在保护继承的派生类中为保护类型,不可在类外调用 stud.display1(); return 0; }
/*保护继承&公有继承,显然在这样的程序中修改后可以运行,但是两者不是任何情况可以替代的。 可以看到,两者在继承的访问属性变化,只有一个区别, ⭐⭐就是基类公有类型 保护继承:基类公有在派生类访问属性变为保护类型,只能在类里使用;而公有继承:基类公有在派生类中仍是公有,可以在类外使用。 所以如果基类中的公有在公有继承派生类的外部调用时是不可以和保护继承替换的。否则程序是可以运行的。*/ #include<iostream> using namespace std; class Student{ public: void get_value(){ cin>>num>>name>>sex; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } protected: //基类保护成员 int num; string name; char sex; }; class Student1:public Student{//改成公有继承 public: void get_value1(){ get_value(); //可以访问基类的 公有成员,类外也可以访问 cin>>age>>addr; } void display1(){ //display(); cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;//可访问基类的保护成员,只能在类中使用 cout<<"age: "<<age<<endl; cout<<"addr: "<<addr<<endl; } private: int age; string addr; }; int main(){ Student1 stud; stud.get_value1(); //stud.get_value(); //基类公有在保护继承的派生类中为保护类型,可以在类外调用 stud.display1(); return 0; }
class A{ public: void f1(); int i; protected: void f2(); int j; private: int k; }; class B:public A{ public: void f3(); protected: int m; private: int n; }; class c:public B{ public: void f4(); private: int p; }; int main(){ A a1; B b1; C c1; } /* (1)在main函数中,可以使用b1.i访问基类成员 b1.j,b1.k不可 //因为公有继承方式,基类公有在派生中访问属性仍是公有,可以在类外访问,保护在派生类中仍是保护,不可在类外访问,而私有无法访问。 (2)B的成员函数f3可以调用A中的f1() f2() //A中f1 f2函数在派生类中分别是公有和保护,都可以在派生类内访问 (3)B中成员函数可引用A中的i j 不可引用k //A中i j在派生类中为公有和保护,可访问 而k在A中私有,不可在派生类中访问 (4)main函数可以访问c1.i 其他均不可 (5)main函数可以访问c1.f1() c1.f3() c1.f4() 不可访问c1.f2() (6)f4()中调用f1() f2() f3()均可 */
class A{ public: void f1(); protected: void f2(); private: int i; }; class B:public A{ public: void f3(); int k; private: int m; }; class c:protected B{ public: void f4(); protected: int m;//同名覆盖⭐P335 private: int n; }; class D:private C{ public: void f5(); protected: int p; private: int q; }; int main(){ A a1; B b1; C c1; D d1; } /* 在B中:f1()public;f2()protected; i不可访问 在C中:f1()protected; f2()protected; f3()protected; k protected; i,m 不可访问 在D中:f1()private; f2()private; f3()private; k private; f4() private; m(c中) private; i,n 不可访问 */
#include<iostream> using namespace std; class A{ public: A(){//构造函数重载 a=0; b=0; } A(int i){ a=i; b=0; } A(int i,int j){ a=i; b=j; } void display(){ cout<<"a="<<a<<" b="<<b; } private: int a; int b; }; class B:public A{ public: 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(){ display(); cout<<" c="<<c<<endl; } private: int c; }; int main(){ B b1; B b2(1); B b3(1,3); B b4(1,3,5); b1.display1(); b2.display1(); b3.display1(); b4.display1(); return 0; }
#include<iostream> using namespace std; class A{ public: A(){ cout<<"constructing A"<<endl; } ~A(){ cout<<"destructing A"<<endl; } }; class B:public A{ public: B(){ cout<<"constructing B"<<endl; } ~B(){ cout<<"destructing B"<<endl; } }; class C:public B{ public: C(){ cout<<"constructing C"<<endl; } ~C(){ cout<<"destructing C"<<endl; } }; int main(){ C c1; return 0; } //P356 调用派生类构造函数时会自动首先调用基类的构造函数, //而对派生类新增成员清理时,正好相反,先调用子对象的析构函数再调用基类的析构函数
//程序1 参考课本P358 P360 #include<iostream> #include<string> using namespace std; class Teacher{ public: /*Teacher(string n,int a,char s,string ad,string te,string ti){ name=n; age=a; sex=s; addr=ad; tele=te; title=ti; } void display(){ cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"addr: "<<addr<<endl; cout<<"tele: "<<tele<<endl; cout<<"title: "<<title<<endl; }*/ Teacher(string n,int a,char s,string ad,string te,string ti); void display(); protected: string name; int age; char sex; string addr; string tele; string title; }; Teacher::Teacher(string n,int a,char s,string ad,string te,string ti){ name=n; age=a; sex=s; addr=ad; tele=te; title=ti; } void Teacher::display(){ cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"addr: "<<addr<<endl; cout<<"tele: "<<tele<<endl; cout<<"title: "<<title<<endl; } class Cadre{ public: /* Cadre(string n,int a,char s,string ad,string te,string p){ name=n; age=a; sex=s; addr=ad; tele=te; post=p; } void display(){ cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"addr: "<<addr<<endl; cout<<"tele: "<<tele<<endl; cout<<"post: "<<post<<endl; }*/ Cadre(string n,int a,char s,string ad,string te,string p); void display(); protected: string name; int age; char sex; string addr; string tele; string post; }; Cadre::Cadre(string n,int a,char s,string ad,string te,string p){ name=n; age=a; sex=s; addr=ad; tele=te; post=p; } void Cadre::display(){ cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"addr: "<<addr<<endl; cout<<"tele: "<<tele<<endl; cout<<"post: "<<post<<endl; } class Teacher_Cadre:public Teacher,public Cadre{//多重继承 public: /*Teacher_Cadre(string n,int a,char s,string ad,string te,string ti,string p,double w): Teacher(n,a,s,ad,te,ti),Cadre(n,a,s,ad,te,p){ wage=w; } void show(){ Teacher::display(); cout<<"post: "<<post<<endl; cout<<"wage: "<<wage<<endl; }*/ Teacher_Cadre(string n,int a,char s,string ad,string te,string ti,string p,double w); void show(); private: double wage; }; Teacher_Cadre::Teacher_Cadre(string n,int a,char s,string ad,string te,string ti,string p,double w): Teacher(n,a,s,ad,te,ti),Cadre(n,a,s,ad,te,p){ wage=w; } void Teacher_Cadre::show(){ Teacher::display();//避免二义性,指明使用Teacher的display函数 cout<<"post: "<<post<<endl; cout<<"wage: "<<wage<<endl; } int main(){ Teacher_Cadre t("lily",20,'f',"dhjsj","12343323","professor","president",1233); t.show(); return 0; }
//程序2, //因为Teacher和Cadre有许多共同的成员尝试使用虚基类完成程序,参考P366 #include<iostream> #include<string> using namespace std; class Person{ public: Person(string n,int a,char s,string ad,string te){ name=n; age=a; sex=s; addr=ad; tele=te; } void display(){ cout<<"name: "<<name<<endl; cout<<"age: "<<age<<endl; cout<<"sex: "<<sex<<endl; cout<<"addr: "<<addr<<endl; cout<<"tele: "<<tele<<endl; } protected: string name; int age; char sex; string addr; string tele; }; class Teacher:virtual public Person{//虚基类 public: Teacher(string n,int a,char s,string ad,string te,string ti): Person(n,a,s,ad,te),title(ti){} void display(){ Person::display(); cout<<"title: "<<title<<endl; } protected: string title; }; class Cadre:virtual public Person{//虚基类 public: Cadre(string n,int a,char s,string ad,string te,string p): Person(n,a,s,ad,te),post(p){} void display(){ Person::display(); cout<<"post: "<<post<<endl; } protected: string post; }; class Teacher_Cadre:public Teacher,public Cadre{//多重继承 public: Teacher_Cadre(string n,int a,char s,string ad,string te,string ti,string p,double w); void show(); private: double wage; }; Teacher_Cadre::Teacher_Cadre(string n,int a,char s,string ad,string te,string ti,string p,double w): Person(n,a,s,ad,te),Teacher(n,a,s,ad,te,ti),Cadre(n,a,s,ad,te,p){ wage=w;//注意这里也要对Person初始化 } void Teacher_Cadre::show(){ Teacher::display();//避免二义性,指明使用Teacher的display函数 cout<<"post: "<<post<<endl; cout<<"wage: "<<wage<<endl; } int main(){ Teacher_Cadre t("lily",20,'f',"dhjsj","12343323","professor","president",1233); t.show(); return 0; }
#include<iostream> #include<string> using namespace std; class Teacher{ public: Teacher(int nu,string n,char s){ num=nu; name=n; sex=s; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } private: int num; string name; char sex; }; class BirthDate{ public: BirthDate(int y,int m,int d){ year=y; month=m; day=d; } void change_date(){ int y,m,d; cout<<endl<<"input the new date:"<<endl; cin>>y>>m>>d; year=y; month=m; day=d; } void display_date(){ cout<<"year: "<<year<<endl; cout<<"month: "<<month<<endl; cout<<"day: "<<day<<endl; } private: int year; int month; int day; }; class Professor:public Teacher{//继承 public: Professor(int nu,string n,char s,int y,int m,int d): Teacher(nu,n,s),birthday(y,m,d){}//对象成员初始化 void show(){ display(); birthday.display_date(); birthday.change_date(); display(); birthday.display_date(); } private: BirthDate birthday; //组合 }; int main(){ Professor p(1001,"lily",'f',1978,2,2); p.show(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。