using namespace std; class Point { private: int x; int y; static int numCreatedObjects; public: Point() : x(0), y(0) { numCreatedObjects++; } // constructor receiving int_x and int_y as inputs Point(int _x, int _y) : x(_x), y(_y) { numCreatedObjects++; } ~Point() { cout << "Destructed..." << endl; } void setXY(int _x, int _y) { //this-> initialization with this->x = _x; this->y = _y; } int getX() const { return x; } int getY() const { return y; } // // *this + pt2 -> Point operator+(Point& pt2) { Point result(this->x + pt2.getX(), this->y + pt2.getY()); return result; } //operator overloading Point& operator=(Point& pt) { this->x = pt.getX(); this->y = pt.getY(); return *this; } static int getNumCreatedObject() { return numCreatedObjects; } friend void print(const Point& pt); friend ostream& operator<<(ostream& cout, Point& pt); friend class SpyPoint; }; //initialize static member variables (numCreatedObjects) int Point::numCreatedObjects = 0; //when object call by reference: only const method is available in function when entering function with const // const: The member data inside the object is constant (unchanging) void print(const Point& pt) { cout << pt.x << ", " << pt.y << endl; } //Point operator+(Point& pt1, Point& pt2){ // // Point result(pt1.getX() + pt2.get(X), pt1.getY() + pt2.getY()); // // return result; //} ostream& operator<<(ostream& cout, Point& pt) { return cout << pt.x << ", " << pt.y; } class SpyPoint { public: //Implement the hack_all_info function to output as follows //Hacked by SpyPoint //x: 40 //y: 60 //numCreatedObj.: 10 void hack_all_info(Point& pt) { cout << "Hacked by SpyPoint" << endl << "x: " << pt.x << endl << "y: " << pt.y << endl << "numCreatedObj.: " << Point::getNumCreatedObject() << endl <<endl; } }; int main() { Point pt1(1, 2); cout << "pt1 : "; print(pt1); cout << endl;
// pointer
Point* pPt1 = &pt1;
// Call getX, getY through the value of pPt1 and output
cout << "pt1 : ";
cout << (*pPt1).getX() << ", " << (*pPt1).getY() << endl;
// Call getX, getY through pPt1 and output
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << endl;
//Dynamically allocate Point* pPt2, add 10,20 and output using -> (see pt1 output)
Point* pPt2;
pPt2 = new Point(10, 20);
cout << "pt2 : ";
cout << pPt2->getX() << ", " << pPt2->getY() << endl;
cout << endl;
//pPt1, Release memory on pPt2
delete pPt2;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl;
// Operator Overloading
//pt4 = pt2, plus pt3
Point pt2(10, 20);
Point pt3(30, 40);
Point pt4 = pt2 + pt3;
cout << "pt2 : ";
cout << pt2 << endl;
cout << "pt3 : ";
cout << pt3 << endl;
cout << "pt4 : ";
cout << pt4 << endl;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl << endl;
// // object array
Point* ptAry = new Point[5];
cout << "pt2 NumCreatedObject : ";
cout << pt2.getNumCreatedObject() << endl;
cout << endl;
// ptAry memory release
delete[] ptAry;
cout << endl;
// // friend class
SpyPoint spy;
cout << "pt1 info" << endl;
spy.hack_all_info(pt1);
cout << "pt4 info" << endl;
spy.hack_all_info(pt4);
return 0;
} If I do this pt1 : 1, 2
pt1 : 1, 2 pt1 : 1, 2
pt2 : 10, 20
Destructed... pt1 NumCreatedObject : 2 Destructed... pt2 : 10, 20 pt3 : 30, 40 pt4 : 40, 60 pt1 NumCreatedObject : 5
pt2 NumCreatedObject : 10
Destructed... Destructed... Destructed... Destructed... Destructed...
pt1 info Hacked by SpyPoint x: 1 y: 2 numCreatedObj.: 10
pt4 info Hacked by SpyPoint x: 40 y: 60 numCreatedObj.: 10
Destructed... Destructed... Destructed... Destructed... It comes out like this
Destructed... pt1 NumCreatedObject : 2 Destructed...This is
Destructed... Destructed... pt1 NumCreatedObject : 2 Can't we change it like this? pt1 NumCreatedObject: I don't understand why Destroyed
c++
I don't know much about c++, but isn't the distructor stipulated that "if you don't run this, this instance won't be a destruct"? So, if an instance that can conditionally print "pt1 NumCreatedObject" is given a destructor to print "Destroyed..." that instance will always print "pt1 NumCreatedObject" or not at the end. What's born is always dead, and the instance that's created is always dead.
I think that's all I can give you without knowing c++. I'll have to watch the rest of it for the other monster to see.
590 Understanding How to Configure Google API Key
586 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
689 Uncaught (inpromise) Error on Electron: An object could not be cloned
671 GDB gets version error when attempting to debug with the Presense SDK (IDE)
591 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.