运行结果好像不太对,结果只是triangle类的结果,并不是total的结果,求指教
/*设shape类为表示形状的抽象类,
里面有一个成员函数area( )用于求图形面积的函数,
为纯虚函数total( )则是一个通用的函数,
用于求不同形状的图形面积总和
shape类派生三角形类(triangle),矩形类(rectangle),
并给出具体的求面积函数,
请将程序补充完整*/
//StudybarCommentBegin
//#include<iostream.h>
#include<math.h>
#include<iostream>
using namespace std;
class shape
{
//StudybarCommentEnd
protected:
double x,y;
public:
shape(){};
shape(double a, double b)
{
x = a;
y = b;
}
double area(){};
virtual double total() = 0;
};
class triangle:public shape //三角形类
{
private:
double n, height;
public:
triangle(double nn, double h)
{
n = nn;
height = h;
}
double area(){return ((1/2)*n*height);}
double total(){}
};
//StudybarCommentBegin
class rectangle:public shape //矩形类
{
private:
int width,length;
public:
rectangle(int c=10,int d=5)//:shape(a,b)
{width=c;length=d;}
double area()
{return width*length;}
double total(){}
};
double total(shape *s[],int n)
{
double sum=0.0;
for(int i=0;i<n;i++)
sum=sum+s[i]->area();
return sum;
}
int main()
{
shape *sp[2] ;
int x,y;
cin>>x>>y;
triangle one(x,y);
rectangle two;
sp[0]=&one;
sp[1]=&two;
cout<<"the total area is "<<total(sp,2)<<endl;
}
//StudybarCommentEnd