C++ 中的虚:虚基类和虚继承
一、问题由来
以沙发床为例:沙发床继承了 沙发 和 床。
#include <iostream>
#include<string>
using namespace std;
class Sofa
{
public:
Sofa(float pe = .0, string cr = "black")
:price_(pe), color_(cr)
{}
void disInfo()
{
cout << "price: " << price_ << endl;
cout << "color: " << color_ << endl;
}
void sit()
{
cout << "It can be used to sit." << endl;
}
protected:
float price_;
string color_;
};
class Bed
{
public:
Bed(float pe = .0, string cr = "black")
:price_(pe), color_(cr)
{}
void disInfo()
{
cout << "price: " << price_ << endl;
cout << "color: " << color_ << endl;
}
void sleep()
{
cout <