01.//02.* Copyright (c) 2013, 烟台大学计算机学院
02.//04.* 作 者: 马德鹏
03.//05.* 完成日期:2014 年 6 月 19 日
04.//06.* 版 本 号:v1.0
05.//07.*
#include <iostream>
#include <iomanip>
double PI=3.14159738;
using namespace std;
class Shape
{
public:
virtual void Area() const=0;
};
class Circle:public Shape
{
public:
Circle(float r):R(r) {}
void Area()const;
private:
float R;
};
void Circle::Area()const
{
cout<<R*R*PI<<endl;
}
class Rectangle:public Shape
{
public:
Rectangle(float l,float w):lon(l),wide(w) {}
void Area()const;
private:
float lon,wide;
};
void Rectangle::Area()const
{
cout<<lon*wide<<endl;
}
class Triangle:public Shape
{
public:
Triangle(float l,float h):lo(l),hight(h) {}
void Area()const;
private:
float lo,hight;
};
void Triangle::Area()const
{
cout<<lo*hight/2<<endl;
}
void printArea(Circle&c)
{
c.Area();
}
void printArea(Rectangle&r)
{
r.Area();
}
void printArea(Triangle&t)
{
t.Area();
}
int main()
{
float r,a,b,w,h;
cout<<fixed<<setprecision(2);
cin>>r;
Circle circle(r);
cout<<"area of circle = ";
printArea(circle);
cin>>a>>b;
Rectangle rectangle(a,b);
cout<<"area of rectangle = ";
printArea(rectangle);
cin>>w>>h;
Triangle triangle(w,h);
cout<<"area of triangle = ";
printArea(triangle);
return 0;
}
抽象基类
最新推荐文章于 2023-12-09 22:57:01 发布