c++程序怎么编

来源:百度知道 编辑:UC知道 时间:2024/06/18 18:38:08
下面的Shape类是一个表示形状的抽象类。请从Shape类派生三角形类(Triangle)、矩形类(Rectangle),并用一个普通函数total( )求2个图形的面积总和,两个图形的数据在定义对象时给出。
class Shape
{public:
virtual void area()=0;// area( )为求图形面积的纯虚函数

#include<iostream.h>
#include<math.h>

class shape
{
public:
virtual float area()=0;
};

class rectangle:public shape
{
float width,height;
public:
rectangle(float w,float h){width=w;height=h;}
void show()
{
cout<<"width:"<<width<<","<<"height:"<<height<<endl;
cout<<"area:"<<area()<<endl;
}
float area(){return width*height;}
};

class triangle:public shape
{
float a,b,c;
public:
triangle(float aa,float bb,float cc)
{a=aa;b=bb;c=cc;}
float show()
{
cout<<"a:"<<a<<",b:"<<b<<",c:"<<c<<endl;
cout<<&quo