What should I do to add code to the Rectangle class so that the code called Rectangle; in main() can be executed?

Asked 2 years ago, Updated 2 years ago, 75 views

class Point{
public:
    int x, y;
    Point(){}
    Point(int xx, int yy) { x = xx; y = yy; }
};
class Rectangle{
    Point p;
    int width, height;
public:
     Rectangle(int x, int y, int w, int h) {
        p.x = x; p.y = y; width = w; height = h;
    }
};

c++ class

2022-09-21 18:53

1 Answers

classPoint { 
public: 
    int x, y;
    Point() {}
    Point(int x, int y) { this->x = x; this->y =y; }
};
class Rectangle: private Point {
    Point p;
    int width, height;
public:
    Rectangle();
    Rectangle(int x, int y, int w, int h) {
        p.x = x; p.y = y; width = w; height = h;
    }
};

Rectangle::Rectangle() {
    x = 1, y = 2, width = 3, height = 4;
    cout << x << " " << y << " " << width << " " << height << endl;
}

From here, it seems that Tturu Ttutu-nim wants to inherit a class called Point and receive the value from Rectangle. Then, add the value of the point to the Rectangle I think I want to get the length and height. If you want to do it in the main() function, you can create the constructor Rectangle(); and then create a phrase that says Rectangle::Rectangle(). Note that if you change the Point statement to Point(intx,inty) { this->x = x; this->y = y;}, you can call yourself and use it comfortably. This is called this pointer.


2022-09-21 18:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.