💻 개발 & CSC++ 프로그래밍

명품 C++ programming 실습 문제 7장 9번

문제 : 문제 8번의 Circle 객체에 대해 다음 연산이 가능하도록 연산자를 구현하라. 1 2 3 4 Circle a(5), b(4); b = 1+a; // b의 반지름을 a의 반지름에 1을 더한 것으로 변경 a.show(); b.show(); 실행 결과 : 목적 및 힌트 : 프렌드 함수로 연산자 구현 연습 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include 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 x, Circle c); }; Circle operator+ (int x, Circle c){ c.radius += x; return c; } int main() { Circle a(5), b(4); b = 1+a; // b의 반지름을 a의 반지름에 1을 더한 것으로 변경 a.show(); b.show(); }

2020년 3월 6일 · 1 분 · Sobamemil
💻 개발 & CSC++ 프로그래밍

명품 C++ programming 실습 문제 7장 4번

문제 : 1번 ~ 4번 문제까지 사용될 Book 클래스는 다음과 같습니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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; } }; 다음 연산을 통해 책의 제목을 사전 순으로 비교하고자 한다. ...

2020년 3월 6일 · 2 분 · Sobamemil