// 4 单一继承 /* #include <iostream> using namespace std; class fatcher { private: int height, width; public: void setHeight(int h){ this->height = h; } void setWidth(int w){ this->width = w; } void show() { cout<<"身高="<<height<<" 体重="<<width<<endl; } }; class sun : public fatcher { private: int sunJK, sunPC; public: void setSunJK(int j){ this->sunJK = j;} void setSunPC(int j){ this->sunPC = j;} void showSun(){ cout<<"JK:"<<sunJK<<" PC:"<<sunPC<<endl;} }; int main() { sun a; a.setHeight(170); a.setWidth(50); a.setSunJK(100); a.setSunPC(200); a.show(); a.showSun(); return 0; }*/ //5 公有型 保护型和私有型 /* #include <iostream> using namespace std; class fatcher { //private: protected: int height, width; public: void setHeight(int h){ this->height = h; } void setWidth(int w){ this->width = w; } void show() { cout<<"身高="<<height<<" 体重="<<width<<endl; } }; class sun : public fatcher { private: int sunJK, sunPC; public: void setSunJK(int j){ this->sunJK = j;} void setSunPC(int j){ this->sunPC = j;} //void showSun(){ cout<<"JK:"<<sunJK<<" PC:"<<sunPC<<endl;} void show(){ cout<<"身高="<<height<<" 体重="<<width<<endl; cout<<"JK:"<<sunJK<<" PC:"<<sunPC<<endl;} }; int main() { sun a; a.setHeight(170); a.setWidth(50); a.setSunJK(100); a.setSunPC(200); a.show(); //a.showSun(); return 0; }*/ //6 公有派生的公有成员 /* #include <iostream> using namespace std; class fatcher { public: void root(){ cout<<"父亲的大房子我也可以享受"<<endl;} }; //公有继承对父类的公有属性,就像自己的一样,可以随便用 class son:public fatcher { }; int main() { son s; s.root(); return 0; }*/ // 7 公有派生的保护成员 /* #include <iostream> using namespace std; class fatcher { protected: void room(){ cout<<"父亲的大房子我也可以享受"<<endl;} }; //公有继承对父类的公有属性,就像自己的一样,可以随便用 //公有继承对像,父类的保护成员在子类中也是保护成员 class son:public fatcher { public: void enjoy(){ room(); } //只能通过公用的接口来访问父类的保护成员room() }; int main() { son s; //s.root(); //无法访问保护成员 s.enjoy(); return 0; }*/ //8 公有派生的私有成员 /*#include <iostream> using namespace std; class fatcher { protected: void room(){ cout<<"父亲的大房子我也可以享受"<<endl;} private: void secret(){ cout<<"父亲的密秘"<<endl;} }; //公有继承对父类的公有属性,就像自己的一样,可以随便用 //公有继承对像,父类的保护成员在子类中也是保护成员 class son:public fatcher { public: void enjoy(){ room(); //secret(); 不能访问你类的私有成员 } //只能通过公用的接口来访问父类的保护成员room() }; int main() { son s; //s.root(); //无法访问保护成员 s.enjoy(); return 0; }*/ //公有派生的赋值问题 /* #include <iostream> using namespace std; class father { public: int x; void show(){ cout<<"x:"<<x<<endl;} }; class son : public father { public: int y; }; int main() {*/ /*father Nick; son Jack; Jack.y = 11; Jack.x = 22; Jack.show(); Nick = Jack; //这里可以将子类的对像赋值给父类,但不能把你类的对像赋值给子类 Nick.show();*/ /*father *p; son Jack; p = &Jack; p->x = 3; cout<<p->x<<endl;*/ /* son Jack; father &Nick = Jack; return 0; }*/ // 私有派生 /* #include <iostream> using namespace std; class father { public: void room(){cout<<"父亲的大房子我可以享受"<<endl;} }; //私有继承 //class son: private father //保护继承 class son: protected father { public: void enjoy(){room();} }; int main() { son a; a.enjoy(); return 0; }*/ // 11 多重继承 /* #include <iostream> using namespace std; class father { public: void setA(int a){toll = a;} void printA(){ cout<<"身高:"<<toll<<endl;} private: int toll; }; class mother { private: int weight; public: void setB(int w){ weight = w;} void printB(){ cout<<"体重"<<weight<<endl;} }; //class son: public father, public mother class son: public father, private mother //以私有方式继承后,基类的私有成员变成不可访问,而公有成员和保护成员也变成了私有,派生类中的成员想要访问他们必须定义一个公有函数作为接口 { private: int age; public: void setC(int a){ age = a;} void setb(int b){ setB(b);} //必须定义一个公有的接口来访问私有成员,两个成员不可访问的问题 void printC(){ printA(); printB(); cout<<"年龄:"<<age<<endl; } }; int main() { son s; s.setA(170); //s.setB(50); s.setb(50); s.setC(23); s.printC(); return 0; }*/ //12 继承和构造与析构 /* #include <iostream> using namespace std; class father { private: int a; public: father(int i){ a= i; cout<<"构造基类a的值:"<<a<<endl;} ~father(){ cout<<"析构基类a的值"<<a<<endl;} }; class son: public father { private: int b; public: son(int i, int j); ~son(){cout<<"析构子类b的值:"<<b<<endl;} }; son::son(int i, int j):father(i) { b = j; cout<<"构造子类的b的值:"<<b<<endl; }; int main() { son a(11,22); return 0; }*/ //构造和析构的顺序 //先构造父类的构造函数,然后构造子类的构造函数 //先析构子类的析构函数,然后析构父类的析构函数 // 向基类构造函数传递参数 /* #include <iostream> #include <string> using namespace std; class father { protected: string name; int tall; public: father(string a, int i){ name=a, tall=i; cout<<"执行带两个参数的基类对像"<<endl; }; father(){cout<<"构造基类"<<endl;} //这里是输出字符串的 void print(){cout<<" 身高为:"<<tall<<" Name:"<<name<<endl;} ~father(){cout<<"释放基类对像"<<endl;} };*/ /* father::father(string a, int i) { cout<<"\n 在基类的构造函数内\n"; name = a; tall = i; print(); cout<<"离开基类构造函数"<<endl; };*/ /* class son:public father { private: int weight; public: son(string a, int i, int j); void print1(); ~son(){cout<<"离开子类对像"<<endl;} }; son::son(string a, int i, int j):father(a, i) { //name = a; //tall = i; weight = j; cout<<"在子类的构造函数内"<<endl; }; void son::print1() { father::print(); cout<<"体重:"<<weight<<endl; } int main() { son Make("Mike",180,77); Make.print1(); cout<<"程序执行完成"<<endl; return 0; }*/ // 向基类构造函数传递参数 /* #include <iostream> #include <string> using namespace std; class father { protected: string name; int tall; public: father(string a, int i); father(){cout<<"构造基类"<<endl;} //这里是输出字符串的 void print(){cout<<" 身高为:"<<tall<<" Name:"<<name<<endl;} ~father(){cout<<"释放基类对像"<<endl;} }; father::father(string a, int i) { cout<<"\n 在基类的构造函数内\n"; name = a; tall = i; print(); cout<<"离开基类构造函数"<<endl; }; class son:public father { private: int weight; public: son(string a, int i, int j); void print1(); ~son(){cout<<"离开子类对像"<<endl;} }; son::son(string a, int i, int j) { name = a; tall = i; weight = j; cout<<"在子类的构造函数内"<<endl; }; void son::print1() { father::print(); cout<<"体重:"<<weight<<endl; } int main() { son Make("Mike",180,77); Make.print1(); cout<<"程序执行完成"<<endl; return 0; } */ //多重继承容易产生两义性 /* #include <iostream> using namespace std; class A { public: void hello(){cout<<"基类A的hello函数"<<endl;} }; class B { public: void hello(){cout<<"基类B的hello函数"<<endl;} }; class C : public A, public B { public: void hello(){cout<<"基类C的hello函数"<<endl;} }; int main() { C c; c.hello(); //这里多重继承容易产生两义性 c.A::hello(); //这里可以去掉两义性 c.B::hello(); return 0; } */ //15 继承中的重载 /* #include <iostream> using namespace std; class father { public: void hello(){cout<<"基类的事带参数的hello函数"<<endl;} void hello(int i){ cout<<"基类的带一个参数hello函数"<<endl;} }; class son:public father { public: void hello(){cout<<"子类的事带参数的hello函数"<<endl;} //当我们在子类中定义一个与基类同名的函数时,那么等于是告诉编译器,用子类的函数覆盖掉基类的同名函数,同时将它重载函数隐藏起来. void hello()const{cout<<"子类的事带参数的const hello函数"<<endl;} //这里与hello()函数是不一样的,因为hello()const函数只能被const对像所调用 void hello(int i){ cout<<"子类的带一个参数hello函数"<<endl;} }; int main() { son s; s.hello(); //s.hello(2); //将不能再访问了 const son ss; ss.hello(); return 0; }*/ /* #include <iostream> using namespace std; class A { public: void hello(){cout<<"基类A的hello函数"<<endl;} void hello(int i) { cout<<"a.hello(i):"<<i<<endl; cout<<"基类a的带一个参数的hello函数"<<endl; } }; class B : public A { public: //void hello(){ cout<<"子类b的hello函数"<<endl;} //还是一种方法是添加const,这样就不会把父类的函数隐藏起来了 void hello()const{ cout<<"子类b的hello函数"<<endl;} void hello(int i)const{ cout<<"b.hello(i):"<<i<<endl; cout<<"子类B的带一个参数的hello函数"<<endl; } }; int main() { B b; b.hello(); b.hello(11); //b.hello(11); //把带参的重载函数会是被隐藏起来了 //b.A::hello(11); //呵呵,这样也行,不握你隐藏起来 return 0; }*/ // 16 两义性的归属问题 //一个派生类从多个基类派生,而这些基类双有一个共同的基类,那么在派生类中访问共同的基类中的成员时会产生两义性 /* #include <iostream> using namespace std; class Human { public: void stand(){cout<<"人类能够直立行走"<<endl;} }; class father: public Human { }; class mother: public Human { }; class son : public father, public mother { }; int main() { son s; //s.stand();//两义性的归属问题 s.mother::stand(); return 0; }*/ //17 虚基类不会产生两义性 /* #include <iostream> using namespace std; #include <iostream> using namespace std; class Human { public: void stand(){cout<<"人类能够直立行走"<<endl;} }; //virtual 声明为虚基类 class father:virtual public Human { }; class mother:virtual public Human { }; class son : public father, public mother { }; int main() { son s; //s.stand();//两义性的归属问题 //s.mother::stand(); s.stand(); return 0; };*/ #include <iostream> using namespace std; #include <iostream> using namespace std; class Human { public: void stand(){cout<<"人类能够直立行走"<<endl;} }; //virtual 声明为虚基类 class father:virtual public Human { public: void stand(){cout<<"父类能够直立行走"<<endl;} }; class mother:virtual public Human { public: void stand(){cout<<"母类能够直立行走"<<endl;} }; class son : public father, public mother { public: void stand(){cout<<"子类能够直立行走"<<endl;} }; int main() { son s; s.stand(); father f; f.stand(); mother m; m.stand(); Human h; h.stand(); return 0; };