1번 ~ 4번 문제까지 사용될 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 객체에 대해 다음 연산을 하고자 한다.
Book a("청춘", 20000, 300) , b("미래", 30000, 500);
a += 500; // 책 a의 가격 500원 증가
b -= 500; // 책 b의 가격 500원 감소
a.show();
b.show();
(1) +=, -= 연산자 함수를 Book 클래스의 멤버 함수로 구현하라.
#include<iostream>
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 op1);
Book operator-=(int op1);
};
Book Book::operator+=(int op1)
{
price += op1 ;
return *this;
}
Book Book::operator-=(int op1)
{
price += op1;
return *this;
}
int main()
{
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a.show();
a += 500;
a.show();
}
(2) +=, -= 연산자 함수를 Book 클래스 외부 함수로 구현하라.
#include<iostream>
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& b ,int op1);
friend Book operator-=(Book& b, int op1);
};
Book operator+=(Book& b, int op1)
{
b.price += op1;
return b;
}
Book operator-=(Book& b, int op1)
{
b.price += op1;
return b;
}
int main()
{
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a.show();
a += 500;
a.show();
}
Book 객체를 활용하는 사례이다.
Book a("명품 C++" , 30000, 500), b("고품 C++", 30000, 500);
if(a == 30000) cout << "정가 30000원" << endl; // price 비교
if(a == "명품 C++") cout << "명품 C++ 입니다." << endl; // 책 title 비교
if(a == b) cout << "두 책이 같은 책입니다." << endl; // title, price, pages 모두 비교
(1) 세 개의 == 연산자 함수를 가진 Book 클래스를 작성하라.
#include<iostream>
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 op1);
bool operator==(string op2);
bool operator==(Book op2);
};
bool Book::operator==(int op1)
{
if (price == op1)
{
return true;
}
else
{
return false;
}
}
bool Book::operator==(string op1)
{
if (title == op1)
{
return true;
}
else
{
return false;
}
}
bool Book::operator==(Book op2)
{
Book tmp;
tmp.title = this->title;
if (op2.getTitle() == tmp.getTitle())
{
return true;
}
else
{
return false;
}
}
int main()
{
Book a("청춘", 20000, 300), b("미래", 30000, 500);
if (a == 20000)
{
cout << "정가 30000원" << endl;
}
if (a == "청춘")
{
cout << "청춘입니다" << endl;
}
if (a == b)
{
cout << "두 책은 같은 책입니다" << endl;
}
}
(2) 세 개의 == 연산자를 프렌드 함수로 작성하라.
#include<iostream>
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& b ,int op1);
friend bool operator==(Book& b,string op2);
friend bool operator==(Book& b,Book op2);
};
bool operator==(Book& b,int op1)
{
if (b.price == op1)
{
return true;
}
else
{
return false;
}
}
bool operator==(Book& b,string op1)
{
if (b.title == op1)
{
return true;
}
else
{
return false;
}
}
bool operator==(Book& b,Book op2)
{
Book tmp;
tmp.title = b.title;
if (op2.getTitle() == tmp.getTitle())
{
return true;
}
else
{
return false;
}
}
int main()
{
Book a("청춘", 20000, 300), b("미래", 30000, 500);
if (a == 20000)
{
cout << "정가 30000원" << endl;
}
if (a == "청춘")
{
cout << "청춘입니다" << endl;
}
if (a == b)
{
cout << "두 책은 같은 책입니다" << endl;
}
}
다음 연산을 통해 공짜 책인지를 판별하도록 ! 연산자를 작성하라.
Book book("벼룩시장" , 0 , 50); // 가격은 0
if(!book) cout << "공짜다" << endl;
#include<iostream>
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 !();
};
bool Book::operator!()
{
Book tmp;
tmp.price = this->price;
if (price == 0)
{
return true;
}
else
{
return false;
}
}
int main()
{
Book a("청춘", 0, 300);
if (!a)
{
cout << "공짜다";
}
}
다음 연산을 통해 책의 제목을 사전 순으로 비교하고자 한다.
< 연산자를 작성하라.
int main() {
Book a("청춘" , 2000 , 300);
string b;
cout << "책 이름을 입력하세요>>";
getline(cin, b); // 키보드로부터 문자열로 책 이름을 입력 받음
if(b < a)
cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}
#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 s,Book b);
};
bool operator<(string s, Book b)
{
if (s < b.title)
{
return true;
}
}
int main() {
Book a("청춘", 20000, 300);
string b;
cout << "책 이름을 입력하세요>>";
getline(cin, b);
if( b < a )
cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}
다음 main()에서 Color 클래스는 3요소(빨강, 초록, 파랑)로 하나의 색을 나타내는 클래스이다(4장 실습 문제 1번 참고).
+연산자로 색을 더하고, == 연산자로 색을 비교하고자 한다.
실행 결과를 참고하여 Color 클래스와 연산자, 그리고 프로그램을 완성하라.
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 << "보라색 아님";
}
(1) +와 == 연산자를 Color 클래스의 멤버 함수로 구현하라.
#include<iostream>
using namespace std;
class Color
{
int r, g, b;
public:
Color(int r = 0, int g = 0, int b = 0)
{
this->r = r;
this->g = g;
this->b = b;
};
void show();
Color operator+(Color op1);
bool operator==(Color op1);
};
Color Color::operator+(Color op1)
{
Color tmp;
tmp.r = this->r + op1.r;
tmp.g = this->g + op1.g;
tmp.b = this->b + op1.b;
return tmp;
}
bool Color::operator==(Color op1)
{
Color tmp;
tmp.r = this->r;
tmp.g = this->g;
tmp.b = this->b;
if (tmp.r == op1.r && tmp.g == op1.g && tmp.b == op1.b)
return true;
else
return false;
}
void Color::show()
{
cout << r << ' ' << g << ' ' << b << endl;
}
int main()
{
Color red(255, 0, 0), blue(0, 0, 255), c;
c = red + blue;
c.show();
Color funchia(255, 0, 255);
if (c == funchia)
cout << "보라색 맞음";
else
cout << "보라색 아님";
}
(2) +와 == 연산자를 Color 클래스의 프렌드 함수로 구현하라.
#include<iostream>
using namespace std;
class Color
{
int r, g, b;
public:
Color(int r = 0, int g = 0, int b = 0)
{
this->r = r;
this->g = g;
this->b = b;
};
void show();
friend Color operator+(Color& col ,Color op1);
friend bool operator==(Color& col ,Color op1);
};
Color operator+(Color& col,Color op1)
{
Color tmp;
tmp.r = col.r + op1.r;
tmp.g = col.g + op1.g;
tmp.b = col.b + op1.b;
return tmp;
}
bool operator==(Color& col, Color op1)
{
if (col.r == op1.r && col.g == op1.g && col.b == op1.b)
return true;
else
return false;
}
void Color::show()
{
cout << r << ' ' << g << ' ' << b << endl;
}
int main()
{
Color red(255, 0, 0), blue(0, 0, 255), c;
c = red + blue;
c.show();
Color funchia(255, 0, 255);
if (c == funchia)
cout << "보라색 맞음";
else
cout << "보라색 아님";
}
2차원 행렬을 추상화한 Matrix 클래스를 작성하고, show() 멤버 함수와 다음 연산이 가능하도록 연산자를 모두 구현하라.
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;
(1) 연산자 함수를 Matrix의 멤버 함수로 구현하라.
#include<iostream>
using namespace std;
class Martix
{
int a, b, c, d;
public:
Martix(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 << "Martix = { " << a << " " << b << " " << c << " " << d << "}" << endl;
};
Martix operator+(Martix op1);
Martix operator+=(Martix op1);
bool operator==(Martix op1);
};
Martix Martix::operator+(Martix op1)
{
Martix tmp;
tmp.a = this->a + op1.a;
tmp.b = this->b + op1.b;
tmp.c = this->c + op1.c;
tmp.d = this->d + op1.d;
return tmp;
}
Martix Martix::operator+=(Martix op1)
{
Martix tmp;
this->a = this->a + op1.a;
this->b = this->b + op1.b;
this->c = this->c + op1.c;
this->d = this->d + op1.d;
tmp.a = this->a;
tmp.b = this->b;
tmp.c = this->c;
tmp.d = this->d;
return tmp;
}
bool Martix::operator==(Martix op1)
{
if (this->a == op1.a && this->b == op1.b && this->c == op1.c && this->d == op1.d)
{
return true;
}
else
{
return false;
}
}
int main()
{
Martix 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) 연산자 함수를 Matrix의 프렌드 함수로 구현하라.
#include<iostream>
using namespace std;
class Martix
{
int a, b, c, d;
public:
Martix(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 << "Martix = { " << a << " " << b << " " << c << " " << d << "}" << endl;
};
friend Martix operator+(Martix& mart ,Martix op1);
friend Martix operator+=(Martix& mart ,Martix op1);
friend bool operator==(Martix& mart ,Martix op1);
};
Martix operator+(Martix& mart, Martix op1)
{
Martix tmp;
tmp.a = mart.a + op1.a;
tmp.b = mart.b + op1.b;
tmp.c = mart.c + op1.c;
tmp.d = mart.d + op1.d;
return tmp;
}
Martix operator+=(Martix& mart, Martix op1)
{
Martix tmp;
mart.a += op1.a;
mart.b += op1.b;
mart.c += op1.c;
mart.d += op1.d;
tmp.a = mart.a;
tmp.b = mart.b;
tmp.c = mart.c;
tmp.d = mart.d;
return tmp;
}
bool operator==(Martix& mart ,Martix op1)
{
if (mart.a == op1.a && mart.b == op1.b && mart.c == op1.c && mart.d == op1.d)
{
return true;
}
else
{
return false;
}
}
int main()
{
Martix 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-6은 Array를 사용할 것 같다. 너무 무지성으로 한듯?
2차원 행렬을 추상화한 Matrix 클래스를 활용하는 다음 코드가 있다.
Matrix a(4,3,2,1), b;
int x[4], y[4]={1,2,3,4}; // 2차원 행렬의 4 개의 원소 값
a >> x; // a의 각 원소를 배열 x에 복사. x[]는 {4,3,2,1}
b << y; // 배열 y의 원소 값을 b의 각 원소에 설정
for(int i=0; i<4; i++) cout << x[i] << ' '; // x[] 출력
cout << endl;
b.show();
(1) <<, >> 연산자 함수를 Matrix의 멤버 함수로 구현하라.
#include<iostream>
using namespace std;
class Martix
{
int a[4];
public:
Martix(int a = 0, int b = 0, int c = 0, int d = 0)
{
this->a[0] = a;
this->a[1] = b;
this->a[2] = c;
this->a[3] = d;
};
void show()
{
cout << "Martix = { " << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << "}" << endl;
};
friend void operator>>(Martix& martix ,int op1[]);
friend Martix operator<<(Martix&,int op1[]);
};
void operator>>(Martix& martix,int op1[])
{
int num = 0;
for (int i = 0; i < 4; i++)
{
op1[i] = martix.a[i];
}
}
Martix operator<<(Martix& martix,int op1[])
{
for (int i = 0; i < 4; i++)
{
martix.a[i] = op1[i];
}
return martix;
}
int main()
{
Martix a(1, 2, 3, 4), b;
int x[4];
int y[4] = { 1, 2, 3, 4 };
a >> x; //a의 각 원소를 배열 x에 복사, x[]는 { 4, 3, 2, 1}
b << y; //배열 y의 원소 값을 b의 각 원소에 설정
for (int i = 0; i < 4; i++)
{
cout << x[i] << ' ';
}
cout << endl;
b.show();
}
(2) <<, >> 연산자 함수를 Matrix의 프렌드 함수로 구현하라.
#include<iostream>
using namespace std;
class Martix
{
int a[4];
public:
Martix(int a = 0, int b = 0, int c = 0, int d = 0)
{
this->a[0] = a;
this->a[1] = b;
this->a[2] = c;
this->a[3] = d;
};
void show()
{
cout << "Martix = { " << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << "}" << endl;
};
friend void operator>>(Martix& martix ,int op1[]);
friend Martix operator<<(Martix&,int op1[]);
};
void operator>>(Martix& martix,int op1[])
{
int num = 0;
for (int i = 0; i < 4; i++)
{
op1[i] = martix.a[i];
}
}
Martix operator<<(Martix& martix,int op1[])
{
for (int i = 0; i < 4; i++)
{
martix.a[i] = op1[i];
}
return martix;
}
int main()
{
Martix a(1, 2, 3, 4), b;
int x[4];
int y[4] = { 1, 2, 3, 4 };
a >> x; //a의 각 원소를 배열 x에 복사, x[]는 { 4, 3, 2, 1}
b << y; //배열 y의 원소 값을 b의 각 원소에 설정
for (int i = 0; i < 4; i++)
{
cout << x[i] << ' ';
}
cout << endl;
b.show();
}
원을 추상화한 Circle 클래스는 간단히 아래와 같다.
class Circle{
int radius;
public:
Circle(int radius=0) { this->radius = radius; }
void show() { cout << "radius = " << radius << " 인 원" << endl; }
};
다음 연산이 가능하도록 연산자를 프렌드 함수로 작성하라.
Circle a(5), b(4);
++a; // 반지름을 1 증가 시킨다.
b = a++; // 반지름을 1 증가 시킨다.
a.show();
b.show();
#include<iostream>
using namespace std;
class Circle
{
int r;
public:
Circle(int r = 0) {
this->r = r;
}
void show() {
cout << "r = " << r << " 인 원" << endl;
}
Circle operator++(); //전위++
Circle operator++(int x); //후위++
};
Circle Circle::operator++()
{
this->r++;
return *this;
}
Circle Circle::operator++(int x)
{
Circle tmp = *this;
this->r++;
return tmp;
}
int main()
{
Circle a(5), b(4);
++a;
b = a++;
a.show();
b.show();
}
문제 8번의 Circle 객체에 대해 다음 연산이 가능하도록 연산자를 구현하라.
Circle a(5), b(4);
b = 1+a; // b의 반지름을 a의 반지름에 1을 더한 것으로 변경
a.show();
b.show()
```;
```c
#include<iostream>
using namespace std;
class Circle
{
int r;
public:
Circle(int r = 0) {
this->r = r;
}
void show() {
cout << "r = " << r << " 인 원" << endl;
}
Circle operator+(int r); //전위++
};
Circle Circle::operator+(int r)
{
Circle cir;
cir.r = this->r + r;
return cir;
}
int main()
{
Circle a(5), b(4);
b = a + 1;
a.show();
b.show();
}
통계를 내는 Statistics 클래스를 만들려고 한다.
데이터는 Statistics 클래스 내부에 int 배열을 동적으로 할당받아 유지한다.
다음과 같은 연산이 잘 이루어지도록 Statistics 클래스와 !, >>, <<, ~ 연산자 함수를 작성하라.
int main() {
Statistics stat;
if(!stat) cout << "현재 통계 데이타가 없습니다." << endl;
int x[5];
cout << "5 개의 정수를 입력하라>>";
for(int i=0; i<5; i++) cin >> x[i]; // x[i]에 정수 입력
for(int i=0; i<5; i++) stat << x[i]; // x[i] 값을 통계 객체에 삽입한다.
stat << 100 << 200; // 100, 200을 통계 객체에 삽입한다.
~stat; // 통계 데이터를 모두 출력한다.
int avg;
stat >> avg; // 통계 객체로부터 평균을 받는다.
cout << "avg=" << avg << endl; // 평균을 출력한다.
}
#include<iostream>
using namespace std;
class Statstics
{
int* num;
int size;
public:
Statstics(int size = 0)
{
this->size = size;
num = new int[size];
};
bool operator!();
void operator~();
Statstics operator<<(int x);
void operator>> (int& avg);
};
bool Statstics::operator!()
{
if (num == NULL)
return false;
else
return true;
}
void Statstics::operator~()
{
for (int i = 0; i < size + 1; i++)
{
cout << num[i] << ' ';
}
cout << endl;
}
Statstics Statstics::operator<<(int x)
{
this->num[size] = x;
this->size++;
return *this;
}
void Statstics::operator>> (int& avg)
{
avg = 0;
for (int i = 0; i < size + 1; i++)
avg += num[i];
avg /= (size + 1);
}
int main()
{
Statstics 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; //100, 200을 통계 객체에 삽입한다.
~stat; //통계 데이터를 모두 출력한다.
int avg;
stat >> avg; //객체로부터 평균을 받는다.
cout << "avg=" << avg << endl;
}
'stat >> avg' 를 못풀어 솔루션을 보고 이해했다. 솔루션이 쉬웠어가지고 조금 더 고민해봤을 걸이라는 생각이 든다.