#include<iostream>
#include<iomanip>
using namespace std;
class Rice
{
float price_per_kg, total_weight;
public:
Rice(float w)
{
price_per_kg = 10.0;
total_weight = w;
}
void display_rice()
{
cout<<"----------------------------------------"<<endl;
cout<<"\tRice Details"<<endl;
cout<<fixed<<setprecision(2);
cout<<"Total weight\t : "<<total_weight<<endl;
cout<<"Price perkg (RM): "<<price_per_kg<<endl;
cout<<"Total (RM)\t : "<<total_weight*price_per_kg<<endl;
}
};
class Product:public Rice
{
float kg;
public:
Product operator+(const Product &p)
{
return Rice(kg + p.kg);
}
void Setdata()
{
cout << "Enter product's weight(kg): ";
cin >> kg;
}
};
int main()
{
Product a, b;
a.Setdata();
b.Setdata();
Rice h = a + b;
h.display_rice();
}
I got this error:
[Error] could not convert 'Rice((((Product*)this)->Product::kg + ((float)p.Product::kg)))' from 'Rice' to 'Product'
Is there any way to solve the error? I have tried to use other ways to solve but the question requires operator overloading.
The compiler is telling you a Rice
isn't a Product
.
Either you change the return type
Rice operator+(const Product &p)
Or you change the expression you return
{
Product result = *this;
result.kg += p.kg; // or some other things?
return result;
}
Or you re-think your classes. Do you really mean that a Product
is a Rice
and not the other way around? At the moment a Product
has both a kg
member directly, and a total_weight
member within the Rice
base-subobject.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments