1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <iostream> using namespace std;
class Color { int red, green, blue; public: Color(int red = 0, int green = 0, int blue = 0) { this->red = red; this->green = green; this->blue = blue; } void show(); Color operator+(Color op2); bool operator==(Color op2); }; void Color::show() { cout << "Red: " << this->red << " Green: " << this->green << " Blue: " << this->blue << endl; }
Color Color::operator+(Color op2) { Color tmp; tmp.blue = this->blue + op2.blue; tmp.red = this->red + op2.red; tmp.green = this->green + op2.green; return tmp; }
bool Color::operator==(Color op2) { if (this->red == op2.red && this->green == op2.green && this->blue == op2.blue) return true; else 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 << "보라색 아님"; }
|
cs |
'C++' 카테고리의 다른 글
명품 c++ 7장 실습문제 8번 (0) | 2019.06.06 |
---|---|
명품 c++ 7장 실습문제 6번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 4번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 3번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 2번 (0) | 2019.06.05 |