๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.5 Exercise 9 Solution

Problem: ํด๋ž˜์Šค Accumulator๋Š” add() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ณ„์† ๊ฐ’์„ ๋ˆ„์ ํ•˜๋Š” ํด๋ž˜์Šค๋กœ์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋œ๋‹ค. Accumulator ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. 1 2 3 4 5 6 7 class Accumulator{ int value; public: Accumulator(int value); // ๋งค๊ฐœ ๋ณ€์ˆ˜ value๋กœ ๋ฉค๋ฒ„ value๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. Accumulator& add(int n); // value์— n์„ ๋”ํ•ด ๊ฐ’์„ ๋ˆ„์ ํ•œ๋‹ค. int get(); // ๋ˆ„์ ๋œ ๊ฐ’ value๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. }; Accumulator๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด main() ํ•จ์ˆ˜์— ์˜ํ•ด ํ™œ์šฉ๋œ๋‹ค. 1 2 3 4 5 int main() { Accumulator acc(10); acc.add(5).add(6).add(7); // acc์˜ value ๋ฉค๋ฒ„๊ฐ€ 28์ด ๋œ๋‹ค. cout << acc.get(); // 28 ์ถœ๋ ฅ } Execution Result: ...

March 5, 2020 ยท 2 min ยท Sobamemil