文章目录

  • ​​1.多重继承​​
  • ​​2.虚继承与虚基类​​
  • ​​3.虚基类及其派生类构造函数​​

1.多重继承

  • 单重继承:一个派生类最多只能有一个基类
  • 多重继承:一个派生类可以有多个基类
    (1)派生类同时继承多个基类的成员,更好的软件重用
    (2)可能会有大量的二义性,多个基类中可能包含同名变量或函数
    (3)用法
class 类名: 继承方式 基类, 继承方式 基类2,...
{...};
  • 多重继承中解决访问歧义的方法:
    明确指明要访问定义于哪个基类中的成员
基类名::数据成员名(或成员函数(参数表))
  • eg:P32\01.cpp
#include <iostream>
using namespace std;

class Bed
{
public:
Bed(int weight) : weight_(weight)
{

}
void sleep()
{
cout<<"Sleep..."<<endl;
}


int weight_;
};

class Sofa
{
public:
Sofa(int weight) : weight_(weight)
{

}
void WatchTV()
{
cout<<"Watch TV..."<<endl;
}
int weight_;
};

//多重继承
//沙发床
class SofaBed : public Sofa, public Bed
{
public:
SofaBed() : Bed(0), Sofa(0)
{
FoldInt();
}
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldInt()
{
cout<<"FoldIn..."<<endl;
}
};

int main(void)
{
SofaBed sofaBed;
//error,指定不明确,是Bed的weight_还是Sofa的weight_?
// sofaBed.weight_ = 10;
// sofaBed.weight_ = 10;

sofaBed.Bed::weight_ = 10;
sofaBed.Sofa::weight_ = 20;
sofaBed.WatchTV();
sofaBed.FoldOut();
sofaBed.sleep();

return 0;
}
  • eg:P32\02.cpp
#include <iostream>
using namespace std;

//Bed,Sofa,SofaBed都是家具,都有weight这个属性
class Furniture
{
public:
Furniture(weight) : weight_(weight)
{

}
int weight_;
};

class Bed : public Furniture
{
public:
Bed(int weight) : Furniture(weight)
{

}
void sleep()
{
cout<<"Sleep..."<<endl;
}

};

class Sofa: public Furniture
{
public:
Sofa(int weight) : Furniture(weight)
{

}
void WatchTV()
{
cout<<"Watch TV..."<<endl;
}
};

//多重继承
//沙发床
class SofaBed : public Sofa, public Bed
{
public:
SofaBed(int weight) : Bed(weight), Sofa(weight)
{
FoldInt();
}
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldInt()
{
cout<<"FoldIn..."<<endl;
}
};

int main(void)
{
SofaBed sofaBed(10);
//error,指定不明确,是Bed的weight_还是Sofa的weight_?二义性
//多重继承容易出现二义性
// sofaBed.weight_ = 10;
// sofaBed.weight_ = 10;

sofaBed.Bed::weight_ = 10;
sofaBed.Sofa::weight_ = 20;
sofaBed.WatchTV();
sofaBed.FoldOut();
sofaBed.sleep();

return 0;
}

类图

Bed类继承至Furniture类;

SofaBed有2个weight_;

(P32)继承:多重继承 ,虚继承与虚基类 ,虚基类及其派生类构造函数_派生类

2.虚继承与虚基类

  • 当派生类从多个基类派生,而这些基类又从同一个基类派生,则在访问此共同基类中的成员时,将产生二义性——采用虚基类来解决。
    eg:P32\02.cpp中,SofaBed类应该只有一个weight_,也不应该有2个weight_
  • 虚基类的引入
    用于有共同基类的场合
  • 声明
以virtual修饰说明基类
class B1: virtual public BB
等价于
class B1: public virtual BB

类图:钻石继承体系,通常需要虚继承

若Bed类从Furniture类中虚继承下来,Furniture类是Bed类的虚基类,sofa类类似,

Bed类和sofa类会共享Furniture类中的weight_数据成员,继承下来只有1份拷贝

(P32)继承:多重继承 ,虚继承与虚基类 ,虚基类及其派生类构造函数_构造函数_02

  • 作用
    (1)主要用来解决多继承时,可能发生的对同一基类继承多次而产生的二义性问题
    (2)为最远的派生类提供唯一的基类成员,而不重复产生多次拷贝
  • eg:P32\03.cpp
#include <iostream>
using namespace std;

//Bed,Sofa,SofaBed都是家具,都有weight这个属性
class Furniture
{
public:
Furniture(weight) : weight_(weight)
{

}
int weight_;
};

class Bed : virtual public Furniture
{
public:
// Bed(int weight) : Furniture(weight)
// {

// }
void sleep()
{
cout<<"Sleep..."<<endl;
}
};

class Sofa: virtual public Furniture
{
public:
// Sofa(int weight) : Furniture(weight)
// {

// }
void WatchTV()
{
cout<<"Watch TV..."<<endl;
}
};

//多重继承
//沙发床
class SofaBed : public Sofa, public Bed
{
public:
// SofaBed(int weight) : Bed(weight), Sofa(weight)
// {
// FoldInt();
// }
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldInt()
{
cout<<"FoldIn..."<<endl;
}
};

int main(void)
{
SofaBed sofaBed;
//这样就没有二义性了
sofaBed.weight_ = 10;

sofaBed.WatchTV();
sofaBed.FoldOut();
sofaBed.sleep();

return 0;
}

3.虚基类及其派生类构造函数

  • 虚基类的成员是由最远派生类的构造函数通过调用虚基类的构造函数进行初始化的
  • 在整个继承结构中,直接或间接继承虚基类的所有派生类,都必须在构造函数的成员初始化列表中给出对虚基类的构造函数的调用。
    如果未列出,则表示调用该虚基类的默认构造函数。
    看eg:P32\04.cpp
  • 在建立对象时,只有最远派生类(也是最底层)的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类构造函数的调用被忽略。
    看eg:P32\04.cpp,SofaBed类调用虚基类的构造函数,Bed类和Sofa类不再调用虚基类的构造函数了,确保weight_只被构造一次。
  • eg:P32\04.cpp
#include <iostream>
using namespace std;

//Bed,Sofa,SofaBed都是家具,都有weight这个属性
class Furniture
{
public:
Furniture(weight) : weight_(weight)
{
cout<<"Furniture ..."<<endl;
}
//虚基析构函数
~Furniture()
{
cout<<"~Furniture ..."<<endl;
}
int weight_;
};

//Furniture类是Bed类的虚基类
class Bed : virtual public Furniture
{
public:
//如果Furniture不是虚基类,会先调用Furniture的构造函数
Bed(int weight) : Furniture(weight)
{
cout<<"Bed ..."<<endl;
}
~Bed()
{
cout<<"~Bed ..."<<endl;
}
void sleep()
{
cout<<"Sleep..."<<endl;
}
};

class Sofa: virtual public Furniture
{
public:
Sofa(int weight) : Furniture(weight)
{
cout<<"Sofa ..."<<endl;
}
~Sofa()
{
cout<<"~Sofa ..."<<endl;
}
void WatchTV()
{
cout<<"Watch TV..."<<endl;
}
};

//多重继承
//沙发床
//使用SofaBed类来构造对象时,weight_应该在哪里被构造呢?如果在Bed或者Sofa类中
//构造,就会出现weight_被构造2次的情况,所以应该在SofaBed类中构造
//下面是最底层的派生类的构造函数
class SofaBed : public Sofa, public Bed
{
public:
SofaBed(int weight) : Bed(weight), Sofa(weight), Furniture(weight)
{
cout<<"SofaBed ..."<<endl;
FoldInt();
}
~SofaBed()
{
cout<<"~SofaBed ..."<<endl;
}
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldInt()
{
cout<<"FoldIn..."<<endl;
}
};

int main(void)
{
SofaBed sofaBed(5);
//这样就没有二义性了
sofaBed.weight_ = 10;

sofaBed.WatchTV();
sofaBed.FoldOut();
sofaBed.sleep();

return 0;
}
  • 测试:
    首先调用SofaBed的构造函数,接着调用基类的构造函数,首先会调用最顶层的基类的构造函数,其调用顺序与 Bed(weight), Sofa(weight), Furniture(weight)初始化列表的顺序无关,先调用Furniture类的构造函数,接着调用Bed类的构造函数(why不是Sofa类的构造函数?因为这与class SofaBed : public Sofa, public Bed继承的前后顺序有关)