//比如有一个类
class student { public: void display() {cout<<"num:"<num<<endl; cout<<"name"<<nam<<endl; cout<<"sex"<<sex<<endl; } private: int num; string nam; char sex; };
现在想增加student 类的信息,可以重新写
class student1 { public: void display() {cout<<"num:"<num<<endl; cout<<"name"<<nam<<endl; cout<<"sex"<<sex<<endl; cout<<"age"<<age<<endl; cout<<"address"<<addr<<endl; } private: int num; string nam; char sex; int age; char addr{20}; };
也可以通过继承来实现
class student1: public student //声明基类是student, { public: void display() //新增加的成员函数 {cout<<"age"<<age<<endl; cout<<"address"<<addr<<endl;} private: int age; //新增加的数据成员 char addr{20}; //新增加的数据成员 };
声明派生类的一般形式
#include<iostream> #include<string> using namespace std; class student //基类 { public: student(int n, string nam, char s) //定义基类构造函数 { num = n; name = nam; sex = s; } ~student() {} //基类的虚构函数 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: student1(int n, string nam, char s, int a) :student(n, nam, s) //派生类构造函数 { age = a; } void display1() //新增加的成员函数 { display(); //调用基类函数访问基类私有数据 cout << "age:" << age << endl; } private: int age; //新增加的数据成员 }; int main() { student1 stud(10010, "liu", ‘m‘, 29); stud.display1(); return 0; }
student1虽然继承了student类,但是在派生类中的函数不能调用基类的私有成员,
#include<iostream> #include<string> using namespace std; class student //基类 { public: student(string nam) //定义基类构造函数 { name = nam; } ~student() {} //基类的虚构函数 void display() { cout << "the name is:" << name << endl; } protected: string name; }; class student1 : public student //公共派生类 { public: student1(string nam,string nam1 ,int a) :student(nam),monitor(nam1) //派生类构造函数,子对象的初始化是在建立派生类时通过调用派生类构造函数来实现的 { age = a; } void display1() //新增加的成员函数 { display(); cout << "age:" << age << endl; } void show_monitor() { cout<<"class monior is :"<<endl; monitor.display(); } private: int age; //新增加的数据成员 student monitor; }; int main() { student1 stud("A171", "li", 29); stud.display1(); return 0; }
以上派生类构造函数实现了
class A { A(int a,int b,int c); }; class B:public A { B(int a,int b,int c,int d ):A(a,b,c) { dive=d; } ... }; class C:public B { C(int a,int b,int c,int d,int e):B(a,b,c,d) { exchange=e; } };
不要列出每一层派生类的构造函数,只需写出其上一层派生类的构造函数即可。
class A { public: int a; void display(); }; class B { public: int a; void display(); }; class C :public:A,public:B { public: int b; void show(); }; (为了简化,。没有写函数的定义) int main() { C c1; c1.a=3; //出错,编译器无法判别要访问的是哪个基类的a, c1.dispaly(); //出错,编译器无法判别要访问的是哪个基类的display() c1.A::a=3; //正确,使用基类名A来限定 }
class N { public: int a; void display(){ cout<<"A::a"<<a<<endl; } }; class A { public: int a1; void display(); }; class B { public: int a2; void display(); }; class C :public:A,public:B { public: int a3; void show(){ cout<<"a3="<<a3<<endl; } }; int main() { C c1; ... }
class A {A(int i){} //虚基类构造函数,有一个参数 }; class B: virtual public A {B(int i):A(i){} //在初始化表对虚基类初始化 }; class c: virtual public A { c (int i):A(i){} };、 class d: public B,public c { d(int i):A(i),B(i),c(i){} //对初始化表中所有基类初始化 };
规定:在最后的派生类中不仅要负责对直接基类初始化,还要对虚基类初始化。
原文:https://www.cnblogs.com/lanjieduanxin/p/11561223.html