STUDY BLOG

명품 C++ programming Chapter 07 예제 본문

Software/C++

명품 C++ programming Chapter 07 예제

쥬루 2020. 11. 25. 22:22

명품 C++ programming Chapter 07 예제

 

예제 7-1 프랜드 함수 만들기

#include <iostream>
using namespace std;

class Rect;
bool equals(Rect r, Rect s);

class Rect {
	int width, height;
public:
	Rect(int width, int height) { this->width=width; this->height=height; }
    friend bool equals(Rect r, Rect s);
};

bool equals(Rect r, Rect s) {
	if(r.width==s.width && r.height==s.height) return true;
    else return false;
}

int main() {
	Rect a(3,4), b(4,5);
    if(equals(a,b)) cout << "equal" << endl;
    else cout << "not equal" << endl;
}

 

예제 7-2 다른 클래스의 멤버 함수를 프렌드 함수로 선언

#include <iostream>
using namespace std;

class Rect;

class RectManager {
public:
	bool equals(Rect r, Rect s);
};

class Rect {
	int width, height;
public:
	Rect(int width, int height) { this->width = width; this->height = height; }
    friend bool RectManager::equals(Rect r, Rect s);
};

bool RectManager::equals(Rect r, Rect s){
	if(r.width==s.width && r.height==s.height) return true;
    else return false;
}

int main() {
	Rect a(3,4), b(3,4);
    RectManager man;
    
    if(man.equals(a,b)) cout << "equal" << endl;
    else cout << "not equal" << endl;
}

 

예제 7-3 다른 클래스 전체를 프렌드로 선언

#include <iostream>
using namespace std;

class Rect;

class RectManager {
public:
	bool equals(Rect r, Rect s);
   	void copy(Rect& dest, Rect& src);
};

class Rect {
	int width, height;
public:
	Rect(int width, int height) { this->width = width; this->height = height; }
    friend RectManager;
};

bool RectManager::equals(Rect r, Rect s){
	if(r.width==s.width && r.height==s.height) return true;
    else return false;
}

bool RectManager::copy(Rect& dest, Rect& src){
	dest.width = src.width; dest.height = src.height;
}

int main() {
	Rect a(3,4), b(3,4);
    RectManager man;
    
    man.copy;
    if(man.equals(a,b)) cout << "equal" << endl;
    else cout << "not equal" << endl;
}

 

예제 7-4 두 개의 Power 객체를 더하는 + 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0){
    	this->kick = kick; this->punch=punch;
    }
    void show();
    Power operator+(Power op2);
};

void Power::show(){
	cout << "kick=" << kick << ', ' << "punch=" << punch << endl;
}

Power Power::operator+(Power op2){
	Power tmp;
    tmp.kick = this->kick + op2.kick;
    tmp.punch = this->punch + op2.punch;
    return tmp;
}

int main() {
	Power a(3,5), b(4,6), c;
    c=a+b;
    a.show();
    b.show();
    c.show();
}

 

예제 7-5 두 개의 Power 객체를 비교하는 == 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0){
    	this->kick = kick; this->punch=punch;
    }
    void show();
    bool operator==(Power op2);
};

void Power::show(){
	cout << "kick=" << kick << ', ' << "punch=" << punch << endl;
}

bool Power::operator==(Power op2){
	if(kick==op2.kick && punch==op2.punch) return true;
    else return false;
}

int main() {
	Power a(3,5), b(3,5);
    a.show();
    b.show();
    
    if(a==b) cout << "두 파워가 같다." << endl;
    else cout << "두 파워가 같지 않다." << endl;
    
}

 

예제 7-6 두 개의 Power 객체를 더하는 += 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0){
    	this->kick = kick; this->punch=punch;
    }
    void show();
    bool operator+=(Power op2);
};

void Power::show(){
	cout << "kick=" << kick << ', ' << "punch=" << punch << endl;
}

bool Power::operator+=(Power op2){
	kick = kick + op2.kick;
    punch = punch + op2.punch;
    return *this;
}

int main() {
	Power a(3,5), b(4,6), c;
    a.show();
    b.show();
    c = a += b;
    a.show();
    c.show();
}

 

예제 7-7 b = a + 2; 의 + 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0){
    	this->kick = kick; this->punch=punch;
    }
    void show();
    Power operator+ (int op2);
};

void Power::show(){
	cout << "kick=" << kick << ', ' << "punch=" << punch << endl;
}

Power Power::operator+(int op2){
	Power tmp;
    tmp.kick = kick +op2;
    tmp.punch = punch +op2;
    return tmp;
}

int main() {
	Power a(3,5), b;
    a.show();
    b.show();
    b=a+2;
    a.show();
    b.show();
    
}

 

예제 7-8 전위 ++ 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) {
    	this->kick = kick; this->punch = punch;
    }
    void show();
    Power& operator++ ();
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power& Power::operator++() {
	kick++;
    punch++;
    return *this;
}

int main() {
	Power a(3,5), b;
    a.show();
    b.show();
    b = ++a;
    a.show();
    b.show();
}

 

예제 7-9 Power 클래스에 ! 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) {
    	this->kick = kick; this->punch = punch;
    }
    void show();
    bool operator! ();
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power& Power::operator++() {
	if(kick == 0 && punch == 0) return true;
    else return false;
}

int main() {
	Power a(3,5), b(5,5);
    
    if(!a) cout << "a의 파워가 0이다." << endl;
    else cout << "a의 파워가 0이 아니다." << endl;
    
    if(!b) cout << "b의 파워가 0이다." << endl;
    else cout << "b의 파워가 0이 아니다." << endl;
}

 

예제 7-10 후위 ++ 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) 	{
    	this->kick = kick; this->punch = punch;
    }
    void show();
    Power operator++ (int x);
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power Power::operator++(int x) {
	Power tmp = *this;
    kick++;
    punch++;
    return tmp;
}

int main() {
	Power a(3,5), b;
    a.show();
    b.show();
    b = a++;
    a.show();
    b.show();
}

 

예제 7-11 2+a 를 위한 + 연산자 함수를 프렌드로 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) 	{
    	this->kick = kick; this->punch = punch;
    }
    void show();
    friend Power operator+ (int op1, Power op2);
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power operator+(int op1, Power op2) {
	Power tmp;
    tmp.kick = op1 + op2.kick;
    tmp.punch = op1 + op2.punch;
    return tmp;
}

int main() {
	Power a(3,5), b;
    a.show();
    b.show();
    b = 2+a;
    a.show();
    b.show();
}

 

예제 7-12 a+b를 위한 + 연산자 함수를 프렌드로 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) 	{
    	this->kick = kick; this->punch = punch;
    }
    void show();
    friend Power operator+ (Power op1, Power op2);
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power operator+(Power op1, Power op2) {
	Power tmp;
    tmp.kick = op1.kick + op2.kick;
    tmp.punch = op1.punch + op2.punch;
    return tmp;
}

int main() {
	Power a(3,5), b(4,6), c;
    c= a+b
    a.show();
    b.show();
    c.show();
}

 

예제 7-13 ++ 연산자 함수를 프렌드로 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) 	{ this->kick = kick; this->punch = punch; }
    void show();
    friend Power& operator++ (Power& op);
    friend Power operator++ (Power& op, int x);
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power& operator++(Power& op) {
	op.kick++;
    op.punch++;
    return op;
}

Power operator++(Power& op, int x) {
	Power tmp = op;
    op.kick++;
    op.punch++;
    return tmp;
}

int main() {
	Power a(3,5), b;
    b = ++a;
    a.show();
    b.show();
    
    b = a++;
    a.show();
    b.show();
}

 

예제 7-14  참조를 리턴하는 << 연산자 작성

#include <iostream>
using namespace std;

class Power {
	int kick;
    int punch;
public:
	Power(int kick=0, int punch=0) 	{ this->kick = kick; this->punch = punch; }
    void show();
    Power operator <<(int n);
};

void Power::show() {
	cout << "kick = " << kick << ', ' << "punch = " << punch << endl;
}

Power& Power::operator <<(int n) {
	kick += n;
    punch += n;
    return *this;
}

int main() {
	Power a(1,2);
    a << 3 << 5 << 6;
    a.show();
   
}