【发布时间】:2017-03-16 22:08:27
【问题描述】:
#include <string>
#include <iostream>
using namespace std;
class Surgery
{
public:
Surgery();
int getPrice();
string getType();
protected:
int price;
string type;
};
Surgery::Surgery()
{
price = 0;
type = "";
}
int Surgery::getPrice()
{
return price;
}
string Surgery::getType()
{
return type;
}
class Neurosurgery :public Surgery
{
private:
string type = "Neurosurgery";
int price = 23000;
};
class Plastic :public Surgery
{
private:
string type = "Plastic";
int price = 15000;
};
class Trauma :public Surgery
{
private:
string type = "Trauma";
int price = 5000;
};
class Endocrine :public Surgery
{
private:
string type = "Endocrine";
int price = 20000;
};
class Ophthalmological :public Surgery
{
public:
Ophthalmological();
private:
string type;
int price;
};
Ophthalmological::Ophthalmological():Surgery()
{
type = "Ophthalmological";
price = 10000;
}
int main()
{
Ophthalmological var1;
cout << var1.getPrice() << endl;
return 0;
}
当我运行这段代码时,我预计会看到 10000 相反,我看到 0
我非常简单地避免了使用 const 的单一默认构造函数的任何错误。
在 Neurosurgery 之后执行 First Surgery 构造函数。
Neurosurgery 构造函数应该覆盖默认 Surgery 构造函数的值。
我是否以错误的方式使用 c++11
【问题讨论】:
-
从派生类中删除所有数据成员。
标签: c++ c++11 inheritance