다음 C++ 프로그램의 실행 결과가 아래와 같이 나오기 위해 ㉠에서 구현해야 하는 생성자 코드로 옳은 것은?
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
class A {
char *str;
public:
A() { str = NULL; }
㉠
A(char *ss) {
str = new char[strlen(ss)+1];
strcpy(str, ss);
}
void set(char *cc) { strcpy(str, cc); }
void show() { cout << str << endl; }
};
void main() {
A a("AAAA");
A b = a;
a.set("BBBB");
a.show();
b.show();
}<실행 결과> | ||
BBBB
AAAA | ||
- ①A(A a) {str = new char[strlen(a.str)+1];strcpy(str, a.str);}
- ②A(A *a) {str = new char[strlen(a->str)+1];strcpy(str, a->str);}
- ③A(A &a) {str = new char[strlen(a.str)+1];strcpy(str, a.str);}
- ④A(char *a) {str = new char[strlen(a)+1];strcpy(str, a);}
선지를 골라 바로 채점해 보세요.
정답 ③
출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)