C++_day8_ 多重继承、钻石继承和虚继承

时间:2023-03-08 21:15:48
C++_day8_ 多重继承、钻石继承和虚继承

1.继承的复习

1.1 类型转换

编译器认为访问范围缩小是安全的。

1.2 子类的构造与析构

子类中对基类构造函数初始化只能写在初始化表里,不能写在函数体中。

阻断继承。

1.3 子类的拷贝构造与拷贝赋值

2. 多重继承、钻石继承和虚继承

  • 多重继承

  一个类可以同时从多个基类继承实现代码。

  C++_day8_ 多重继承、钻石继承和虚继承

  C++_day8_ 多重继承、钻石继承和虚继承

  示例代码:

 #include <iostream>

 using namespace std;

 class Phone{
public:
Phone(string const& no):m_no(no){
cout << "Phone构造" << this << endl;
}
~Phone(void)
{
cout << "Phone析构" << this << endl;
}
void call (string const& no)
{
cout << m_no << "呼叫" << no << endl;
}
private:
string m_no;
}; class Player{
public:
Player(string const& media):m_media(media)
{
cout << "Player构造" << this << endl;
}
~Player(void)
{
cout << "Player析构" << this << endl;
}
void play(string const& clip)
{
cout << m_media << "播放" << clip << endl;
}
private:
string m_media;
}; class Computer{
public:
Computer(string const& os):m_os(os)
{
cout << "Computer构造" << this << endl;
}
~Computer(void)
{
cout << "Computer析构" << this << endl;
}
void run(string const& app)
{
cout << "在" << m_os << "上运行" << app << endl;
}
private:
string m_os;
}; class SmartPhone:public Phone, public Player, public Computer{
public:
SmartPhone (string const& no, string const& media, string const& os):Phone(no), Player(media), Computer(os){}
private:
}; int main(void)
{
SmartPhone sp("", "MP3/MP4/3GP", "Andriod");
sp.call("");
sp.play("我还年轻");
sp.run("JBG大战Victor Wang");
Phone* pPhone = &sp; //访问范围缩小,不会报错
cout << "&sp = " << &sp << endl;
cout << "pPhone = " << pPhone << endl;
Player* pPlayer = &sp;
cout << "pPlayer = " << pPlayer << endl;
Computer* pComputer = &sp;
cout << "pComputer = " << pComputer << endl;
/*
SmartPhone* pSmart = static_cast<SmartPhone*> (pComputer);
cout << "pSmart = " << pSmart << endl;
*/ /*
SmartPhone* pSmart = (SmartPhone*) pComputer;
cout << "pSmart = " << pSmart << endl;
*/ SmartPhone* pSmart = reinterpret_cast <SmartPhone*> (pComputer);
cout << "pSmart = " << pSmart << endl; return ;
}

  C++_day8_ 多重继承、钻石继承和虚继承

  名字冲突问题:

  1.

 class C: public A, public B{
public:
using A::foo;
using B::foo; };

  2.

 /*
c.A::foo();
c.B::foo(100);
*/

  3.

 class C: public A, public B{
public:
/*
using A::foo;
using B::foo;
*/
void foo(int f, int x)
{
if(f == )
A::foo();
else if(f == )
B::foo(x);
}
};
  • 钻石继承

     一个子类继承自多个基类,而这些基类有源自共同的祖先,这样的继承结构成为钻石继承(菱形继承)。

  C++_day8_ 多重继承、钻石继承和虚继承

  C++_day8_ 多重继承、钻石继承和虚继承

  C++_day8_ 多重继承、钻石继承和虚继承

  • 虚继承

  C++_day8_ 多重继承、钻石继承和虚继承

  示例代码:

 #include <iostream>

 using namespace std;

 class A{
public:
A(int data):m_data(data)
{
cout << "A构造" << this << endl;
}
protected:
int m_data;
}; class B: virtual public A {
public:
B(int data): A(data)
{
cout << "B构造" << this << endl;
}
void set(int data)
{
cout << "B:" << &m_data << endl;
m_data = data;
}
}; class C: virtual public A {
public:
C(int data): A(data)
{
cout << "C构造" << this << endl;
}
int get (void) const
{
cout << "C:" << &m_data << endl;
return m_data;
}
}; class D: public B, public C{
public:
D(int data):B(data), C(data), A(data) {}
}; int main(void)
{
D d();
d.set();
cout << d.get() << endl;
cout << sizeof(d) << endl; return ;
}

虚继承是为了解决钻石继承的问题。