C++继承
#include <iostream>
using namespace std;
class Shape
{
public:
void setLength(double len) { length = len; }
void setWidth(double wid) { width = wid; }
void setHeight(double hei) { height = hei; }
double length;
double width;
double height;
};
class painCost
{
public:
int getCost(double area)
{
cost = area * 10;
return cost;
}
double cost;
};
class Rectangle:public Shape
{
public:
int getVolume()
{
return length*width*height;
}
double getArea()
{
double Area = (length*height + length*width + width*height) * 2;
return Area;
}
};
class PainRectangle : public painCost, public Rectangle
{
public:
double budget = 200;
bool isInBudget()
{
if (cost > budget)
return true;
else
return false;
}
};
int main()
{
Rectangle rec;
rec.setLength(4);
rec.setWidth(2);
rec.setHeight(1);
cout << "矩形的体积为:" << rec.getVolume() << endl;
PainRectangle Prec;
Prec.setLength(3);
Prec.setWidth(2);
Prec.setHeight(1);
cout << "长方体的体积为:" << Prec.getVolume() << endl;
cout << "长方体的表面积为:" << Prec.getArea() << endl;
cout << "长方体表面涂漆需要花费:" << Prec.getCost(Prec.getArea()) << endl;
if (Prec.isInBudget())
cout << "花费为:" << Prec.cost << "\t预算为:" << Prec.budget << "\t超出预算" << endl;
else
cout << "花费为:" << Prec.cost << "\t预算为:" << Prec.budget << "\t没有超出预算" << endl;
getchar();
return 0;
}