7-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 op2) {
price += op2;
return *this;
}
Book& operator -=(int op2) {
price -= op2;
return *this;
}
};
int main() {
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a += 500;
b -=500;
a.show();
b.show();
}
(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 price); //참조를 리턴하지 않아도 결과는 같다.
friend Book& operator -=(Book& op1,int price);
};
Book& operator +=(Book& op1,int price) {
op1.price += price;
return op1;
}
Book& operator -=(Book& op1,int price) {
op1.price -= price;
return op1;
}
int main() {
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a += 500;
b -= 500;
a.show();
b.show();
}
7-2
(1)
#include <iostream>
#include <string>
using namespace std;
class Book {
string name;
int price;
int pages;
public:
Book(string name, int price, int pages);
bool operator ==(int price);
bool operator ==(string name);
bool operator ==(Book a);
};
Book::Book(string name, int price, int pages) {
this->name = name;
this->price = price;
this->pages = pages;
}
bool Book::operator ==(int price) {
if (this->price == price)
return true;
else return false;
}
bool Book::operator ==(string name) {
if (this->name.compare(name) == 0)
return true;
else return false;
}
bool Book::operator ==(Book a) {
if (this->name.compare(a.name) == 0 && this->price == a.price && this->pages == a.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)
#include <iostream>
#include <string>
using namespace std;
class Book {
string name;
int price;
int pages;
public:
Book(string name, int price, int pages);
friend bool operator ==(Book a,int price);
friend bool operator ==(Book a,string name);
friend bool operator ==(Book a,Book b);
};
Book::Book(string name, int price, int pages) {
this->name = name;
this->price = price;
this->pages = pages;
}
bool operator ==(Book a,int price) {
if (a.price == price)
return true;
else return false;
}
bool operator ==(Book a,string name) {
if (a.name.compare(name) == 0)
return true;
else return false;
}
bool operator ==(Book a,Book b) {
if (a.name.compare(b.name) == 0 && a.price == b.price && a.pages == b.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;
}
7-3
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int price;
int pages;
public:
Book(string title, int price, int pages);
bool operator !();
};
Book::Book(string title, int price, int pages) {
this->title = title;
this->price = price;
this->pages = pages;
}
bool Book::operator !() {
if (this->price == 0)
return true;
}
int main() {
Book book("벼룩시장",0,50);
if (!book)cout << "공짜다" << endl;
}
7-4
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int price;
int pages;
public:
Book(string title, int price, int pages);
friend bool operator <(string str, Book a);
string getTitle();
};
Book::Book(string title, int price, int pages) {
this->title = title;
this->price = price;
this->pages = pages;
}
string Book::getTitle() {
return title;
}
bool operator <(string str, Book a) {
if (str.compare(a.title)<0)
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;
}
7-5
(1)
#include <iostream>
#include <string>
using namespace std;
class Color {
int red;
int green;
int blue;
public:
Color();
Color(int red, int green, int blue);
Color operator +(Color a);
bool operator ==(Color a);
void show();
};
Color::Color() {
red = 0;
green = 0;
blue = 0;
}
Color::Color(int red, int green, int blue) {
this->red = red;
this->green = green;
this->blue = blue;
}
Color Color::operator +(Color a) {
red = this->red + a.red;
green = this->green + a.green;
blue = this->blue + a.blue;
return *this;
}
bool Color::operator ==(Color a) {
if (red == a.red && green == a.green && blue == a.blue)
return true;
else return false;
}
void Color::show() {
cout << red << ' ' << green << ' ' << blue << endl;
}
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 << "보라색 아님";
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Color {
int red;
int green;
int blue;
public:
Color() : Color(0, 0, 0) {};
Color(int red, int green, int blue);
friend Color operator +(Color a, Color b);
friend bool operator ==(Color a, Color b);
void show();
};
Color::Color(int red, int green, int blue) {
this->red = red;
this->green = green;
this->blue = blue;
}
Color operator +(Color a, Color b) {
Color tmp;
tmp.red = a.red + b.red;
tmp.green = a.green + b.green;
tmp.blue = a.blue + b.blue;
return tmp;
}
bool operator ==(Color a, Color b) {
if (a.red == b.red && a.green == b.green && a.blue == b.blue)
return true;
else return false;
}
void Color::show() {
cout << red << ' ' << green << ' ' << blue << endl;
}
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
#include <iostream>
#include <string>
using namespace std;
class Matrix {
int a;
int b;
int c;
int d;
public:
Matrix(int a, int b, int c, int d);
Matrix() : Matrix(0, 0, 0, 0) {};
void show();
Matrix operator +(Matrix b);
Matrix&operator +=(Matrix b);
bool operator ==(Matrix b);
};
Matrix::Matrix(int a, int b, int c, int d) {
this->a = a;
this->b = b;
this->c = c;
this->d = d;
}
void Matrix::show() {
cout << "Matrix = { " << a << ' ' << b << ' ' << c << ' '<<d <<" }"<<endl;
}
Matrix Matrix::operator +(Matrix b) {
Matrix tmp;
tmp.a = this->a + b.a;
tmp.b = this->b + b.b;
tmp.c = this->c + b.c;
tmp.d = this->d + b.d;
return tmp;
}
Matrix& Matrix::operator+=(Matrix b){
this->a = this->a + b.a;
this->b = this->b + b.b;
this->c = this->c + b.c;
this->d = this->d + b.d;
return *this;
}
bool Matrix::operator ==(Matrix b) {
if (this->a == b.a && this->b == b.b && this->c == b.c && this->d == b.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-7
7-8
Comments (0)