c++学习-继承
继承
#include <iostream> using namespace std; class father{ public: void getHeight(){cout<<height<<endl;} void getAge(){cout<<age<<endl;} protected: int height,age; }; class son: public father{ private: int weight; public: void getWeight(){cout<<weight<<endl;} void shwo() { cout<<this->weight<<endl; cout<<this->height<<endl; cout<<this->age<<endl; } }; int main() { son one; one.shwo(); }
子类对象赋值给父类
#include <iostream> using namespace std; class father{ public: int height; }; class son: public father{ public: int weight; }; int main() { son a; father b; a.height=1; b=a;//子类赋给父类 }
父类引用指向子类对象
#include <iostream> using namespace std; class father{ public: int height; }; class son: public father{ public: int weight; }; int main() { father *p; son jack; p=&jack; //父类引用指向子类对象 父类还可以作为子类的别名(虚函数) p->height=110; cout<<jack.height<<endl; cout<<jack.weight<<endl; }
#include <iostream> using namespace std; class father{ protected: int height; }; //私有派生用的不多,因为继承的成员都变成私有的了,不可以访问 class son: private father{ //继承的父类的protected,public全部变成 private public: int weight; int getHeight() { return height; } void setHeight(int x) { this->height=x; } }; int main() { son a ; a.setHeight(1); cout<<a.getHeight()<<endl; }
继承中构造函数的执行顺序(先构造基类,后构造子类, 先析构子类,在析构基类)
#include <iostream> using namespace std; class father{ private: int height; public: father(){cout<<"father construct"<<endl;} ~father(){cout<<"father destruct"<<endl;} }; class son: public father{ public: int weight; son(){cout<<"son construct"<<endl;} ~son(){cout<<"son destruct"<<endl;} }; int main() { son a ; return 0; }
多重继承,以 继承的顺序进行构造
向基类构造函数传递参数
#include <iostream> using namespace std; class father{ public: int height; int weight; public: father(int height, int weight ){ this->height=height; this->weight=weight; cout<<"father construct"<<endl; } ~father(){cout<<"father destruct"<<endl;} }; class son: public father{ public: int age; son(int height,int weight,int age); ~son(){cout<<"son destruct"<<endl;} }; son::son(int height, int weight, int age):father(height, weight) { this->age=age; cout<<"son construct"<<endl; } int main() { son a(1,2,3) ; cout<<a.height<<endl; cout<<a.weight<<endl; cout<<a.age<<endl; return 0; }
多继承的歧义(作用域操作符)
#include <iostream> using namespace std; //class father{ // //public: // int height; // int weight; //public: // father(int height, int weight ){ // this->height=height; // this->weight=weight; // cout<<"father construct"<<endl; // } // ~father(){cout<<"father destruct"<<endl;} // //}; // //class son: public father{ //public: // int age; // son(int height,int weight,int age); // ~son(){cout<<"son destruct"<<endl;} // //}; // //son::son(int height, int weight, int age):father(height, weight) //{ // this->age=age; // cout<<"son construct"<<endl; //} 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 i; i.hello(); //本类的无需 i.a::hello(); //作用域操作符,作用域分辨 i.b::hello(); return 0; }
解决两异性(虚基类)
#include <iostream> using namespace std; class common{ public: void stand(){} }; class a: virtual public common{ public: void hello(){cout<<"a hello"<<endl;} }; class b: virtual public common{ public: void hello(){cout<<"b hello"<<endl;} }; class c : public a, public b{ public: void hello(){cout<<"c hello"<<endl;} }; int main() { c i; i.stand(); return 0; }