다음 C++ 프로그램의 실행 결과로 옳은 것은?
#include <iostream>
using namespace std;
class Student {
public:
Student() : Student(0) {};
Student(int id) : _id(id) {
if (_id > 0) _cnt++;
};
static void print() { cout << _cnt; };
void printID() { cout << ++_id; };
private:
int _id;
static int _cnt;
};
int Student::_cnt = 0;
int main() {
Student A(2);
Student B;
Student C(4);
Student D(-5);
Student E;
Student::print();
E.printID();
return 0;
}정답 1번
정적 변수 _cnt는 양수 ID를 받은 A(2)와 C(4)를 생성할 때만 증가하므로 Student::print()는 2를 출력한다. 기본 생성된 E의 _id는 0이고 printID()가 이를 먼저 1 증가시켜 출력하므로 이어지는 값은 1이다. 따라서 전체 출력은 21로 1번이 정답이다.
콘텐츠 정보
- 자료 유형
- 공개 기출
- 검수 상태
- 내용 검수 완료
- 해설 작성·검수
- 정처LAB 편집 기준
- 최종 검수
- 2026-09-05