6-2 设计一个三角形Triangle类(C++构造函数) (10 分)
设计一个三角形Triangle类。这个类包括: 两个名为width和height的double型数据域,它们分别表示三角形的底宽和高。一个为width和height指定初值的构造函数。 一个名为getArea()的方法返回这个三角形的面积。
类名为:
Triangle
裁判测试程序样例:
#include
using namespace std;
// 你提交的代码将被嵌入到这里
int main( )
{
double w,h;
cin>>w>>h;
Triangle t(w,h);
cout<<"The area of the Triangle is: "<<t.getArea( )<<endl;
}
输入样例:
3.5 4.8
输出样例:
The area of the Triangle is: 8.4
class Triangle
{
private:
double width;//宽
double height;//高
public:
Triangle(double x,double y)//有参构造函数
{
width=x;
height=y;
}
double getArea()//得到面积
{
double area;
area=width*height/2;
return area;
}
};