๐ŸŒˆ๋ช…ํ’ˆ C++ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 7์žฅ๐Ÿง‘โ€๐Ÿ’ป

Se0ng_1lยท2022๋…„ 6์›” 23์ผ
0

๋ช…ํ’ˆCPPํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
7/10
post-thumbnail

7์žฅ ์š”์ 

ํ”„๋ Œ๋“œ์™€ ์—ฐ์‚ฐ์ž ์ค‘๋ณต

ํ”„๋ Œ๋“œ

ํ”„๋ Œ๋“œ ํ•จ์ˆ˜

private์ด๋‚˜ protected์ธ ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด friendํ‚ค์›Œ๋“œ๋ฅด ์„ ์–ธํ•˜์—ฌ ๋งŒ๋“  ์™ธ๋ถ€ ํ•จ์ˆ˜

class Shape{
    friend void foo(); // ํ”„๋ Œ๋“œ ํ•จ์ˆ˜ ์„ ์–ธ
};
void foo(){ } // ์™ธ๋ถ€ํ•จ์ˆ˜, Shapeํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„ ์ ‘๊ทผ ๊ฐ€๋Šฅ

ํด๋ž˜์Šค์™€ ํ”„๋ Œ๋“œ ํ•จ์ˆ˜

ํด๋ž˜์Šค ๋ฉค๋ฒ„ํ•จ์ˆ˜๋„ ํ”„๋ Œ๋“œ ํ•จ์ˆ˜๋กœ ์„ ์–ธํ•˜์—ฌ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๋‹จ, public์œผ๋กœ ์„ ์–ธ์„ ํ•ด์•ผ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

class Shape{
public:
    void setName(){}
};
class Color{
    friend void Shape::setName();
};

ํ”„๋ Œ๋“œ ํด๋ž˜์Šค

ํด๋ž˜์Šค๋ฅผ ํ”„๋ Œ๋“œ๋กœ ์„ ์–ธํ•˜๋ฉด ์„ ์–ธ๋œ ํด๋ž˜์Šค๋Š” ์„ ์–ธํ•œ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„์— ์ž์œ ๋กญ๊ฒŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

#include <iostream>
using namespace std;

class Color;

class Shape{
    string name;
    string length;
public:
    friend Color; // ํ”„๋ Œ๋“œ ํด๋ž˜์Šค ์„ ์–ธ
    Shape(){
        name = "๋„ค๋ชจ";
    }
    void setName(string name){
        this->name = name;
    }
};

class Color{
    string color;
public:
    Color(){color = "๋นจ๊ฐ•";}
    void setColor(string color){
        this->color = color;
    }

    void print(Shape s){ 
        cout << color << "์ƒ‰์˜ " << s.name << "์ž…๋‹ˆ๋‹ค." << endl;
    }
    // Shapeํด๋ž˜์Šค์—์„œ Colorํด๋ž˜์Šค๋ฅผ friend๋กœ ์„ ์–ธํ•˜์—ฌ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ชจ์Šต
};

int main()
{
    Shape nemo;
    nemo.setName("์„ธ๋ชจ");
    Color white;
    white.setColor("ํ•˜์–‘");
    white.print(nemo);
    // ์ถœ๋ ฅ : ํ•˜์–‘์ƒ‰์˜ ์„ธ๋ชจ์ž…๋‹ˆ๋‹ค.
}

ย 

์—ฐ์‚ฐ์ž ์ค‘๋ณต

์—ฐ์‚ฐ์ž ์ค‘๋ณต(๋‹คํ˜•์„ฑ)

์˜ˆ๋ฅผ ๋“ค์–ด '+' ๊ฐ€ ๋‹จ์ˆœ ์‚ฌ์น™์—ฐ์‚ฐ์ด ์•„๋‹Œ
Magenta + Cyan + Yellow = Black
์ด ๋˜๋„๋ก ์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ

ํŠน์ง•
1. C++์–ธ์–ด์— ๋ณธ๋ž˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž๋งŒ ์ค‘๋ณต ๊ฐ€๋Šฅ
2. ํ”ผ์—ฐ์‚ฐ์ž์˜ ํƒ€์ž…์ด ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ์ƒˆ๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ
3. ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค.
4. ํด๋ž˜์Šค์™€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„๋‹ค.
5. ํ”ผ์—ฐ์‚ฐ์ž์˜ ๊ฐœ์ˆ˜๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.
6. ์—ฐ์‚ฐ์˜ ์šฐ์„ ์ˆœ์œ„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
7. ๋ชจ๋“  ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฐ€๋Šฅํ•œ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

์„ ์–ธ ๋ฐ ์ •์˜
1. ์™ธ๋ถ€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ํด๋ž˜์Šค์˜ ํ”„๋ Œ๋“œํ•จ์ˆ˜๋กœ ์„ ์–ธ

Shape operator + (Shape op1, Shape op2);
bool operator == (Shape op1, Shape op2);
class Shape{
	friend Shape operator + (Shape op1, Shape op2);
    friend bool operator == (Shape op1, Shape op2);
}
  1. ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„
class Shape{ // op1์€ ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ ์ด ๋˜๋ฏ€๋กœ this๋กœ ์ ‘๊ทผํ•œ๋‹ค.
	Shape operator + (Shape op2);
    bool operator == (Shape op2);
}

ย 
๊ฐ์ฒด ์ž์‹ ์„ ๋ฆฌํ„ด์‹œ์ผœ์•ผ ํ•˜๋Š” ์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ ex '+='
๊ฐ์ฒด ์ž์‹ ์˜ ์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ด์‹œํ‚จ๋‹ค.

Shape& Shape::operator+=(Shape op2){
	length = length + op2.length;
    return *this;
}

ย 

๋‹จํ•ญ์—ฐ์‚ฐ์ž

๋‹จํ•ญ์—ฐ์‚ฐ์ž๋Š” ๊ฐ์ฒด ์ž์‹ ์˜ ์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ด์‹œํ‚จ๋‹ค.
์ „์œ„++ ์—ฐ์‚ฐ์ž์™€ ํ›„์œ„++์—ฐ์‚ฐ์ž์ฐจ์ด์ 
์ „์œ„๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”์—†์ง€๋งŒ ํ›„์œ„๋Š” ํ•„์š”ํ•˜๋‹ค.

// ์ „์œ„์—ฐ์‚ฐ์ž
Shape& Shape::operator++(){
	length++;
    return *this;
}

ํ›„์œ„์—ฐ์‚ฐ์ž๋Š” ์šฐ์„  ์ž์‹ ์„ ๋ณต์‚ฌํ•ด ์ €์žฅํ•œ ํ›„ ์ž์‹ ์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ณต์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

temp(ํ›„์œ„์—ฐ์‚ฐ ์ „ ์ž์‹ )๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋‚˜๋ฉด ์ดํ›„์˜ ๊ฐ์ฒด๋Š” ํ›„์œ„์—ฐ์‚ฐ์ด ์ง„ํ–‰๋œ ๊ฐ’์„ ์ง€๋‹Œ๋‹ค.

// ํ›„์œ„์—ฐ์‚ฐ์ž
// ์ด๋•Œ a๋Š” ๋‹จ์ˆœํžˆ ํ›„์œ„์—ฐ์‚ฐ์ž„์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์˜๋ฏธ๋Š” ์—†๋‹ค.
Shape Shape::operator++(int a){
	Shape temp = *this;
	length++;
    return temp; // ๋ณต์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
}

ย 

์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” '<<' ์—ฐ์‚ฐ์ž

๋งŒ์•ฝ ๊ฐ์ฒด a, b, c๋ฅผ ๋”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด
a = a + b;
a = a + c;
๋ฅผ ํ•ด์•ผํ•œ๋‹ค.
ํ•˜์ง€๋งŒ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋” ๊ฐ„๋‹จํžˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Shape& Shape::operator << (Shape a){
	length += a.length;
    return *this; // ๋ณต์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
}
int main(){ a << b << c; 
// a.length๊ฐ’์€ ๋ชจ๋“  length๋ฅผ ๋”ํ•œ ๊ฐ’์ด ๋œ๋‹ค.
}
#include <iostream>
using namespace std;
class Shape{
    int length;
public:
    Shape(int a) : length(a){}
    Shape& operator << (Shape a);
    void getLength(){
        cout << length;
    }
};

Shape &Shape::operator<<(Shape a) {
    length += a.length;
    return *this;
}
int main()
{
    Shape a(4), b(5), c(7);
    a << b << c;
    a.getLength(); // ์ถœ๋ ฅ : 16
}

๋ช…ํ’ˆ C++ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฑ…
https://book.naver.com/bookdb/book_detail.naver?bid=13395206

โš ๏ธ ์ฃผ์˜ โš ๏ธ
1. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์‹ค์Šต๋ฌธ์ œ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
2.์ œ๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ํ‹€๋ฆฐ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํžˆ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
3. C++๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ํ•™์ƒ์ด์‹œ๋ผ๋ฉด ๋ณด์‹œ๊ธฐ ์ „์— ์ง์ ‘ ๊ผญ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์„ธ์š”!

์‹ค์Šต ๋ฌธ์ œ 7 - 1, 2, 3, 4
๋ฌธ์ œ : 1. 1 +=, -=์—ฐ์‚ฐ์ž๋ฅผ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

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

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 a);
    Book& operator -= (int a);
};

Book& Book::operator += (int a){
    price += a;
    return *this;
}
Book& Book::operator -= (int a){
    price -= a;
    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{
    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 a);
    friend Book& operator -= (Book &op1, int a);

};

Book& operator += (Book &op1, int a){
    op1.price += a;
    return op1;
}
Book& operator -= (Book &op1, int a){
    op1.price -= a;
    return op1;
}
int main()
{
    Book a("์ฒญ์ถ˜", 20000, 300), b("๋ฏธ๋ž˜", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

ย 

๋ฌธ์ œ : 2. 1 ์„ธ๊ฐœ์˜ == ์—ฐ์‚ฐ์ž ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง„ Bookํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ

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

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) {
    return this->price == price;
}
bool Book::operator==(string title) {
    return this->title == title;
}
bool Book::operator==(Book op2) {
    if(this->title == op2.title && this->price == op2.price && this->pages == op2.pages)
        return true;
    return false;
}

int main()
{
    Book a("๋ช…ํ’ˆ C++", 30000, 300), 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{
    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) {
    return op1.price == price;
}
bool operator==(Book op1, string title) {
    return op1.title == title;
}
bool operator==(Book op1, Book op2) {
    if(op1.title == op2.title && op1.price == op2.price && op1.pages == op2.pages)
        return true;
    return false;
}

int main()
{
    Book a("๋ช…ํ’ˆ C++", 30000, 300), 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{
    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 ! (){
        if(this->price > 0)
            return false;
        return true;
    }
};

int main()
{
    Book book("๋ฒผ๋ฃฉ์‹œ์žฅ", 0, 50);
    if(!book) cout << "๊ณต์งœ๋‹ค" << endl;
}

ย 

๋ฌธ์ œ : 4 < ์—ฐ์‚ฐ์ž ์ด์šฉํ•˜์—ฌ ์ฑ…์˜ ์ œ๋ชฉ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋น„๊ตํ•˜๊ธฐ

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

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 title, Book op2);
};
bool operator < (string title, Book op2) {
    if(title < op2.title )
        return true;
    return false;
}

int main()
{
    Book a("์ฒญ์ถ˜", 20000, 300);
    string b;
    cout << "์ฑ… ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”>>";
    getline(cin, b);
    if(b < a)
        cout << a.getTitle() << "์ด " << b << "๋ณด๋‹ค ๋’ค์— ์žˆ๊ตฌ๋‚˜!" << endl;
}

ย 
์‹ค์Šต ๋ฌธ์ œ 7 - 5
๋ฌธ์ œ : Colorํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์ƒ‰ ๋”ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Color{
    int red, green, blue;
public:
    Color(){ red = green = blue = 0;}
    Color(int r, int g, int b){ red = r; green = g; blue = b;}
    void setColor(int r, int g, int b) { red = r; green = g; blue = b;}
    void show() {cout << red << ' ' << green << ' ' << blue << endl; }
    Color operator + (Color op2){
        Color tmp;
        tmp.red = this->red + op2.red;
        tmp.green = this->green + op2.green;
        tmp.blue = this->blue + op2.blue;
        return tmp;
    }
    bool operator == (Color op2){
        if(this->red == op2.red && this->green == op2.green && this->blue == op2.blue)
            return true;
        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 << "๋ณด๋ผ์ƒ‰ ์•„๋‹˜";
}

ย 
์‹ค์Šต ๋ฌธ์ œ 7 - 6
๋ฌธ์ œ : Matrixํด๋ž˜์Šค์™€ +=, ==, + ์—ฐ์‚ฐ์ž๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ

(1)

#include <iostream>
using namespace std;

class Matrix{
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0)
    { matrix[0] = a, matrix[1] = b, matrix[2] = c, matrix[3] = d;}
    Matrix operator + (Matrix op2){
        Matrix temp;
        temp.matrix[0] = matrix[0] + op2.matrix[0];
        temp.matrix[1] = matrix[1] + op2.matrix[1];
        temp.matrix[2] = matrix[2] + op2.matrix[2];
        temp.matrix[3] = matrix[3] + op2.matrix[3];
        return temp;
    }
    Matrix& operator += (Matrix op2){
         matrix[0] += op2.matrix[0];
         matrix[1] += op2.matrix[1];
         matrix[2] += op2.matrix[2];
         matrix[3] += op2.matrix[3];
        return *this;
    }
    bool operator == (Matrix op2){
        for(int i = 0; i < 4; i++){
            if(matrix[i] != op2.matrix[i])
                return false;
        }
        return true;
    }
    void show(){
        cout << "Matrix = { ";
        for(int i = 0; i < 4; i++)
        {
            cout << matrix[i] << ' ';
        }
        cout << "}" << endl;
    }
};

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

ย 
(2)

#include <iostream>
using namespace std;

class Matrix{
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0)
    { matrix[0] = a, matrix[1] = b, matrix[2] = c, matrix[3] = d;}
    friend Matrix operator + (Matrix op1, Matrix op2);
    friend Matrix operator += (Matrix &op1, Matrix op2);
    friend bool operator == (Matrix op1, Matrix op2);
    void show(){
        cout << "Matrix = { ";
        for(int i = 0; i < 4; i++)
        {
            cout << matrix[i] << ' ';
        }
        cout << "}" << endl;
    }
};
Matrix operator + (Matrix op1, Matrix op2){
    Matrix temp;
    temp.matrix[0] = op1.matrix[0] + op2.matrix[0];
    temp.matrix[1] = op1.matrix[1] + op2.matrix[1];
    temp.matrix[2] = op1.matrix[2] + op2.matrix[2];
    temp.matrix[3] = op1.matrix[3] + op2.matrix[3];
    return temp;
}
Matrix operator += (Matrix &op1, Matrix op2){
    op1.matrix[0] += op2.matrix[0];
    op1.matrix[1] += op2.matrix[1];
    op1.matrix[2] += op2.matrix[2];
    op1.matrix[3] += op2.matrix[3];
    return op1;
}
bool operator == (Matrix op1, Matrix op2){
    for(int i = 0; i < 4; i++){
        if(op1.matrix[i] != op2.matrix[i])
            return false;
    }
    return true;
}
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 - 7
๋ฌธ์ œ : Matrixํด๋ž˜์Šค์™€ >>, << ์—ฐ์‚ฐ์ž ๊ตฌํ˜„ํ•˜๊ธฐ

(1)

#include <iostream>
using namespace std;

class Matrix{
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0)
    { matrix[0] = a, matrix[1] = b, matrix[2] = c, matrix[3] = d;}
    void show(){
        cout << "Matrix = { ";
        for(int i = 0; i < 4; i++)
        {
            cout << matrix[i] << ' ';
        }
        cout << "}" << endl;
    }
    void operator >> (int *arr);
    void operator << (int *arr);
};
void Matrix::operator >> (int *arr){
    for(int i = 0; i < 4; i++){
        arr[i] = this->matrix[i];
    }
}
void Matrix::operator << (int *arr){
    for(int i = 0; i < 4; i++){
        this->matrix[i] = arr[i];
    }
}

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

ย 
(2)

#include <iostream>
using namespace std;

class Matrix{
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0)
    { matrix[0] = a, matrix[1] = b, matrix[2] = c, matrix[3] = d;}
    void show(){
        cout << "Matrix = { ";
        for(int i = 0; i < 4; i++)
        {
            cout << matrix[i] << ' ';
        }
        cout << "}" << endl;
    }
    friend void operator >> (Matrix op2, int *arr);
    friend void operator << (Matrix &op2, int *arr);
};
void operator >> (Matrix op2, int *arr){
    for(int i = 0; i < 4; i++){
        arr[i] = op2.matrix[i];
    }
}
void operator << (Matrix &op2, int *arr){
    for(int i = 0; i < 4; i++){
        op2.matrix[i] = arr[i];
    }
}

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 - 8
๋ฌธ์ œ : Circleํด๋ž˜์Šค ++(์ „์œ„, ํ›„์œ„)์—ฐ์‚ฐ์ž ๊ตฌํ˜„ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int radius = 0){ this->radius = radius; }
    void show() {cout << "radius = " << radius << "์ธ ์›" << endl;}
    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();
}

์‹ค์Šต ๋ฌธ์ œ 7 - 9
๋ฌธ์ œ : Matrixํด๋ž˜์Šค์™€ + ์—ฐ์‚ฐ์ž ๊ตฌํ˜„ํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int radius = 0){ this->radius = radius; }
    void show() {cout << "radius = " << radius << "์ธ ์›" << endl;}
    friend Circle operator + (int a, Circle op1);
};

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

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

์‹ค์Šต ๋ฌธ์ œ 7 - 10
๋ฌธ์ œ : Statisticsํด๋ž˜์Šค์™€ !, >>, <<, ~์—ฐ์‚ฐ์ž ํ•จ์ˆ˜ ๊ตฌํ˜„

#include <iostream>
using namespace std;

class Statistics{
    int *arr;
    int index;
public:
    Statistics(){
        arr = new int[10];
        index = 0;
    }
    ~Statistics(){
        if(arr)
            delete [] arr;
    }
    bool operator ! ();
    Statistics& operator << (int a);
    void operator >>(int &avg);
    void operator ~();
};

bool Statistics::operator!() {
    return this->arr;
}
Statistics& Statistics::operator << (int a) {
    this->arr[index++] = a;
    return *this;
}
void Statistics::operator>>(int &avg) {
    avg = 0;
    for(int i = 0; i < index; i++){
        avg += arr[i];
    }
    avg = avg / index;
}
void Statistics::operator~() {
    for(int i = 0; i < index; i++){
        cout << arr[i] << ' ';
    }
    cout << endl;
}
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;
}

์‹ค์Šต ๋ฌธ์ œ 7 - 11
๋ฌธ์ œ : Stackํด๋ž˜์Šค ๊ตฌํ˜„ pop = >>, push = <<

#include <iostream>
using namespace std;

class Stack{
    int *stack;
    int tos;
public:
    Stack(){
        stack = new int[10];
        tos = -1;
    }
    ~Stack(){
        if(stack)
            delete [] stack;
    }
    Stack& operator << (int a){
        this->stack[++tos] = a;
        return *this;
    }
    int operator >> (int &a){
        a = this->stack[tos--];
        return a;
    }
    bool operator !(){
        if(stack[tos])
            return false;
        return true;
    }

};

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

์‹ค์Šต ๋ฌธ์ œ 7 - 12
๋ฌธ์ œ : SortedArrayํด๋ž˜์Šค์™€ +, = ์—ฐ์‚ฐ์ž ์™„์„ฑํ•˜๊ธฐ
ํžŒํŠธ์—์„œ ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์“ฐ๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์ •์ž‘ ์•ˆ์“ฐ์ž„.
๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•œ๋‹ค๊ณ ํ•˜์ง€๋งŒ ์ง€์›Œ๋„ ์ •์ƒ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

#include <iostream>
using namespace std;

class SortedArray{
    int size;
    int *p;
    void sort();

public:
    SortedArray();
    SortedArray(SortedArray& src);
    SortedArray(int p[], int size);
    ~SortedArray();
    SortedArray operator + (SortedArray& op2);
    SortedArray& operator = (const SortedArray& op2);
    void show();
};

SortedArray::SortedArray() {
    p = nullptr;
    size = 0;
}
SortedArray::SortedArray(int *p, int size) {
    this->size = size;
    this->p = new int[size];
    for(int i = 0; i < size; i++)
        this->p[i] = p[i];
}

SortedArray::SortedArray(SortedArray &src) {
    cout << "์ง€๊ธˆ ๋“ค์–ด์™”๋‹น" << endl;
    size = src.size;
    p = new int[size];
    for(int i = 0; i < size; i++)
        p[i] = src.p[i];
}
SortedArray::~SortedArray() {
    if(p)
        delete [] p;
}
SortedArray SortedArray::operator+(SortedArray &op2) {
    int tempSize = this->size + op2.size;
    int *tempP = new int[tempSize];
    SortedArray temp(tempP, tempSize);

    for(int i = 0; i < this->size; i++){
        temp.p[i] = this->p[i];
    }
    for(int i = this->size; i < temp.size; i++){
        temp.p[i] = op2.p[i - this->size];
    }

    return temp;
}

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

void SortedArray::show() {
    sort();
    cout << "๋ฐฐ์—ด ์ถœ๋ ฅ : ";
    for(int i = 0; i < size; i++)
        cout << p[i] << ' ';
    cout << endl;
}

void SortedArray::sort() {
    for(int i = 0; i < size - 1; i++)
    {
        for(int j = i + 1; j < size; j++)
        {
            if(p[j] < p[i])
            {
                int temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
}
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();
}
profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€