01.//02.* Copyright (c) 2013, 烟台大学计算机学院
02.//04.* 作 者: 马德鹏
03.//05.* 完成日期:2014 年 6 月 29 日
04.//06.* 版 本 号:v1.0
05.//07.*
#include <iostream>
#include <iomanip>
const double PI=3.1415927;
using namespace std;
class Shape
{
public:
virtual double area() const=0;
};
class Circle:public Shape
{
public:
Circle(double r):R(r){}
double area()const
{
return (PI*R*R);
}
private:
double R;
};
class Square:public Shape
{
public:
Square(double l):L(l){}
double area()const
{
return L*L;
}
private:
double L;
};
class Rectangle:public Shape
{
public:
Rectangle(double l,double w):lon(l),wide(w){};
double area()const
{
return lon*wide;
}
private:
double lon;
double wide;
};
class Trapezoid:public Shape
{
public:
Trapezoid(double l,double d,double h):uplon(l),downlon(d),high(h) {};
double area()const
{
return (uplon+downlon)*high/2;
}
private:
double uplon;
double downlon;
double high;
};
class Triangle:public Shape
{
public:
Triangle(double l,double h):lo(l),hight(h){};
double area() const
{
return (lo*hight)/2;
}
private:
double lo;
double hight;
};
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;
}
虚函数-计算图形面积
最新推荐文章于 2022-06-12 09:44:35 发布