Please tell me the order in which the results of the generator extinction program example came out

Asked 2 years ago, Updated 2 years ago, 89 views

# include <iostream> 

using namespace std ; 

class Point {
    int x, y ;
    public: 
        Point(int x=0, int y=0) {
            this->x = x ; this->y = y ; 
            cout << "\tPoint " ; print() ; cout << " constructed." << endl ;
        } 
        ~Point() { cout << "\tPoint " ; print() ; cout << " destructed." << endl ; } 
        void print() const { cout << "(" << x << ", " << y << ")" ; } 
} ; 
class Rectangle { 
    Point rightBottom, leftTop ; 
    public: 
        Rectangle(const Point& p1, const Point& p2=Point(0,0)) : leftTop(p1), rightBottom(p2) {
            cout << "Rectangle: " ; print() ; cout << " constructed." << endl ;
        } 
        Rectangle(int x1, int y1, int x2=0, int y2=0) : leftTop(x1, y1), rightBottom(x2, y2) {
            cout << "Rectangle: " ; print() ; cout << " constructed." << endl ; 
        } 
        Rectangle() { cout << "Rectangle: " ; print() ; cout << " constructed." << endl ; } 
        ~Rectangle() { cout << "Rectangle: " ; print() ; cout << " destructed." << endl ; } 
        void print() const { leftTop.print() ; rightBottom.print() ; } 
} ;

int main(){
    Point p(2, 2);
    Rectangle r2(p);
}

Results of

I can, but I don't know what order the program goes through to get this result. Help me

constructor destructor

2022-09-22 18:43

1 Answers

I added a little more logs and tried to run them.

The compiler used VS2015.



# # include <iostream> 

using namespace std;

class Point {
    int x, y;
public:
    Point(int x = 0, int y = 0) {
        this->x = x; this->y = y;
        cout << "\tPoint "; print(); cout << " constructed." << endl;
    }
    ~Point() { cout << "\tPoint "; print(); cout << " destructed." << endl; }
    void print() const { cout << "(" << x << ", " << y << ") " << this ; }
};
class Rectangle {
    Point rightBottom, leftTop;
public:
    Rectangle(const Point& p1, const Point& p2 = Point(0, 0)) : leftTop(p1), rightBottom(p2) {
        cout << "Rectangle: "; print(); cout << " constructed." << endl;
    }
    Rectangle(int x1, int y1, int x2 = 0, int y2 = 0) : leftTop(x1, y1), rightBottom(x2, y2) {
        cout << "Rectangle: "; print(); cout << " constructed." << endl;
    }
    Rectangle() { cout << "Rectangle: "; print(); cout << " constructed." << endl; }
    ~Rectangle() { cout << "Rectangle: "; print(); cout << " destructed." << endl; }
    void print() const { leftTop.print(); rightBottom.print(); }
};

int main() {
    Point p(2, 2);
    cout << "1-------1-------1-------" << endl;
    {
        Rectangle r2(p);
        cout << "2-------2-------2-------" << endl;
    }
    cout << "3-------3-------3-------" << endl;
}

Here's the result.

1|         Point (2, 2) 00AFFBA0 constructed.
2| 1-------1-------1-------
3|         Point (0, 0) 00AFFAB8 constructed.
4| Rectangle: (2, 2) 00AFFB90(0, 0) 00AFFB88 constructed.
5|         Point (0, 0) 00AFFAB8 destructed.
6| 2-------2-------2-------
7| Rectangle: (2, 2) 00AFFB90(0, 0) 00AFFB88 destructed.
8|         Point (2, 2) 00AFFB90 destructed.
9|         Point (0, 0) 00AFFB88 destructed.
A| 3-------3-------3-------
B|        Point (2, 2) 00AFFBA0 destructed.


2022-09-22 18:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.