#include<iostream>
using namespace std;
class graph // 抽象父类
{
public:
virtual void Area() = 0;//定义纯虚函数
virtual void Perimeter() = 0;
};
class Circle :public graph // 定义圆类
{
public:
Circle(float r) :radius(r)// 定义构造函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual ~Circle()//定义析构函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual void Area() //重新定义虚函数
{
cout << "Area of Circle:" << endl << 3.14 * radius * radius << endl;
}
virtual void Perimeter()//重新定义虚函数
{
cout << "Perimeter of Circle" << endl << 3.14 * 2 * radius << endl;
}
private:
float radius;
};
class Rectangle :public graph // 定义矩形类
{
public:
Rectangle(float w, float h) :width(w), height(h)// 定义构造函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual ~Rectangle()//定义析构函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual void Area()
{
cout << "Area of Rectangle:" << endl << width * height << endl;
}
virtual void Perimeter()
{
cout << "Perimeter of Rectangle:" <<endl << 2*(width + height) << endl;
}
private:
float width;
float height;
};
class Triangle :public graph // 定义三角形类
{
public:
Triangle(float l, float w,float h) :width(l), height(w),length(h) // 定义构造函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual ~Triangle()//定义析构函数
{
cout << __func__ << ":" << __LINE__ <<endl;
}
virtual void Area()
{
cout << "Area of Triangle:" << endl << 0.5 * length * height << endl;
}
virtual void Perimeter()
{
cout << "Perimeter of Triangle:" << endl << length + width + height << endl;
}
private:
float length;
float width;
float height;
};
int main()
{
Circle circle(6); //圆
circle.Area();
circle.Perimeter();
Rectangle rectangle(3, 4); //矩形
rectangle.Area();
rectangle.Perimeter();
Triangle triangle(5,4,3); //三角形
triangle.Area();
triangle.Perimeter();
return 0;
}