#include<stdio.h> #include<iostream> using namespace std; class Goods { public: virtual double goodsprice()=0; virtual double show()=0; ~Goods(){} }; class rice: public Goods { private: double weight; double price; double goodsprice() { return weight*price; } public: rice(){} rice(const double w,const double p) { weight=w; price=p; } double show() { return goodsprice(); } friend double total(const rice r,const pork p,const oil o); }; class pork : public Goods { private: double weight; double price; double goodsprice() { return weight*price; } public: pork(const double w,const double p) { weight=w; price=p; } double show() { return goodsprice(); } friend double total(const rice r,const pork p,const oil o); }; class oil : public Goods { private: double weight; double price; double goodsprice() { return weight*price; } public: oil(const double w,const double p) { weight=w; price=p; } double show() { return goodsprice(); } friend double total(const rice r,const pork p,const oil o); }; double total(const rice r,const pork p,const oil o) { return (r.goodsprice()+p.goodsprice()+o.goodsprice()); } int main() { rice r(5.0,2.4); pork p(2.0,10.0); oil o(1.0,50.0); cout<<"大米价格为"<<r.show()<<"元"<<endl; cout<<"猪肉价格为"<<p.show()<<"元"<<endl; cout<<"油价格为"<<o.show()<<"元"<<endl; //cout<<"商品的总价格为"<<total()<<"元"<<endl; system("pause"); return 0; } 哪里错了
时间: 2024-03-17 09:45:56 浏览: 83
代码中存在以下几个问题:
1. 头文件 `<iostream>` 应该改为 `<iostream.h>`,因为这是 C++ 的标准头文件。
2. `show()` 和 `goodsprice()` 方法的返回值类型应该是 `double`,而不是 `void`。
3. `total()` 函数中的参数应该是对象的引用(即加上 `&` 符号),而不是对象本身。
4. `total()` 函数中的 `goodsprice()` 方法应该改为对象的公有方法,而不是私有方法。
下面是修改后的代码:
```c++
#include<stdio.h>
#include<iostream.h>
using namespace std;
class Goods {
public:
virtual double goodsprice() = 0;
virtual double show() = 0;
virtual ~Goods(){}
};
class rice: public Goods {
private:
double weight;
double price;
double goodsprice()
{
return weight * price;
}
public:
rice(){}
rice(const double w, const double p)
{
weight = w;
price = p;
}
double show()
{
return goodsprice();
}
friend double total(const rice& r, const pork& p, const oil& o);
};
class pork : public Goods {
private:
double weight;
double price;
double goodsprice()
{
return weight * price;
}
public:
pork(const double w, const double p)
{
weight = w;
price = p;
}
double show()
{
return goodsprice();
}
friend double total(const rice& r, const pork& p, const oil& o);
};
class oil : public Goods {
private:
double weight;
double price;
double goodsprice()
{
return weight * price;
}
public:
oil(const double w, const double p)
{
weight = w;
price = p;
}
double show()
{
return goodsprice();
}
friend double total(const rice& r, const pork& p, const oil& o);
};
double total(const rice& r, const pork& p, const oil& o) {
return (r.show() + p.show() + o.show());
}
int main() {
rice r(5.0, 2.4);
pork p(2.0, 10.0);
oil o(1.0, 50.0);
cout << "大米价格为" << r.show() << "元" << endl;
cout << "猪肉价格为" << p.show() << "元" << endl;
cout << "油价格为" << o.show() << "元" << endl;
cout << "商品的总价格为" << total(r, p, o) << "元" << endl;
system("pause");
return 0;
}
```
阅读全文