[C++]명품 C++ 프로그래밍 실습문제 7장 5번
[C++]명품 C++ programming 실습문제 7장 5번
명품 C++ 프로그래밍실습문제/연습문제 /C++
//5-1
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
// 외부함수
using namespace std;
class Color{
int red;
int green;
int blue;
public:
Color(int red=0, int green=0 ,int blue=0){
this -> red =red;
this -> green =green;
this -> blue =blue;
}
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;
else
return false;
}
void show(){
cout << this->red << " " << this->green << " " << this->blue << endl;
}
};
int main() {
Color red(255,0,0);
Color blue(0,0,255);
Color c;
c = red + blue;
c.show();
Color fuchsia(255,0,255);
if(c == fuchsia)
cout << "보라색 맞음" << endl;
else
cout << "보라색 아님" << endl;
return 0;
}
//5-2
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
//fr함수로 구현
using namespace std;
class Color{
int red;
int green;
int blue;
public:
Color(int red=0, int green=0 ,int blue=0){
this -> red =red;
this -> green =green;
this -> blue =blue;
}
friend Color operator+(Color &op1,Color &op2);
friend bool operator==(Color &op1,Color &op2);
void show(){
cout << this->red << " " << this->green << " " << this->blue << endl;
}
};
bool operator==(Color &op1,Color &op2){
if(op1.red == op2.red && op2.green == op2.green && op1.blue == op2.blue)
return true;
else
return false;
}
Color operator+(Color &op1,Color &op2){
Color tmp;
tmp.red = op1.red + op2.red;
tmp.green = op1.green + op2.green;
tmp.blue = op1.blue + op2.blue;
return tmp;
}
int main() {
Color red(255,0,0);
Color blue(0,0,255);
Color c;
c = red + blue;
c.show();
Color fuchsia(255,0,255);
if(c == fuchsia)
cout << "보라색 맞음" << endl;
else
cout << "보라색 아님" << endl;
return 0;
}
'C++' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 실습문제 7장 7번 (0) | 2023.01.11 |
---|---|
[C++] 명품 C++ 프로그래밍 실습문제 7장 6번 (0) | 2023.01.11 |
[C++] 명품 C++ 프로그래밍 실습문제 7장 4번 (0) | 2023.01.11 |
[C++] 명품 C++ 프로그래밍 실습문제 7장 3번 (0) | 2023.01.11 |
[C++] 명품 C++ 프로그래밍 실습문제 7장 2번 (0) | 2023.01.11 |