标签:数通 引用 必须 derived 问题 类的成员 ios 类设计 形式
格式:
class 派生类名:继承方式 基类名, 继承方式 基类名....
{
类体
}
继承形式多种多样:
继承会把基类的所有成员均继承
graph LR
A类-->B
A类-->C
graph LR
A类-->B类
B类-->C类
graph LR
A类-->B类
B类-->C类
A类-->D类
D类-->C类
继承方式有:public, protected, privated
对于继承来的基类是把基类所有成员(除开基类的构造函数与析构函数)都继承了的,仅是对于访问权限的不同.一般说来基类的私有成员,派生类是无法直接访问的,任何继承方式都是这样
public:基类的public和protected在派生类中依旧是public和privated,但是基类的privated在派生类中将无法直接访问(可用继承的基类函数对这些私有成员访问进行操作)
protected:基类的public和protected在派生类中变成protected,基类的privated在派生类中将无法直接访问
privated:基类的public和protected在派生类中变成privated,基类的privated在派生类中将无法直接访问
类的派生:
除开继承来的东西还加入了直接的特性
#include
using namespace std;
class Base1{ //基类base1定义
public:
void display() const{
cout << "Base1::display()" << endl;
}
};
class Base2:public Base1{//共有派生类base2定义
public:
void display() const{
cout << "Base2::disply()" << endl;
}
};
class Derived:public Base2{
public:
void display() const{
cout << "Derived::display()" << endl;
}
};
void fun(Base1 *ptr)
{
ptr->display();
}
int main()
{
Base1 base1;
Base2 base2;
Derived derived;
fun(&base1);
fun(&base2);
fun(&derived);
return 0;
}
结果:
#include
using namespace std;
class Base1 {
public:
Base1(int i) { cout << " Constructing Base1" << endl; }
Base1() = default;
};
class Base2 {
public:
Base2(int j) { cout << "Constructing Base2" << endl; }
Base2() = default;
};
class Base3 {
public:
Base3() { cout << "Constructing Base3 *" << endl; }
};
class Derived :public Base2, public Base1, public Base3 {
public:
Derived(int a, int b, int c, int d) :member2(d), member1(c), Base1(a), Base2(b){}
Derived(int k) :Derived() { w = k, h = k; }
Derived() = default;
private:
Base1 member1;
Base2 member2;
Base3 memeber3;
int w, h;
};
int main()
{
Derived obj(1, 2, 3, 4);
return 0;
}
先按照继承顺序对类进行构造初始化,再按照对象顺序对对象进行初始化,图中第24行的委托构造函数要单独进行
#include
using namespace std;
class Base1 {
public:
Base1(int i)
{
cout << "Constructing Base1" << i << endl;
}
~Base1() { cout << "Destructing Base1" << endl; }
};
class Base2 {
public:
Base2(int j)
{
cout << "Constructing Base2" << j << endl;
}
~Base2() { cout << "Destructing Base2 " << endl; }
};
class Base3 {
public:
Base3() { cout << "Constructing Base3 *" << endl; }
~Base3() { cout << "Destructing Base3" << endl; }
};
class Derived :public Base2, public Base1, public Base3 {
public:
Derived(int a, int b,int c, int d):Base1(a), member2(d), member1(c), Base2(b){}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main()
{
Derived obj(1, 2, 3, 4);
return 0;
}
解决问题:当派生类从多个基类派生,而这些基类又有共同基类,则在访问此共同基类中的成员时,将产生冗余,并有可能因冗余带来不一致性
虚基类的声明:
虚基类的作用:
注意:
在第一级继承时就要将共同基类设计为虚基类
#include
using namespace std;
class Base0 {
public:
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1 :virtual public Base0 {
public:
int var1;
};
class Base2 :virtual public Base0 {
public:
int var2;
};
class Derived :public Base1, public Base2 {
public:
int var;
void fun() {
cout << "Member of Derived" << endl;
}
};
int main()
{
Derived d;
d.var0 = 2;
d.fun0();
cout << &d.Base1::var0 << endl;
cout << &d.Base2::var0 << endl;
return 0;
}
#include
using namespace std;
class Base0 {
public:
Base0(int var):var0(var){}
int var0;
void fun0() { cout << "Member of Base0" << endl; }
};
class Base1 :virtual public Base0 {
public:
Base1(int var) :Base0(var) {}
int var1;
};
class Base2 :virtual public Base0 {
public:
Base2(int var) :Base0(var){}
int var2;
};
class Derived :public Base1, public Base2 {
public:
Derived(int var) :Base0(var), Base1(var), Base2(var){}
int var;
void fun()
{
cout << "Member of Derived" << endl;
}
};
int main()
{
Derived d(1);
d.var0 = 2;
d.fun0();
return 0;
}
标签:数通 引用 必须 derived 问题 类的成员 ios 类设计 形式
原文地址:https://www.cnblogs.com/Liberavi/p/11668019.html