# C++习题 虚函数-计算图形面积

C++习题 虚函数-计算图形面积

``````12.6
3.5
4.5 8.4
2.0 4.5 3.2
4.5 8.4``````

``total of all areas=574.11``

``````#include <iostream>
#include <iomanip>
using namespace std;
//定义抽象基类Shape
class Shape
{
public:
virtual double area() const =0;     //纯虚函数
};

//定义Circle(圆形)类
class Circle:public Shape
{
public:
virtual double area() const
{
};   //定义虚函数
protected:
};

//定义Square(正方形)类
class Square:public Shape
{
public:
Square(double s):side(s) {}         //结构函数
virtual double area() const
{
return side*side;   //定义虚函数
}
protected:
double side;
};

//定义Rectangle(矩形)类
class Rectangle:public Shape
{
public:
Rectangle(double w,double h):width(w),height(h) {}    //结构函数
virtual double area() const
{
return width*height;   //定义虚函数
}
protected:
double width,height;    //宽与高
};

//定义Trapezoid(梯形)类
class Trapezoid:public Shape
{
public:
Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) {} //结构函数
virtual double area() const
{
return 0.5*(top+bottom)*height;   //定义虚函数
}
protected:
double top,bottom,height;        //上底、下底与高
};

//定义Triangle(三角形)类
class Triangle:public Shape
{
public:
Triangle(double w,double h):width(w),height(h) {}               //结构函数
virtual double area() const
{
return 0.5*width*height;   //定义虚函数
}
protected:
double width,height;                                            //宽与高
};

int main()
{
float r,a,b,w1,w2,w,h;
cout<<fixed<<setprecision(2);
cin>>r;
Circle circle(r);
cin>>a;
Square square(a);
cin>>a>>b;
Rectangle rectangle(a,b);
cin>>w1>>w2>>h;
Trapezoid trapezoid(w1,w1,h);
cin>>w>>h;
Triangle triangle(w,h);
Shape *pt[5]= {&circle,&square,&rectangle,&trapezoid,&triangle};
double areas=0.0;
for(int i=0; i<5; i++)
areas=areas+pt[i]->area();
cout<<"total of all areas="<<areas<<endl;
return 0;
}
``````

``````#include <iostream>
#include <iomanip>
using namespace std;
//定义抽象基类Shape
class Shape
{
public:
virtual double area() const =0;     //纯虚函数
};

//定义Circle(圆形)类
class Circle:public Shape
{
public:
virtual double area() const
{
};   //定义虚函数
protected:
};

//定义Square(正方形)类
class Square:public Shape
{
public:
Square(double s):side(s) {}         //结构函数
virtual double area() const
{
return side*side;   //定义虚函数
}
protected:
double side;
};

//定义Rectangle(矩形)类
class Rectangle:public Shape
{
public:
Rectangle(double w,double h):width(w),height(h) {}    //结构函数
virtual double area() const
{
return width*height;   //定义虚函数
}
protected:
double width,height;    //宽与高
};

//定义Trapezoid(梯形)类
class Trapezoid:public Shape
{
public:
Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) {} //结构函数
virtual double area() const
{
return 0.5*(top+bottom)*height;   //定义虚函数
}
protected:
double top,bottom,height;        //上底、下底与高
};

//定义Triangle(三角形)类
class Triangle:public Shape
{
public:
Triangle(double w,double h):width(w),height(h) {}               //结构函数
virtual double area() const
{
return 0.5*width*height;   //定义虚函数
}
protected:
double width,height;                                            //宽与高
};

int main()
{
float r,a,b,w1,w2,w,h;
cout<<fixed<<setprecision(2);
cin>>r;
Circle circle(r);
cin>>a;
Square square(a);
cin>>a>>b;
Rectangle rectangle(a,b);
cin>>w1>>w2>>h;
Trapezoid trapezoid(w1,w1,h);
cin>>w>>h;
Triangle triangle(w,h);
Shape *pt[5]= {&circle,&square,&rectangle,&trapezoid,&triangle};
double areas=0.0;
for(int i=0; i<5; i++)
areas=areas+pt[i]->area();
cout<<"total of all areas="<<areas<<endl;
return 0;
}
``````