C++ 언어/연습문제
C++ 언어 연습문제27
powerdeng
2022. 11. 1. 08:02
[문제]
아래 main문을 보고 클래스를 정의하라.
int main(void)
{
Sosimple sim1(15, 30);
cout << "생성 및 초기화 직전" << endl;
Sosimple sim2 = sim1;
cout << "생성 및 초기화 직후" << endl;
sim2.ShowSimpleData();
return 0;
}
[실행 결과]
[코드]
#include <iostream>
#include<cstring>
using namespace std;
class Sosimple
{
private:
int num1;
int num2;
public:
Sosimple(int n1, int n2)
:num1(n1), num2(n2)
{
//empty
}
Sosimple(const Sosimple& copy)
{
num1 = copy.num1 * -1;
num2 = copy.num2 * -1;
cout << "Called Sosimple(Sosimple ©)" << endl;
}
void ShowSimpleData()
{
cout << num1 << endl;
cout << num2 << endl;
}
};
int main(void)
{
Sosimple sim1(15, 30);
cout << "생성 및 초기화 직전" << endl;
Sosimple sim2 = sim1;
cout << "생성 및 초기화 직후" << endl;
sim2.ShowSimpleData();
return 0;
}
<참고>
윤성우의 열혈 C++ 프로그래밍