#include<iostream>
using namespace std;
//图形工具类的构建和使用,完成对基类Point,和图形子类的操作应用。
//体验封装,继承,多态的综合应用。
class Point {
public:
Point(int xa, int yb) { //有参构造函数
x = xa;
y = yb;
}
Point() {
x = 0;
y = 0;
}
//定义虚函数display(),满足子类继承时的多态效应。
virtual void display() { //显示图形
cout<<"调用Point的display函数,显示一个点.\n"<< "坐标:(" << x << ", " << y << ")" << endl;
}
//由于在Point基类阶段,无法实现实际的求面积方法,因此将get_area()设计为纯虚函数。
virtual double get_area() = 0;
//析构函数通常定义为虚函数
virtual ~Point()
{
cout << "Point destructor" << endl;
}
protected:
int x; //x坐标
int y; //y坐标
};
//Square继承了Point
class Square : public Point {
public:
Square(int xa, int xb, int len):Point(xa,xb),length(len) {
}
Square() {
length = 1;
}
void display() {
//显示一个方形
cout<<"调用Square的display函数,显示一个正方形.\n"<< "坐标:(" << x << ", " << y << "), 边长:" << length <<endl;
}
double get_area() {
return length * length;
}
//析构函数通常定义为虚函数
~Square()
{
cout << "Square destructor" << endl;
}
protected:
int length;
};
//Circle继承了Point
class Circle: public Point {
public:
Circle(int xa, int xb, int r):Point(xa,xb) {
radius = r;
}
Circle() {
radius = 1;
}
void display() { //显示一个圆
cout<<"调用Circle的display函数,显示一个圆.\n"<< "坐标:(" << x << ", " << y << "), 半径:" << radius <<endl;
}
double get_area() {
return 3.14 * radius * radius;
}
//析构函数通常定义为虚函数
~Circle()
{
cout << "Circle destructor" << endl;
}
protected:
int radius;
};
//编写一个Triangle类,继承Point,属性包含底边长和高
//并有自己的构造函数,display函数和get_area函数,以及析构函数
class Triangle: public Point {
public:
Triangle(int xa, int xb, int be, int h):Point(xa, xb) {
bottom_edge = be;
height = h;
}
Triangle() {
bottom_edge = 1;
height = 1;
}
void display() { //显示一个三角形
cout<<"调用Triangle的display函数,显示一个三角形.\n"<< "坐标:(" << x << ", " << y << "), 底边长:" << bottom_edge << ", 高:" << height <<endl;
}
double get_area() {
return 0.5 * bottom_edge * height;
}
~Triangle() {
cout << "Triangle destructor" << endl;
}
protected:
int bottom_edge;
int height;
};
//编写一个工具类ShapeTool,它可以动态申请和注销图形。
//还可以加载图形,显示图形信息。
class ShapeTool {
public:
//构造函数
ShapeTool(){
p = NULL;
}
//装载一个图形
void loadshape(Point* sp){
p = sp;
}
//显示一个图形的通用函数Disp
void Disp() {
p->display();
}
//显示一个图形面积的通用函数:Area_of_shape
void Area_of_shape() {
//补全代码,完成功能
//-------------------------------------------------
}
//函数create_shapes实现在堆内存中,动态生成一个图形。
//option表示图形类型,1为方形,2为圆形,3为三角形
void create_shape(int option) {
//补全代码,实现功能
//--------------------------------------------
}
//注销动态申请的图形空间
void clear(){
if(p != NULL)
{
delete p;
p = NULL;
}
}
private:
//根据上述设计和实现,补全此处属性设计,注意属性的类型。
//----------------------------------------------
};
int main() {
//在堆内存中生成不同的图形。
//定义基类图形指针
ShapeTool st;
//生成一个方形
st.create_shape(1);
//显示方形基本信息以及面积
st.Disp();
st.Area_of_shape();
st.clear();
//生成一个圆形
st.create_shape(2);
//显示这圆形的基本信息,以及面积。
st.Disp();
st.Area_of_shape();
st.clear();
//生成一个三角形
st.create_shape(3);
//依次显示这组三角形的基本信息,以及面积。
st.Disp();
st.Area_of_shape();
st.clear();
//定义一个圆形
Circle c(1, 2, 3);
//补全代码,完成st对c的装载
//-------------------------
//显示c的基本信息。
st.Disp();
st.Area_of_shape();
return 0;
}
测试输入:
预期输出:
调用Square的display函数,显示一个正方形.
坐标:(0, 0), 边长:1
1
Square destructor
Point destructor
调用Circle的display函数,显示一个圆.
坐标:(0, 0), 半径:1
3.14
Circle destructor
Point destructor
调用Triangle的display函数,显示一个三角形.
坐标:(0, 0), 底边长:1, 高:1
0.5
Triangle destructor
Point destructor
调用Circle的display函数,显示一个圆.
坐标:(1, 2), 半径:3
28.26
Circle destructor
Point destructor