习题 12.4 编写一个程序,声明抽象基类Shape,由它派生出3个派生类:Circle、Rectangle、Triangle,用一个函数pritArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。
代码:
#include<iostream>
#include<iomanip>
using namespace std;
class Shape
{
public:
Shape() {}
virtual ~ Shape() {}
virtual double printArea() const = 0;
};
class Circle : public Shape
{
public:
Circle(){}
Circle(double r):radius(r){}
virtual double printArea() const;
private:
double radius;
};
double Circle::printArea() const
{
return (3.14159 * radius * radius);
}
class Rectangle : public Shape
{
public:
Rectangle() {}
Rectangle(double h,double w) :height(h) , width(w) {}
virtual double printArea() const;
private:
double height;
double width;
};
double Rectangle::printArea() const
{
return (height * width);
}
class Triangle : public Shape
{
public:
Triangle() {}
Triangle(double h,double b) :height(h),bottom(b) {}
virtual double printArea() const;
private:
d