有关派生类的一次实验作业

以点(point)类为基类(不必和上题点类完全一致),定义派生类:矩形类和圆类。
点为直角坐标点,矩形水平放置,由左下方的顶点和长宽定义。圆有圆心和半径定义。
派生类操作判断任一坐标点是在图形内,在图形的边缘上,还是在图形外。
默认初始化图形退化为点。
要求包括拷贝构造函数。编程测试类是否正确。

1. Experimental procedure

#include<iostream>
using namespace std;
typedef double Elementype;
//采用别名方便日后对点数据类型的更改
class Point
{
protected:   
    Elementype X, Y;
public:
    Point(Elementype x = 0, Elementype y = 0):X(x),Y(y){}//带默认参数的构造函数
    Point(Point& p) //点的拷贝构造函数
    {
        p.GetPoint(X, Y);
        cout << "Point coordinates after copying are"
            << "(" << X << "," << Y << ")" << endl;
    }
    void SetPoint(Elementype x, Elementype y)//设置点
    {
        X = x; Y = y;
        cout << "Point coordinates after changing are"
            << "(" << X << "," << Y << ")" << endl;
    }
    void GetPoint(Elementype& x, Elementype& y)//获取点坐标
    {
        x = X; y = Y;
       
    }
    virtual void point_in(Elementype x, Elementype y) 
//创建虚函数point_in判断点
    {
        if (x == X && y == Y)cout << "The point is on the drawing.\n";
        else cout << "The point is outside the drawing.\n";
    }
};
class Rectangle:public Point{
    Elementype Lenth, Width;//矩形长与宽
public:
    Rectangle(Elementype x, Elementype y, Elementype l=0, Elementype w=0)
        :Point(x, y), Lenth(l),Width(w)//Rectangle类构造函数
    { 
        cout << "A rectangle with a length of " << Lenth
            << " and a width of " << Width
            << "and a vertex coordinate of " << "(" << X << "," << Y << ") "
            << " at the bottom left is created\n";
    }void GetRectangle(Elementype &x,Elementype &y,Elementype &l, Elementype &w) {
        x = X; y = Y, l = Lenth; w = Width;       
    }
    Rectangle(Rectangle& r)//矩形的拷贝构造函数
    {
        r.GetRectangle(X,Y, Lenth, Width);
        cout << "Acquire a rectangle with a length of " << Lenth
            << " and a width of " << Width
            << "and a vertex coordinate of " << "(" << X << "," << Y << ") "
            << " at the bottom left is created by copying.\n";
    }virtual void point_in(Elementype x, Elementype y) 
    {
        if (x > X&& x<X + Lenth && y>Y&& y < Y + Lenth)
            cout << "The point is in the rectangle.\n";
        else if (x<X || x>X + Lenth || y<Y || y>Y + Lenth)
            cout << "The point is outside the rectangle.\n";
        else cout << "The point is on the rectangle.\n";
    }
};
class Circle :public Point {
    Elementype Radium;//圆的半径
public:
    Circle(Elementype x, Elementype y, Elementype r=0):Point(x,y),Radium(r)
        //Circle类构造函数
    {
        cout << "A circle with a radium of " << Radium
            << "and a certain of " << "(" << X << "," << Y << ") "
            << "is created.\n";
    }void GetCircle(Elementype& x, Elementype& y, Elementype& r) {
        x = X; y = Y; r = Radium;
    }
    Circle(Circle& c) //圆的拷贝构造函数
    {
        c.GetCircle(X, Y, Radium);
        cout << "A circle with a radium of " << Radium
            << "and a certain of " << "(" << X << "," << Y << ") "
            << "is created by copying.\n";
    }virtual void point_in(Elementype x, Elementype y)
    {
        if (x > X-Radium&& x<X + Radium && y>Y-Radium&& y < Y + Radium)
            cout << "The point is in the circle.\n";
        else if (x<X-Radium || x>X +Radium || y<Y-Radium || y>Y + Radium)
            cout << "The point is outside the circle.\n";
        else cout << "The point is on the circle.\n";
    }
};
int main() {
    Elementype x, y,lenth,width,radium;
    cout << "Please input the first point coordinate:";
    cin >> x >> y;
    cout << "Please input the lenth and the width of the rectangle1:";
    cin >> lenth >> width;
    Rectangle R1(x, y, lenth, width);
    cout << "Please input the radium of the circle1:";
    cin >> radium;
    Circle C1(x, y, radium);
    cout << "Next, test the copy constructor.\n";
    Rectangle R2(0, 0, 2, 2);
    Rectangle R3(R2);
    Circle C2(0, 0, 2);
    Circle C3(C2);
    cout << "Please input a Point :";
    cin >> x >> y;
    R1.point_in(x, y);
    C1.point_in(x, y);
    cout << "Bye!";  
}

Experimental results and analysis

在这里插入图片描述
All tests on copy constructors and derived classes in run structure are normal.

Experimental process analysis

When writing a derived class function, you need to pay attention to the inheritance type of the base class function. When you write a derived class constructor, you need to pay particular attention to the order of assignments in the initialization list.
编写派生类函数时需要注意对基类函数的继承类型。编写派生类构造函数时尤其需要注意初始化列表中的赋值顺序。

Empirical conclusion

I wrote a virtual function in the program to judge whether the point is in the graph. The advantage of using virtual function is that the same function name can be used in the base class and derived class to define different implementations of the function, so as to realize “one interface, multiple ways”.
我在程序中编写了虚函数来判断点是否在图形内,而利用虚函数的好处是:可在基类和派生类中使用相同的函数名定义函数的不同实现,从而实现“一个接口,多种方式”。

By Suki 2020 4 14
cout<<“Good night!”<<endl;

发布了29 篇原创文章 · 获赞 27 · 访问量 2952

猜你喜欢

转载自blog.csdn.net/Eumenides_Suki/article/details/105502015