본문 바로가기

명품 C++

[명품 C++] 7. 프렌드와 연산자 중복

7.1 C++ 프렌드 개념

클래스 외부에 작성된 함수를 클래스 내에 friend 키워드로 선언하여 클래스의 멤버 함수와 동일한 접근 자격을 부여할 수 있다

프렌드 함수는 클래스의 멤버인 것처럼 클래스의 모든 변수나 함수에 접근할 수 있다

프렌드 함수를 선언할 수 있는 경우

- 클래스 외부에 작성된 함수를 프렌드로 선언

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

- 다른 클래스의 모든 멤버 함수를 프렌드로 선언

선언 방법

class Rect{
	....
    friend bool equals (Rect r, Rect s); //equals 를 프렌드 함수로 선언
}

예제 7-1)

#include <iostream>
using namespace std;
class Rect;
bool equals(Rect r, Rect c);

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;
    
}

다른 클래스의 멤버 함수를 클래스의 프랜드 함수로 선언할 수 있다

class Rect{
	....
    friend bool Rectmanager::equals (Rect r, Rect s); 
    //Rectmanager의 equals를 프렌드 함수로 선언
}

예제 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;
}

다른 클래스의 모든 멤버 함수를 프렌드 함수로 한 번에 선언할 수 있다

class Rect{
	....
    friend Rectmanager; 
    //Rectmanager를 프렌드 함수로 선언
}

예제 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;
}
void RectManager::copy(Rect& dest, Rect& src) {
	dest.width = src.width, dest.height = src.height;
}

int main() {
	Rect a(3, 4), b(5, 6);
	RectManager man;

	man.copy(a, b);
	if (man.equals(a, b)) cout << "equal" << endl;
	else cout << "not equal" << endl;
}

 

 

 

7.2 연산자 중복

피연산자에 따라 다른 연산을 하도록 동일한 연산자를 중복해서 작성하는 것이 연산자 중복

1. 두 개의 정수 더하기

3+5

2. 두 개의 문자열 합치기

"C" + "++" = C++

3. 두 색을 섞은 새로운 색 만들기

4. 두 개의 배열 더하기

연산자 중복의 특징

- c++ 언어에 본래 있는 연산자만 중복 가능 -> 새로운 연산자를 만들어 낼 순 없음

- 연산자 중복은 피연산자의 타입이 다른 연산을 새로 정의하는 것

- 연산자 중복이란 새로운 연산 처리를 수행하는 함수를 구현하는 것 -> 연산자 함수

- 연산자 중복은 반드시 클래스와 관계를 가짐 -> 클래스의 멤버 함수로 구현 or 전역 함수로 구현 후 프렌드 선언

- 연산자 중복으로 피연산자의 개수를 바꿀 수 없음

- 연산자 중복으로 연산의 우선순위를 바꿀 수 없음

- 모든 연산자가 중복 가능한 것은 아님

연산자 함수 선언

연산자 중복은 연산자 함수를 통해 구현됨

리턴타입 operator 연산자(매개변수리스트);

- 클래스의 멤버 함수로 구현

class Color {
	Color operator + (Color op2); //왼쪽 피연산자는 객체 자신
	bool operator == (Color op2); //왼쪽 피연산자는 객체 자신
};

- 외부 함수로 구현하고 클래스의 프렌드 함수로 선언

Color operator + (Color op1, Color op2);
bool operator == (Color op1, Color op2);

class Color {
	friend Color operator + (Color op1, Color op2);
	friend bool operator == (Color op1, Color op2);
};

 

 

 

7.3 이항 연산자 중복

+연산자 중복

#include <iostream>
using namespace std;

class Power {
	int kick;
	int power;
public:
	Power(int kick = 0, int punch = 0) {
		this->kick = kick;
		this->power = power;
	}
    show();
};
void Power::show() {
	cout << "kick = " << kick << ", punch = " << punch << endl;
}
Power Power::operator + (Power op2) {
	Power temp;
	temp.kick = this->kick + op2.kick;
	temp.power = this->power + op2.power;
	return temp;
}

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

== 연산자 중복

연산자 착안 : 두 개의 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 (this->kick == op2.kick && this->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;
}

+= 연산자 중복

+= 연산자의 리턴 타입은 &Power로 해야한다

this를 사용해서 결과를 리턴하게 된다

#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) {
	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();
}

+ 연산자 작성 실습 : b = a + 2;

예제 7-7

#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 temp;
	temp.kick = this->kick + op2;
	temp.punch = this->punch + op2;
	return temp;
}

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

 

 

 

7.4 단항 연산자 중복

단항 연산자는 연산자의 위치에 따라 전위 연산자와 후위 연산자로 나뉜다

전위 연산자 : !op, ~op, ++op, --op

후위 연산자 : op++, op--

전위 ++ 연산자 중복

자신에 대한 참조를 리턴한다

Power &Power::operator++(//매개변수 없음){
	kick++;
    punch++;
    return this;
}

예제 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)

#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;
}
bool Power::operator ! () {
	if (kick == 0 && punch == 0)return true;
	else return false;
}

int main() {
	Power a(0, 0), 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;
}

후위 ++ 연산자 중복

매개 변수를 가지도록 선언된다

Power Power::operator++(int x){
	Power tmp = *this // 증가 이전 객체 상태 저장
    kick++;
    punch++;
    return tmp;
}

예제 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 temp = *this;
	kick++;
	punch++;
	return temp;
}

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

 

 

 

7.5 프렌드를 이용한 연산자 중복

연산자 함수는 클래스 바깥의 외부 전역 함수로도 작성 가능하다

+연산자를 외부 함수로 작성

2가 객체가 아니므로 컴파일러는

+ (2, a) 형식으로 변환함

class Power {
	int kick;
	int punch;
public:
	...
	friend Power operator + (int op1, Power op2);
};
Power operator + (int op1, Power op2) { //외부 함수로 구현된 연산자 함수
	...
}

예제 7-11)

#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 temp;
	temp.kick = op1 + op2.kick;
	temp.punch = op1 + op2.punch;
	return temp;
}

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

+ 연산자를 외부 프렌드 함수로 작성

예제 7-12)

#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 temp;
	temp.kick = op1.kick + op2.kick;
	temp.punch = op1.punch + op2.punch;
	return temp;
}

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

단항 연산자 ++를 프렌드로 작성하기

두 연산자 함수는 모두 참조 매개 변수 Power &op 를 사용, 전위 연산자의 경우 참조를 리턴

Power& operator ++ (Power& op);
Power operator ++(Power& op, int x);

예제 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 temp = op;
	op.kick++;
	op.punch++;
	return temp;
}

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

	b = a++;
	a.show(); 
    b.show();
}

 

 

 

7.6 참조를 리턴하는 << 연산자 작성 실습

객체 a의 kick과 punch에 각각 3을 더하고 다시 5, 6을 연속적으로 더하는 연산

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

예제 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();
}

 

 

 

실습문제

1-1)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    Book& operator += (int op2);
    Book& operator -= (int op2);
};

Book& Book::operator+= (int op2){
    price = price + op2;
    return *this;
}
Book& Book::operator-= (int op2){
    price = price - op2;
    return *this;
}

int main(){
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

1-2)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    friend Book& operator += (Book& op1, int op2);
    friend Book& operator -= (Book& op1, int op2);
};

Book& operator+= (Book& op1, int op2){
    op1.price = op1.price + op2;
    return op1;
}
Book& operator-= (Book& op1, int op2){
    op1.price = op1.price - op2;
    return op1;
}

int main(){
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

2-1)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    bool operator == (int price);
    bool operator == (string title);
    bool operator == (Book op2);
};

bool Book::operator == (int price){
    if (this->price == price) return true;
    else return false;
}

bool Book::operator == (string title){
    if (this->title == title) return true;
    else return false;
}

bool Book::operator == (Book op2){
    if (this->title == op2.title && this->price == op2.price && this->pages == op2.pages) return true;
    else return false;
}

int main(){
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

2-2)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    friend bool operator == (Book op1, int price);
    friend bool operator == (Book op1,string title);
    friend bool operator == (Book op1, Book op2);
};

bool operator == (Book op1, int price){
    if (op1.price == price) return true;
    else return false;
}

bool operator == (Book op1, string title){
    if (op1.title == title) return true;
    else return false;
}

bool operator == (Book op1, Book op2){
    if (op1.title == op2.title && op1.title == op2.price && op1.title == op2.pages) return true;
    else return false;
}

int main(){
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

3)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    bool operator ! ();
};

bool Book::operator!(){
    if (price == 0) return true;
    else return false;
}


int main(){
    Book book("벼룩시장", 0, 50);
    if (!book) cout << "공짜다" << endl;
}

4)

#include <iostream>
#include <string>
using namespace std;
class Book;

class Book{
    string title;
    int price, pages;
public:
    Book(string title="", int price=0, int pages=0){
        this->title=title; this->price=price; this->pages=pages;
    }
    void show(){
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle(){
        return title;
    }
    friend bool operator < (string op1, Book op2);
};

bool operator<(string op1, Book op2){
    if (op1 < op2.title) return true;
    else return false;
}


int main(){
    Book a("청춘", 20000, 300);
    string b;
    cout << "책 이름을 입력하세요>>";
    getline(cin, b);
    if (b < a)
        cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}

5)

#include <iostream>
using namespace std;
class Color;

class Color{
    int red, green, blue;
public:
    Color(int red=0, int green=255, int blue=0){
        this->red=red; this->green = green; this->blue=blue;
    }
    void show(){
        cout << red << ' ' << green << " " << blue << " " << endl;
    }
    
    Color operator + (Color op2);
    bool operator == (Color op2);
};

Color Color::operator+(Color op2){
    Color temp;
    temp.red = this->red + op2.red;
    temp.green = this->green + op2.green;
    temp.blue = this->blue + op2.blue;
    return temp;
}

bool Color::operator==(Color op2){
    if (this->red == op2.red && this->green == op2.green && this->blue == op2.blue) return true;
    else return false;
}

int main(){
    Color red(255, 0, 0), blue(0, 0, 255), c;
    c = red + blue;
    c.show();

    Color fuchsia(255, 0, 255);
    if (c == fuchsia)
        cout << "보라색 맞음";
    else
        cout << "보라색 아님";
}

6-1)

#include <iostream>
using namespace std;
class Matrix;

class Matrix{
    int a,b,c,d;
public:
    Matrix(int a=0, int b=0, int c=0, int d=0){
        this->a=a; this->b=b; this->c=c; this->d=d;
    }
    void show() {
        cout << "Matrix = { " << a << " " << b << " " << c << " " << d << " }" << endl;
    }
    Matrix operator+(Matrix op2);
    Matrix& operator+=(Matrix op2);
    bool operator==(Matrix op2);
};

Matrix Matrix::operator+(Matrix op2){
    Matrix temp;
    temp.a = this->a + op2.a;
    temp.b = this->b + op2.b;
    temp.c = this->c + op2.c;
    temp.d = this->d + op2.d;
    return temp;
}

Matrix& Matrix::operator+=(Matrix op2){
    a = a + op2.a;
    b = b + op2.b;
    c = c + op2.c;
    d = d + op2.d;
    return *this;
}

bool Matrix::operator==(Matrix op2){
    if (a==op2.a&&b==op2.b&&c==op2.c&&d==op2.d) return true;
    else return false;
}

int main(){
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show(); b.show(); c.show();

    if (a == c)
        cout << "a and c are the same" << endl;
}

6-2)

#include <iostream>
using namespace std;
class Matrix;

class Matrix{
    int a,b,c,d;
public:
    Matrix(int a=0, int b=0, int c=0, int d=0){
        this->a=a; this->b=b; this->c=c; this->d=d;
    }
    void show() {
        cout << "Matrix = { " << a << " " << b << " " << c << " " << d << " }" << endl;
    }
    friend Matrix operator+(Matrix op1, Matrix op2);
    friend Matrix& operator+=(Matrix& op1, Matrix op2);
    friend bool operator==(Matrix op1, Matrix op2);
};

Matrix operator+(Matrix op1, Matrix op2){
    Matrix temp;
    temp.a = op1.a + op2.a;
    temp.b = op1.b + op2.b;
    temp.c = op1.c + op2.c;
    temp.d = op1.d + op2.d;
    return temp;
}

Matrix& operator +=(Matrix& op1, Matrix op2){
    op1.a = op1.a + op2.a;
    op1.b = op1.b + op2.b;
    op1.c = op1.c + op2.c;
    op1.d = op1.d + op2.d;
    return op1;
}

bool operator==(Matrix op1, Matrix op2){
    if (op1.a==op2.a&&op1.b==op2.b&&op1.c==op2.c&&op1.d==op2.d) return true;
    else return false;
}

int main(){
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show(); b.show(); c.show();

    if (a == c)
        cout << "a and c are the same" << endl;
}

7-1)

#include <iostream>
using namespace std;
class Matrix;

class Matrix{
    int a,b,c,d;
public:
    Matrix(int a=0, int b=0, int c=0, int d=0){
        this->a=a; this->b=b; this->c=c; this->d=d;
    }
    void show() {
        cout << "Matrix = { " << a << " " << b << " " << c << " " << d << " }" << endl;
    }
    int* operator >> (int x[]);
    Matrix& operator << (int y[]);
};

int* Matrix::operator>>(int x[]){
    x[0] = a;
    x[1] = b;
    x[2] = c;
    x[3] = d;
    return x;
}

Matrix& Matrix::operator<<(int y[]){
    a = y[0];
    b = y[1];
    c = y[2];
    d = y[3];
    return *this;
}


int main(){
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = { 1,2,3,4 };
    a >> x;
    b << y;

    for (int i = 0; i < 4; i++) cout << x[i] << " ";
    cout << endl;
    b.show();
}

7-2)

#include <iostream>
using namespace std;
class Matrix;

class Matrix{
    int a,b,c,d;
public:
    Matrix(int a=0, int b=0, int c=0, int d=0){
        this->a=a; this->b=b; this->c=c; this->d=d;
    }
    void show() {
        cout << "Matrix = { " << a << " " << b << " " << c << " " << d << " }" << endl;
    }
    friend int* operator >> (Matrix op1, int x[]);
    friend Matrix& operator << (Matrix& op1, int y[]);
};

int* operator>>(Matrix op1, int x[]){
    x[0] = op1.a;
    x[1] = op1.b;
    x[2] = op1.c;
    x[3] = op1.d;
    return x;
}

Matrix& operator<<(Matrix& op1, int y[]){
    op1.a = y[0];
    op1.b = y[1];
    op1.c = y[2];
    op1.d = y[3];
    return op1;
}


int main(){
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = { 1,2,3,4 };
    a >> x;
    b << y;

    for (int i = 0; i < 4; i++) cout << x[i] << " ";
    cout << endl;
    b.show();
}

8)

#include <iostream>
using namespace std;
class Matrix;

class Circle{
    int radius;
public:
    Circle(int radius=0){
        this->radius=radius;
    }
    void show() {
        cout << "radius = " << radius << " 인 원" << "\n";
    }
    friend Circle& operator ++ (Circle& op);
    friend Circle operator ++ (Circle& op, int x);
};

Circle& operator ++ (Circle& op){
    op.radius++;
    return op;
}

Circle operator ++ (Circle& op, int x){
    Circle temp = op;
    op.radius++;
    return temp;
}

int main(){
    Circle a(5), b(4);
    ++a;
    b = a++;
    a.show();
    b.show();
}

9)

#include <iostream>
using namespace std;
class Matrix;

class Circle{
    int radius;
public:
    Circle(int radius=0){
        this->radius=radius;
    }
    void show() {
        cout << "radius = " << radius << " 인 원" << "\n";
    }
    friend Circle operator + (int op1, Circle op2);
};

Circle operator + (int op1, Circle op2){
    Circle temp;
    temp.radius = op1+op2.radius;
    return temp;
}

int main(){
    Circle a(5), b(4);
    b = 1+a;
    a.show();
    b.show();
}

10)

#include <iostream>
using namespace std;
class Statistics;

class Statistics{
    int n; int* p;
public:
    Statistics() {
            n = 0;
            p = new int[100];
        }
    bool operator ! () {
        if (n==0) return true;
        else return false;
    }
    Statistics& operator << (int op) {
        p[n++] = op;
        return *this;
    }
    void operator ~ () {
        for (int i = 0; i < n; i++) {
            cout << p[i] << " ";
        }
        cout << endl;
    }
    void operator >> (int& op) {
        op = 0;
        for (int i = 0; i < n; i++) op += p[i];
        op /= n;
    }
};

int main(){
    Statistics stat;
    if (!stat) cout << "현재 통계 데이터가 없습니다." << endl;

    int x[5];
    cout << "5 개의 정수를 입력하라>>";
    for (int i = 0; i < 5; i++) cin >> x[i];

    for (int i = 0; i < 5; i++) stat << x[i];
    stat << 100 << 200;
    ~stat;

    int avg;
    stat >> avg;
    cout << "avg = " << avg << endl;
}

11)

#include <iostream>
using namespace std;
class Stack;

class Stack{
    int top; int* p;
public:
    Stack() {
        top=-1;
        p = new int[100];
    }
    bool operator ! () {
        if (top==-1) return true;
        else return false;
    }
    Stack& operator << (int op) {
        p[++top] = op;
        return *this;
    }
    void operator >> (int& op) {
        op = 0;
        op = p[top--];
    }
};

int main(){
    Stack stack;
    stack << 3 << 5 << 10;
    while (true) {
        if (!stack) break;
        int x;
        stack >> x;
        cout << x << " ";
    }
    cout << endl;
}

12)

#include <iostream>
#include <algorithm>

using namespace std;
class SortedArray{
    int size; int *p; void sort();
public:
    SortedArray(SortedArray& src){
        this->size = src.size;
        this->p = new int[this->size];
        for (int i = 0; i < size; i++) this->p[i] = src.p[i];
    }
    SortedArray(int p[]=NULL, int size=0){
        this->size = size;
        this->p = new int [size];
        for (int i = 0; i < size; i++) this->p[i] = p[i];
    };
    ~SortedArray(){
        if(p) delete []p;
    }
    SortedArray operator + (SortedArray& op2);
    SortedArray& operator = (const SortedArray& op2);
    void show(){
        sort();
        cout << "배열 출력 : ";
        for (int i = 0; i < size; i++) cout << p[i] << " ";
            cout << endl;
    }
};

SortedArray SortedArray::operator+(SortedArray& op2){
    SortedArray temp;
    temp.size = this->size + op2.size;
    temp.p = new int[temp.size];
    for (int i = 0; i < temp.size; i++) {
        if (i < this->size)
            temp.p[i] = this->p[i];
        else
            temp.p[i] = op2.p[i - (temp.size - op2.size)];
        }
        return temp;
}

SortedArray& SortedArray::operator = (const SortedArray& op2) {
    delete[]p;
    this->size = op2.size;
    this->p = new int[this->size];
    for (int i = 0; i < this->size; i++)this->p[i] = op2.p[i];
    return *this;
}

void SortedArray::sort() {
    int tmp;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (p[j] > p[j + 1]) {
                tmp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = tmp;
            }
        }
    }
}

int main(){
    int n[] = { 2,20,6 };
    int m[] = { 10,7,8,30 };
    SortedArray a(n, 3), b(m, 4), c;

    c = a + b;

    a.show();
    b.show();
    c.show();
}