6014: 长方体体积(继承和派生)
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:
提交:1
解决:1
题目描述
定义一个Rectangle(长方形)类,它包含两个数据成员长和宽;以及包含用于求长方形面积的成员函数CalArea()。
再定义Rectangle的派生类Rectangular(长方体),它包含一个新数据成员高度和用来求长方体体积的成员函数CalVolume()。
在main函数中,分别使用这两个类求某个长方形的面积和某个长方体的体积。
主函数如下:
int main()
{
double length,width,height;
cin>>length>>width;
Rectangle rectangle(length,width);
cout<<rectangle.CalArea()<<endl;
cin>>length>>width>>height;
Rectangular rectangular(length, width, height);
cout<<rectangular.CalVolume()<<endl;
return 0;
}
再定义Rectangle的派生类Rectangular(长方体),它包含一个新数据成员高度和用来求长方体体积的成员函数CalVolume()。
在main函数中,分别使用这两个类求某个长方形的面积和某个长方体的体积。
主函数如下:
int main()
{
double length,width,height;
cin>>length>>width;
Rectangle rectangle(length,width);
cout<<rectangle.CalArea()<<endl;
cin>>length>>width>>height;
Rectangular rectangular(length, width, height);
cout<<rectangular.CalVolume()<<endl;
return 0;
}
输入
输入数据有二行,第一行2个空格分隔的整数,表示矩形的长和宽,第二行3个空格分隔的整数,代表长方体的长宽高。
输出
输出数据2行,输出长方体的底面积和体积。
样例输入 复制
3 4
3 4 5
样例输出 复制
12
60