본문 바로가기

Code/C++

[C++](example) class 생성/ friend/ spy

용용

 

#include <iostream>

using namespace std;

class Point {

        double x;

        double y;

        static int countCreatedObjects;

public:

        Point();

        Point(int x, int y);

        void setPoint(int x, int y);

        int getX(void) const;

        int getY(void) const;

        static int getCreatedObject(void);

        Point operator+ (const Point& point);

        Point& operator= (const Point& point);

        friend ostream& operator<<(ostream& os, const Point& point);

        friend class SpyPoint;

};

int Point::countCreatedObjects = 0;

Point::Point() {

        x = y = 0;

        countCreatedObjects++;

}

Point::Point(int x, int y) {

        this->x = x;

        this->y = y;

        countCreatedObjects++;

}

void Point::setPoint(int x, int y) {

        this->x = x;

        this->y = y;

}

int Point::getX(void) const {

        return this->x;

}

int Point::getY(void) const {

        return this->y;

}

int Point::getCreatedObject(void) {

        return countCreatedObjects;

}

Point Point::operator+(const Point& point) {

        Point result(this->x + point.getX(), this->y + point.getY());

        return result;

}

Point& Point::operator=(const Point& point) {

        this->x = point.getX();

        this->y = point.getY();

        return *this;

}

ostream& operator<<(ostream& os, const Point& point) {

        return os << "(" << point.x << ", " << point.y << ")";

}

class SpyPoint {

public:

        void printPoint(const Point& point);

};

void SpyPoint::printPoint(const Point& point) {

        cout << "(X:" << point.x << ")" << "(y:"<<point.y<<")" << endl;

}

int main() {

        Point* pP1;

        Point* pP2;

        SpyPoint SP;

        cout << "Number of created object is : " << Point::getCreatedObject() << endl;

        pP1 = new Point;

        pP2 = new Point(1, 2);

        pP1->setPoint(10, 20);

        *pP2 = *pP1 + *pP2;

        cout << "[X:" << pP1->getX() << "]" << "[Y:" << pP1->getY() << "]" << endl;

        cout << *pP2 << endl;

        cout << "Number of created object is : " << Point::getCreatedObject() << endl;

        SP.printPoint(*pP1);

        SP.printPoint(*pP2);

        delete pP1;

        delete pP2;

}

 

 

 

 

 

Number of created object is : 0

[X:10][Y:20]

(11, 22)

Number of created object is : 3

(X:10)(y:20)

(X:11)(y:22)