#include "stdafx.h"
#include <iostream>
using namespace std;
//基类 A b:a c:a d:public a,public b
class Furniture
{
int weight;
public:
Furniture() //不允许使用virtual
{
cout << "Furniture create" << endl;
}
virtual ~Furniture()
{
cout << "Furniture delete" << endl;
}
virtual void set_weight(int w)
{
weight = w;
cout << "Furniture set_weight" <<endl;
}
virtual int get_weight()
{
cout << "Furniture get_weight" <<endl;
return weight;
}
};
class Bed:virtual public Furniture
{
int weight;
public:
Bed()
{
cout << "Bed create" << endl;
}
~Bed()
{
cout << "Bed delete" << endl;
}
void sleep()
{
cout << "Bed sleep" <<endl;
}
void set_weight(int w)
{
weight = w;
cout << "Bed set_weight" <<endl;
}
int get_weight()
{
cout << "Bed get_weight" <<endl;
return weight;
}
};
class Sofa:virtual public Furniture
{
int weight;
public:
Sofa()
{
cout << "Sofa create" << endl;
}
~Sofa()
{
cout << "Sofa delete" << endl;
}
void watch_TV()
{
cout << "Sofa watch_TV" <<endl;
}
void set_weight(int w)
{
weight = w;
cout << "Sofa set_weight" <<endl;
}
int get_weight()
{
cout << "Sofa get_weight" <<endl;
return weight;
}
};
class Sleep_Sofa:public Bed,public Sofa //两个父类Bed与Sofa都重载了基类的两个函数set_weight/get_weight 会报Furniture::set_weight的重写不明确 解决办法只有本类重写这两个函数
{
int weight;
public:
Sleep_Sofa()
{
cout << "Sleep_Sofa create" << endl;
}
~Sleep_Sofa()
{
cout << "Sleep_Sofa delete" << endl;
}
void fold_out()
{
cout << "Sleep_Sofa fold_out" <<endl;
}
void set_weight(int w)
{
weight = w;
cout << "Sleep_Sofa set_weight" <<endl;
}
int get_weight()
{
cout << "Sleep_Sofa get_weight" <<endl;
return weight;
}
};
//多继承要加虚继承 虚析构 重写函数要变为虚函数
//缺点:基类会多次创建(加虚继承)
//基类A指向d子类的时候会出现基类A不明确的问题 (加虚继承)
//子类d调用a基类成员的时候会出现不明确调用的问题,不知道调用b指向的基类成员还是c只想的基类成员,当然这个可以通过限定作用域来解决,即在它前加上对应的一个类名((加虚继承可以解决编译报错,但是要得到我们想要的结果,还需要在被重写的函数前加virtual,当然如果不重写就不用加)
//在析构的时候只析构了父类,然后报错(需要加虚析构)
int main()
{
//----------------------------------加了虚继承与虚析构、虚函数-----------------------------------------
Furniture *fn = new Sleep_Sofa;
fn->set_weight(100);
cout << fn->get_weight() << endl;
delete fn; //没有虚析构 调用Furniture delete之后报错
fn = nullptr;
/*
只有Sofa重载了基类函数、调用Sofa的成员函数
Furniture create
Bed create
Sofa create
Sleep_Sofa create
Sofa set_weight
Sofa get_weight
100
Sleep_Sofa delete
Sofa delete
Bed delete
Furniture delete
*/
/*
b c d三个类都重写了基类的成员(被重写的成员函数 只需要a基类声明它为virtual就行,后边重写的类都不用加virtual,但是也是virtual)
Furniture create
Bed create
Sofa create
Sleep_Sofa create
Sleep_Sofa set_weight
Sleep_Sofa get_weight
100
Sleep_Sofa delete
Sofa delete
Bed delete
Furniture delete
*/
//-------------------加了虚继承(只需要b、c加虚继承)与虚析构(只需要a基类加虚析构)------------------------
/*Furniture *fn = new Sleep_Sofa;
fn->set_weight(100);
cout << fn->get_weight() << endl;
delete fn; //没有虚析构 调用Furniture delete之后报错
fn = nullptr;*/
/*
只需要a基类加虚析构
Furniture create
Bed create
Sofa create
Sleep_Sofa create
Furniture set_weight
Furniture get_weight
100
Sleep_Sofa delete
Sofa delete
Bed delete
Furniture delete
*/
//-------------------------加了虚继承与虚析构---------------------------------------
/*Furniture *fn = new Sofa;
fn->set_weight(100);
cout << fn->get_weight() << endl;
delete fn; //没有虚析构 调用Furniture delete之后报错
fn = nullptr;*/
/*
加了虚析构之后
Furniture create
Sofa create
Furniture set_weight
Furniture get_weight
100
Sofa delete
Furniture delete
*/
//-------------------------没加虚继承与只加了虚继承---------------------------------------
//Sofa sf;
/*Sleep_Sofa ss;
Furniture fn = ss; //error 基类Furniture不明确 加了虚继承就可以了
//Furniture fn = sf; //这里不再创建 Furniture 只加虚继承这里的输出还是一样
fn.set_weight(1);
cout << fn.get_weight() << endl;*/
/* 只是虚继承 fn指向sf的输出
Furniture create
Sofa create
Furniture set_weight
Furniture get_weight
1
*/
/*只是虚继承 fn指向ss的输出
Furniture create
Bed create
Sofa create
Sleep_Sofa create
Furniture set_weight
Furniture get_weight
1
*/
return 0;
}