#include<iostream>
using namespace std;
class Rectangle
{
protected:
float length;
float width;
public:
Rectangle(float l, float w)
{
length = l;
width = w;
}
Rectangle()
{
length = 0;
width = 0;
}
float area()
{
return length * width;
}
void disp()
{
cout<<"矩形面积为"<< area() << endl;
}
};
class Cuboid :public Rectangle
{
private:
float height;
public:
Cuboid(float l, float w, float h)
{
length = l;
width = w;
height = h;
}
Cuboid()
{
length = 0;
width = 0;
height = 0;
}
float calv()
{
return Rectangle::area() * height;
}
void disp()
{
Rectangle::disp();
cout<< "长方体体积为" << calv() << endl;
}
};
int main()
{
Rectangle a(1, 2);
a.disp();
Cuboid c(1,2,3);
c.disp();
getchar();
return 0;
}
大二(下)C++ 类的继承与派生(课堂练习)
最新推荐文章于 2020-05-30 17:07:44 发布