728x90
반응형
생성자와 소멸자
생성자 : 객체가 생성될 때 자동으로 호출되는 함수
소멸자 : 객체가 소멸될 때 자동으로 호출되는 함수
// 생성자 : 객체가 생성될 때 자동으로 호출되는 함수
// 소멸자 : 객체가 소멸될 때 자동으로 호출되는 함수
#include <iostream>
using namespace std;
class Myclass
{
public:
Myclass() // 생성자
{
cout << "생성자가 호출되었다!!" << endl;
}
~Myclass() // 소멸자
{
cout << "소멸자가 호출되었다!!" << endl;
}
};
// Myclass globalObj;
void testlocalObj() //지역 객체의 생성과 소멸을 테스트하기 위한 함수
{
cout << "testlocalObj함수 시작!" << endl;
Myclass localObj; // 지역변수
cout << "testlocalObj함수 끝!" << endl;
}
int main(void)
{
cout << "main함수 시작!" << endl;
testlocalObj();
cout << "main함수 끝!" << endl;
}
생성자와 소멸자의 용도
생성자 : 멤버 변수 초기화
소멸자 : 메모리 해제
// 생성자와 소멸자의 용도
// 생성자 : 멤버 변수 초기화
// 소멸자 : 메모리 해제
// 복소수(real(실수), imag(허수))
#include <iostream>
using namespace std;
class Complex
{
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(double real_, double imag_)
{
real = real_;
imag = imag_;
}
/*
Complex() 삭제 후 아래의 Complex함수를 이렇게 바꾸어도 실행이 된다)
Complex(double real_ = 0, double imag_ = 0)
{
real = real_;
imag = imag_;
}
*/
double Getreal()
{
return real;
}
void Setreal(double real_)
{
real = real_;
}
double Getimag()
{
return imag;
}
void Setimag(double imag_)
{
imag = imag_;
}
private:
double real;
double imag;
};
int main(void)
{
Complex c1;
Complex c2 = Complex(2, 3);
Complex c3(2, 3);
Complex c4 = { 2,3 }; // 중괄호를 이용해서도 선언이 가능하다.
Complex c5 = Complex{ 2,3 };
Complex c6{ 2,3 };
cout << "c1 = " << c1.Getreal() << ", " << c1.Getimag() << endl;
cout << "c1 = " << c2.Getreal() << ", " << c2.Getimag() << endl;
cout << "c1 = " << c3.Getreal() << ", " << c3.Getimag() << endl;
}
생성자의 다양한 형태
// 생성자의 다양한 형태
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() : real(0), imag(0){}
Complex(double real_, double imag_) : real(real_), imag(imag_){}
double Getreal()
{
return real;
}
void Setreal(double real_)
{
real = real_;
}
double Getimag()
{
return imag;
}
void Setimag(double imag_)
{
imag = imag_;
}
private:
double real;
double imag;
};
int main(void)
{
int a(5);
Complex c1;
Complex c2 = Complex(2, 3);
Complex c3(2, 3);
Complex c4 = { 2,3 }; // 중괄호를 이용해서도 선언이 가능하다.
Complex c5 = Complex{ 2,3 };
Complex c6{ 2,3 };
cout << "c1 = " << c1.Getreal() << ", " << c1.Getimag() << endl;
cout << "c2 = " << c2.Getreal() << ", " << c2.Getimag() << endl;
cout << "c3 = " << c3.Getreal() << ", " << c3.Getimag() << endl;
}
시간(시h,분m,초s)를 저장하는 클래스 생성 (생성자 위임 사용)
// 시간(시h,분m,초s)를 저장하는 클래스 생성 (생성자 위임 사용)
#include <iostream>
using namespace std;
class Time {
public:
// Time(5); //초
// Time(5,0); //분, 초
// Time(2,37,54)); //시 분 초
Time() : h(0), m(0), s(0){}
Time(int s_) : Time()
{
s = s_;
}
Time(int m_, int s_) : Time(s_)
{
m = m_;
}
Time(int h_, int m_, int s_) : Time(m_, s_)
{
h = h_;
}
/*
변수 초기화 목록과 형태가 같아서 헷갈릴 수 있으니 주의
변수 초기화 목록 예시 : m(m_), s(s_)
생성자 위임 예시 : Time(m_, s_)
*/
int h;
int m;
int s;
};
int main(void)
{
Time t1;
Time t2(5);
Time t3(3, 16);
Time t4(2, 42, 15);
cout << "t1 : " << t1.h << ":" << t1.m << ":" << t1.s << endl;
cout << "t2 : " << t2.h << ":" << t2.m << ":" << t2.s << endl;
cout << "t3 : " << t3.h << ":" << t3.m << ":" << t3.s << endl;
cout << "t4 : " << t4.h << ":" << t4.m << ":" << t4.s << endl;
}
728x90
반응형