블로그 이미지
SANGHO KIM

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

2018. 1. 22. 21:27 C++Language

복사 생성자?

복사 생성자는 객체가 복사될 때 호출되는 성성자입니다. 

복사 생성자를 정의하기 위해 함수의 매개변수 값으로 '객체'를 전달함.

복사 생성자가 필요한 경우?

동적 할당된 메모리 공간이 존재할 경우, 객체를 단순히 복사하면 문제가 발생할 수 있음.

기본 복사 생성자(얕은 복사) VS 복사 생성자(깊은 복사)

기본 복사 생성자:  멤버값이 사본 객체의 멤버값으로 똑같이 단순 복사됨.

복사 생성자: 멤버값이 사복 객체의 멤버값으로 동적 할당된 메모리 공간을 고려하여 복사됨.

예제(1)

#include<iostream>

#include<cstring>

using namespace std;

class AAA

{

public:

AAA() //생성자 

{

cout<<"AAA() 호출"<<endl; 

}

AAA(int i) //생성자 

{

cout<<"AAA(int i) 호출"<<endl;

}

AAA(const AAA &a) //복사 생성자 

{

cout<<"AAA(const AAA &a) 호출"<<endl; 

}

};

int main(void)

{

AAA obj1;

AAA obj2(10);

AAA obj3(obj2); // const AAA &a=obj2

return 0;

예제(2)

#include<iostream>

#include<cstring>

using namespace std; 

class Point

{

int x, y;

public:

Point(int _x, int _y) 

{

x=_x;

y=_y;

}

Point(const Point &p)

{

x=p.x;

y=p.y;

void ShowData()

{

cout<<x<<' '<<y<<endl;

};

int main(void)

{

Point p1(10,20);

Point p2(p1);

p1.ShowData();

p2.ShowData();

return 0;

예제(3); Default 복사 생성자

#include<iostream>

#include<cstring>

using namespace std;

class Point //클래스를 정의함 

{

int x, y; //정수 x, y를 정의함 

public:

Point(int _x, int _y) //함수를 정의함 

{x=_x;y=_y;}

/* 

Point(const Point& p)

{x=p.x; y=p.y;}

*/ 

void ShowData() //출력 함수를 정의함 

{ cout<<x<<' '<<y<<endl;} 

};

int main(void)

{

Point p1(10,20); //함수 오버로딩 

Point p2(p1);

p1.ShowData();

p2.ShowData();

return 0;

예제(4)

#include<iostream>

#include<cstring> 

using std::cout;

using std::endl;


class AAA //클래스 정의 

{

int val; //변수 val 

public:

AAA(int i) //함수 AAA 

{

val=i;

}

AAA(const AAA& a) //함수 AAA 

{

cout<<"AAA(const A& a) 호출"<<endl;

val=a.val;

}

void ShowData() //함수 ShowData() 

{

cout<<"val: "<<val<<endl;

}

};

int main()

{

AAA obj1(10); //AAA 클레스 obj1 정의 

AAA obj2=obj1; //AAA 클래스 obj1과 동일 

return 0;

}

예제(5)

#include<iostream>

#include<cstring> 

using std::cout;

using std::endl;


class AAA //클래스 정의 

{

int val; //변수 val 

public:

AAA(int i) //함수 AAA 

{

val=i;

}

AAA(const AAA& a) //함수 AAA 

{

cout<<"AAA(const A& a) 호출"<<endl;

val=a.val;

}

void ShowData() //함수 ShowData() 

{

cout<<"val: "<<val<<endl;

}

};

void function(AAA a)

{

a.ShowData();

 } 

int main()

{

AAA obj(30);

function(obj);

return 0;

}

예제(6)

#include<iostream>

#include<cstring> 

using namespace std;

class AAA //클래스 정의 

{ int val; //변수 val 

public:

AAA(int i) //함수 AAA 

{

val=i;

}

AAA(const AAA &a) //함수 AAA 

{

cout<<"AAA(const A &a) 호출"<<endl;

val=a.val;

}

void ShowData() //함수 ShowData() 

{

cout<<"val: "<<val<<endl;

}

};

AAA function(void) //클래스 AAA를 이용하여 객체 a를 만들고 객체 a를 return하는 예제  

{

AAA a(10);

return a;

}

int main()

{

function();

function().ShowData();

return 0;

}

예제(7)

#include <iostream>

using namespace std;

class Age

{

private:

int *age;

public:

Age(int valueOfAge);

Age(const Age &obj);//복사생성자 정의 

int getAge() const{return *age;}

void setAge(int valueOfAge){*age=valueOfAge;}

};

Age::Age(int valueOfAge)

{

age=new int; //int *age; 가 존재하므로 여기서, age에 메모리 주소가 저장됨.  

*age=valueOfAge;

}

Age::Age(const Age &obj)

{

cout<<"Age boy=girl 호출"<<endl;

age=new int;

*age=obj.getAge();//동일 한 메모리 공간의 사용을 피하기 위해 원본객체의 age가 가리키는 값을 얻음 

}

int main()

{

Age girl(30); //int valueOfAge=30;

cout<<"girl의 나이:"<<girl.getAge()<<endl;

Age boy=girl; //const Age &obj=Age girl; 즉 참조에 의하여 const Age형을 전달 받음. 

cout<<"boy의 나이:"<<boy.getAge()<<endl; 

boy.setAge(100);//이 것은 원본 girl의 나이에 영향을 미치지 않음. 

cout<<"boy의 나이:"<<boy.getAge()<<endl;

cout<<"girl의 나이:"<<girl.getAge()<<endl; 

return 0;

}


posted by SANGHO KIM
2018. 1. 22. 02:49 Kratos

codeblocks 

http://www.codeblocks.org/

'Kratos' 카테고리의 다른 글

Kratos 개발 논문  (0) 2017.12.04
Kratos 설치에 대한 이해(1); 윈도우 설치  (0) 2017.12.04
posted by SANGHO KIM
2018. 1. 21. 23:24 C++Language

객체를 생성할 때 크게 정적과 동적방법으로 구분됨.

예제(1); 정적객체 선언

#include <iostream>

using namespace std;

class Time {

protected:

 int hour,min,sec;

public:

 Time(int h,int m,int s) {

   hour=h;

   min=m;

   sec=s;

 }

 

void outtime();

};

void Time::outtime(void)

{

cout << "time is " << hour << " hour " << min

    << " minute " << sec << " second." << endl;

}

int main(void)

{

Time t1(10,12,34);

t1.outtime();

return 0;

예제(2); 동적객체 선언; 

Case(1)

#include <iostream>

using namespace std;

class Time 

{

protected:

int hour,min,sec;

public:

Time(int h,int m,int s) 

{

hour=h;

min=m;

sec=s;

}

void outtime();

};

void Time::outtime(void)

{

cout << "time is " << hour << " hour " << min<< " minute " << sec << " second." << endl;

}

int main(void)

{

Time *t1=new Time(10,12,34);

t1->outtime();

delete t1;

return 0;

}

Case(2)

#include <iostream>

using namespace std;

class Time 

{

protected:

int hour,min,sec;

public:

Time(int h,int m,int s) 

{

hour=h;

min=m;

sec=s;

}

void outtime();

};

void Time::outtime(void)

{

cout << "time is " << hour << " hour " << min

    << " minute " << sec << " second." << endl;

}

int main(void)

{

Time *t1;               // 동적 객체의 번지를  point할  변수

t1=new Time(10,12,34);  // 동적 객체의 생성

t1->outtime();

delete t1;

return 0;


예제(3); 객체를 정적으로 선언

#include<cstring>

using std::cout;

using std::endl;

class Point

{

int x; //정수 x 

int y; //정수 y 

public:

point() //함수 정의 

{

cout<<"Point() call!"<<endl;

x=y=0; //변수 값 = 0 

}

point(int _x, int _y)

{

x=_x;

y=_y;

}

int GetX(){return x;}

int GetY(){return y;}

void SetX(int _x){x=_x;}

void SetY(int _y){y=_y;}

};

int main()

{

Point arr[5]; // 배열 정의 

for(int i=0; i<5; i++)

{

arr[i].SetX(i*2);

arr[i].SetY(i*3);

}

for(int j=0; j<5; j++)

{

cout<<"x: "<<arr[j].GetX()<<' ';

cout<<"y: "<<arr[j].GetY()<<endl;

}

return 0;

객체를 동적으로 선언

#include<iostream>

#include<cstring>

using namespace std;

class Point

{

int x; //정수 x 

int y; //정수 y 

public:

Point() //함수 정의 

{

cout<<"Point() call!"<<endl;

x=y=0; //변수 값 = 0 

}

Point(int _x, int _y)

{

x=_x;

y=_y;

}

int GetX(){return x;}

int GetY(){return y;}

void SetX(int _x){x=_x;}

void SetY(int _y){y=_y;}

};

int main()

{

Point* arr[5]; //포인트 배열 정의 

for(int i=0; i<5; i++)

{

arr[i]=new Point(i*2, i*3); //new에 의한 객체 동적 생성 

}

for(int j=0; j<5; j++)

{

cout<<"x: "<<arr[j]->GetX()<<' ';

cout<<"y: "<<arr[j]->GetY()<<endl;

}

return 0;

}

예제(4)

#include<iostream>

#include<cstring>

using namespace std;

class Person

{

public:

Person *GetThis()

{

return this; 

}

 };

 int main()

 {

  cout<<"***** p1의 정보 *****"<<endl; //출력 

  Person *p1 = new Person(); //Person클래스에 대한 포인트 p1 정의 

  cout<<"포인터 p1: "<<p1<<endl;

  cout<<"p1의 this: "<<p1->GetThis()<<endl;

  cout<<"***** p2의 정보 ******"<<endl;

  Person *p2 = new Person();

cout<<"포인터 p2: "<<p2<<endl;

cout<<"p2의this: : "<<p2->GetThis()<<endl;

return 0;

}


posted by SANGHO KIM