PP.249 MyFriendClass.cpp
| |
| #ifndef __BOY_H__ |
| #define __BOY_H__ |
| #include "Girl.h" |
| |
| class Boy |
| { |
| private: |
| int height; |
| friend class Girl; |
| public: |
| Boy(int len); |
| void ShowYourFriendInfo(Girl& frn); |
| }; |
| |
| #endif |
| |
| |
| #include <iostream> |
| #include <cstring> |
| #include "Boy.h" |
| using namespace std; |
| |
| Boy::Boy(int len) : height(len) {} |
| |
| void Boy::ShowYourFriendInfo(Girl& frn) |
| { |
| cout << "소녀의 전화번호 : " << frn.phoneNum << endl; |
| } |
| |
| #ifndef __GIRL_H__ |
| #define __GIRL_H__ |
| #include "Boy.h" |
| |
| class Girl |
| { |
| private: |
| char phoneNum[20]; |
| friend class Boy; |
| public: |
| Girl(const char* phnum); |
| void ShowYourFriendInfo(Boy& frn); |
| }; |
| |
| #endif |
| |
| #include <iostream> |
| #include <cstring> |
| #include "Girl.h" |
| using namespace std; |
| |
| Girl::Girl(const char* phnum) |
| { |
| strcpy(phoneNum, phnum); |
| } |
| |
| void Girl::ShowYourFriendInfo(Boy& frn) |
| { |
| cout << "소년의 키 : " << frn.height << "cm" << endl; |
| } |
| |
| #include <iostream> |
| #include <cstring> |
| #include "Boy.h" |
| #include "Girl.h" |
| using namespace std; |
| |
| int main() |
| { |
| Boy boy(170); |
| Girl girl("010-123-4567"); |
| boy.ShowYourFriendInfo(girl); |
| girl.ShowYourFriendInfo(boy); |
| return 0; |
| } |
PP. 252 MyFriendFunction.cpp
| #include <iostream> |
| #include <cstring> |
| using namespace std; |
| |
| class Point; |
| |
| class PointOP |
| { |
| private: |
| int opcnt; |
| public: |
| PointOP(): opcnt(0) {} |
| Point PointAdd(const Point&, const Point&); |
| Point PointSub(const Point&, const Point&); |
| ~PointOP() |
| { |
| cout << "동작 회수 : " << opcnt << endl; |
| } |
| }; |
| |
| class Point |
| { |
| private: |
| int x; |
| int y; |
| public: |
| Point(const int& xpos, const int& ypos) : x(xpos), y(ypos) {} |
| friend Point PointOP::PointAdd(const Point&, const Point&); |
| friend Point PointOP::PointSub(const Point&, const Point&); |
| friend void ShowPointPos(const Point&); |
| }; |
| |
| Point PointOP::PointAdd(const Point& pt1, const Point& pt2) |
| { |
| opcnt++; |
| return Point(pt1.x + pt2.x, pt1.y + pt2.y); |
| } |
| Point PointOP::PointSub(const Point& pt1, const Point& pt2) |
| { |
| opcnt++; |
| return Point(pt1.x - pt2.x, pt1.y - pt2.y); |
| } |
| |
| int main() |
| { |
| Point pos1(1, 2); |
| Point pos2(3, 4); |
| PointOP op; |
| ShowPointPos(op.PointAdd(pos1, pos2)); |
| ShowPointPos(op.PointSub(pos1, pos2)); |
| return 0; |
| } |
| |
| void ShowPointPos(const Point& pos) |
| { |
| cout << "x : " << pos.x << endl; |
| cout << "y : " << pos.y << endl; |
| } |