-
8부 (클래스) 종합문제 - 2C++ 2021. 12. 23. 18:35
https://www.youtube.com/watch?v=WfWH3s3rSbo&list=PLlJhQXcLQBJqywc5dweQ75GBRubzPxhAk&index=69
문제1.
// 다음의 코드를 보고 출력 결과를 예상하시오. 살짝 어려움
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() : num(cnt++), ch('\0') { }
void Check(MyClass* ptr) {
if (ptr + num == this) {
cout << num << endl;
}
}
static int cnt;
private:
int num;
char ch;
};
int MyClass::cnt = 0;
int main() {
MyClass obj[5];
cout << "Test #1 : " << endl;
for (int i = 0; i < 5; i++)
{
obj[i].Check(obj);
}
cout << "Test #2 : " << endl;
for (MyClass& i : obj) {
i.Check(obj);
}
cout << "cnt = " << MyClass::cnt << endl;
}정답1.
Test #1 :
0
1
2
3
4
Test #2 :
0
1
2
3
4
cnt = 5'C++' 카테고리의 다른 글
깊은 복사와 얕은 복사 (1) (0) 2021.12.24 동적 할당 (0) 2021.12.23 8부 (클래스) 종합문제 - 1 (0) 2021.12.23 연산자 오버로딩 (0) 2021.12.23 멤버 메서드 활용하기 (0) 2021.12.23