继承
#includeusing namespace std;class father{public: void getHeight(){cout< < weight< height< age<
子类对象赋值给父类
#includeusing 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;//子类赋给父类}
父类引用指向子类对象
#includeusing 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< <
#includeusing namespace std;class father{protected: int height;};//私有派生用的不多,因为继承的成员都变成私有的了,不可以访问class son: private father{ //继承的父类的protected,public全部变成 privatepublic: int weight; int getHeight() { return height; } void setHeight(int x) { this->height=x; }};int main(){ son a ; a.setHeight(1); cout< <
继承中构造函数的执行顺序(先构造基类,后构造子类, 先析构子类,在析构基类)
#includeusing namespace std;class father{private: int height;public: father(){cout<<"father construct"<
多重继承,以 继承的顺序进行构造
向基类构造函数传递参数
#includeusing namespace std;class father{public: int height; int weight;public: father(int height, int weight ){ this->height=height; this->weight=weight; cout<<"father construct"< age=age; cout<<"son construct"<
多继承的歧义(作用域操作符)
#includeusing namespace std;//class father{////public:// int height;// int weight;//public:// father(int height, int weight ){// this->height=height;// this->weight=weight;// cout<<"father construct"< age=age;// cout<<"son construct"<
解决两异性(虚基类)
#includeusing namespace std;class common{public: void stand(){}};class a: virtual public common{public: void hello(){cout<<"a hello"<