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); }
- ํด๋์ค์ ๋ฉค๋ฒํจ์๋ก ๊ตฌํ
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();
}