πŸ’» Dev & CSC++ Programming

C++ Programming Ch.8 Exercise 4 Solution

Problem: 문제 3~4에 μ μš©λ˜λŠ” 2차원 μƒμ˜ ν•œ 점을 ν‘œν˜„ν•˜λŠ” Point ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Point { int x,y; public: point(int x, int y) { this->x = x; this->y = y; } int getX(){ return x; } int getY(){ return y; } protected: void move(int x, int y) { this->x = x; this->y = y; } }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Point 클래슀λ₯Ό μƒμ†λ°›λŠ” ColorPoint 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

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

C++ Programming Ch.6 Exercise 4 Solution

Problem: λ‹€μŒ ν΄λž˜μŠ€μ— μ€‘λ³΅λœ μƒμ„±μžλ₯Ό λ””ν΄νŠΈ 맀개 λ³€μˆ˜λ₯Ό κ°€μ§„ ν•˜λ‚˜μ˜ μƒμ„±μžλ‘œ μž‘μ„±ν•˜κ³  ν…ŒμŠ€νŠΈ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class MyVector{ int *mem; int size; public: MyVector(); MyVector(int n, int val); ~MyVector() { delete [] mem; } }; MyVector::MyVector() { mem = new int [100]; size = 100; for(int i=0; i<size; i++) mem[i] = 0; } MyVector::MyVector(int n, int val) { mem = new int [n]; size = n; for(int i=0; i<size; i++) mem[i] = val; } Execution Result: ...

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

C++ Programming Ch.6 Exercise 3 Solution

Problem: ν•¨μˆ˜ big()을 ν˜ΈμΆœν•˜λŠ” κ²½μš°λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 int main() { int x = big(3, 5); // 3κ³Ό 5쀑 큰 값은 5λŠ” μ΅œλŒ€κ°’ 100보닀 μž‘μœΌλ―€λ‘œ, 5 리턴 int y = big(300, 60); // 300κ³Ό 60 쀑 큰 κ°’ 300이 μ΅œλŒ€κ°’ 100보닀 ν¬λ―€λ‘œ, 100리턴 int z = big(30, 60, 50); // 30κ³Ό 60 쀑 큰 κ°’ 60이 μ΅œλŒ€κ°’ 50보닀 ν¬λ―€λ‘œ, 50리턴 cout << x << ' ' << y << ' ' << z << endl; } (1) big() ν•¨μˆ˜λ₯Ό 2개 μ€‘λ³΅ν•˜μ—¬ μž‘μ„±ν•˜κ³  ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

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

C++ Programming Ch.6 Exercise 1 Solution

Problem: add() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” main() ν•¨μˆ˜λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 int main() { int a[] = {1,2,3,4,5}; int b[] = {6,7,8,9,10}; int c = add(a, 5); // λ°°μ—΄ a의 μ •μˆ˜λ₯Ό λͺ¨λ‘ λ”ν•œ κ°’ 리턴 int d = add(a, 5, b); // λ°°μ—΄ a와 b의 μ •μˆ˜λ₯Ό λͺ¨λ‘ λ”ν•œ κ°’ 리턴 cout << c << endl; // 15 좜λ ₯ cout << d << endl; // 55 좜λ ₯ } (1) add() ν•¨μˆ˜λ₯Ό 쀑볡 μž‘μ„±ν•˜κ³  ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 5, 2020 Β· 2 min Β· Sobamemil