【发布时间】:2020-10-08 07:27:36
【问题描述】:
我正在制作一个小程序,它使用构造函数、类、对象和继承来显示所选项目的价格。但是,派生类中的两个不同构造函数出现两个错误,我该如何解决这个问题?
#include<iostream>
using namespace std;
class Beverage{
public:
int cost_of_water, cost_of_sugar;
Beverage(int x, int y){
cost_of_water = x;
cost_of_sugar = y;
}
int computeCost(){
}
void print(){
cout<<"The cost of the beverage is: ";
}
};
class Tea: public Beverage{
public:
int price_of_tea_leaves;
Tea(int a){
price_of_tea_leaves = a;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_tea_leaves;
return cost;
}
};
class Coffee: public Beverage{
public:
int price_of_coffee_powder;
Coffee(int b){
price_of_coffee_powder = b;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_coffee_powder;
return cost;
}
};
int main(){
int m,n;
cout<<"*****Welcome to the cafeteria management system*****";
cout<<"1 FOR TEA, 2 FOR COFFEE";
cin>>m;
if(m = 1){
Beverage B(10,5);
Tea T(10);
B.print();
T.computeCost();
}
else if (m = 2){
Beverage B(10,5);
Coffee C(15);
B.print();
C.computeCost();
}
else{
cout<<"Thank You!";
}
}
【问题讨论】:
标签: c++ oop inheritance constructor