πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 9 Solution

Problem: 문제 8번의 Circle 객체에 λŒ€ν•΄ λ‹€μŒ 연산이 κ°€λŠ₯ν•˜λ„λ‘ μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ν•˜λΌ. 1 2 3 4 Circle a(5), b(4); b = 1+a; // b의 λ°˜μ§€λ¦„μ„ a의 λ°˜μ§€λ¦„μ— 1을 λ”ν•œ κ²ƒμœΌλ‘œ λ³€κ²½ a.show(); b.show(); Execution Result: Objective & Hints: ν”„λ Œλ“œ ν•¨μˆ˜λ‘œ μ—°μ‚°μž κ΅¬ν˜„ μ—°μŠ΅ Code: 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(); }

March 6, 2020 Β· 1 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 8 Solution

Problem: 원을 μΆ”μƒν™”ν•œ Circle ν΄λž˜μŠ€λŠ” κ°„λ‹¨νžˆ μ•„λž˜μ™€ κ°™λ‹€. 1 2 3 4 5 6 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } void show() { cout << "radius = " << radius << " 인 원" << endl; } }; λ‹€μŒ 연산이 κ°€λŠ₯ν•˜λ„λ‘ μ—°μ‚°μžλ₯Ό ν”„λ Œλ“œ ν•¨μˆ˜λ‘œ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 Circle a(5), b(4); ++a; // λ°˜μ§€λ¦„μ„ 1 증가 μ‹œν‚¨λ‹€. b = a++; // λ°˜μ§€λ¦„μ„ 1 증가 μ‹œν‚¨λ‹€. a.show(); b.show(); Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 7 Solution

Problem: 2차원 행렬을 μΆ”μƒν™”ν•œ Matrix 클래슀λ₯Ό ν™œμš©ν•˜λŠ” λ‹€μŒ μ½”λ“œκ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 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의 멀버 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. ...

March 6, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 6 Solution

Problem: 2차원 행렬을 μΆ”μƒν™”ν•œ Matrix 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , show() 멀버 ν•¨μˆ˜μ™€ λ‹€μŒ 연산이 κ°€λŠ₯ν•˜λ„λ‘ μ—°μ‚°μžλ₯Ό λͺ¨λ‘ κ΅¬ν˜„ν•˜λΌ. 1 2 3 4 5 6 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의 멀버 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. (2) μ—°μ‚°μž ν•¨μˆ˜λ₯Ό Matrix의 ν”„λ Œλ“œ ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. Execution Result: Objective & Hints: ...

March 6, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 5 Solution

Problem: λ‹€μŒ main()μ—μ„œ Color ν΄λž˜μŠ€λŠ” 3μš”μ†Œ(λΉ¨κ°•, 초둝, νŒŒλž‘)둜 ν•˜λ‚˜μ˜ 색을 λ‚˜νƒ€λ‚΄λŠ” ν΄λž˜μŠ€μ΄λ‹€(4μž₯ Exercise Problem 1번 μ°Έκ³ ). μ—°μ‚°μžλ‘œ 색을 λ”ν•˜κ³ , == μ—°μ‚°μžλ‘œ 색을 λΉ„κ΅ν•˜κ³ μž ν•œλ‹€. Execution Resultλ₯Ό μ°Έκ³ ν•˜μ—¬ Color ν΄λž˜μŠ€μ™€ μ—°μ‚°μž, 그리고 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 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 클래슀의 멀버 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. ...

March 6, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 4 Solution

Problem: 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; } }; λ‹€μŒ 연산을 톡해 μ±…μ˜ 제λͺ©μ„ 사전 순으둜 λΉ„κ΅ν•˜κ³ μž ν•œλ‹€. ...

March 6, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 3 Solution

Problem: 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; } }; λ‹€μŒ 연산을 톡해 곡짜 책인지λ₯Ό νŒλ³„ν•˜λ„λ‘ ! μ—°μ‚°μžλ₯Ό μž‘μ„±ν•˜λΌ. ...

March 6, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 2 Solution

Problem: 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; } }; Book 객체λ₯Ό ν™œμš©ν•˜λŠ” 사둀이닀. ...

March 6, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.7 Exercise 1 Solution

Problem: 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; } }; Book 객체에 λŒ€ν•΄ λ‹€μŒ 연산을 ν•˜κ³ μž ν•œλ‹€. ...

March 6, 2020 Β· 3 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.6 Exercise 9 Solution

Problem: κ²Œμ‹œνŒ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄λ³΄μž. λ©€ν‹°νƒœμŠ€ν‚Ήμ˜ 경우 μ—¬λŸ¬ μ‚¬μš©μžλ“€μ΄ κ²Œμ‹œνŒμ— 글을 올리기 λ•Œλ¬Έμ— κ²Œμ‹œνŒ κ°μ²΄λŠ” 전체 ν•˜λ‚˜λ§Œ μžˆμ–΄μ•Ό ν•œλ‹€. κ·ΈλŸ¬λ―€λ‘œ κ²Œμ‹œνŒ 객체의 멀버듀은 static으둜 μž‘μ„±ν•œλ‹€. λ‹€μŒμ€ κ²Œμ‹œνŒ κΈ°λŠ₯을 ν•˜λŠ” Board 클래슀λ₯Ό ν™œμš©ν•˜λŠ” main() μ½”λ“œμ΄λ‹€. Execution Resultλ₯Ό μ°Έκ³ ν•˜μ—¬ Board 클래슀λ₯Ό λ§Œλ“€κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. static μ—°μŠ΅μ΄ λͺ©μ μ΄κΈ° λ•Œλ¬Έμ— κ²Œμ‹œνŒ κΈ°λŠ₯을 글을 μ˜¬λ¦¬λŠ” κΈ°λŠ₯κ³Ό κ²Œμ‹œκΈ€μ„ λͺ¨λ‘ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯으둜 μ œν•œν•˜κ³  main()도 λ‹¨μˆœν™”ν•˜μ˜€λ‹€. 1 2 3 4 5 6 7 8 int main() { // Board myBoard; // 객체 생성은 컴파일 였λ₯˜μž…λ‹ˆλ‹€. Board::add("μ€‘κ°„κ³ μ‚¬λŠ” 감독 μ—†λŠ” 자율 μ‹œν—˜μž…λ‹ˆλ‹€."); Board::add("μ½”λ”© λΌμš΄μ§€ 많이 μ΄μš©ν•΄ μ£Όμ„Έμš”."); Board::print(); Board::add("μ§„μ†Œλ¦° 학생이 κ²½μ§„λŒ€νšŒ μž…μƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. μΆ•ν•˜ν•΄μ£Όμ„Έμš”"); Board::print(); } Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil