본문 바로가기

언어/C++

복사생성자

#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Person{
	char *name;
	int id;
	public :
		Person(int id,char*name);
		Person(Person& person);
		~Person();
		void changeName(char *name);
		void show(){cout << id << ','<< name << endl;}
};

Person:: Person(int id, char *name)
{
	this->id=id;
	int len= strlen(name);
	this->name=new char[len+1];
	strcpy(this->name,name);
}

Person::Person(Person& person){
	
	this->id=person.id;
	int len=strlen(person.name);
	this->name=new char[len+1];
	strcpy(this->name,person.name);
	cout<< "복사 생성자 실행  원본 객체의 이름 " << this->name << endl;
	
}




Person:: ~Person(){
	if(name)
		delete[] name;
}

void Person:: changeName(char*name){
	if(strlen(name)> strlen(this->name)){
		return;
	}
	strcpy(this->name,name);
}


int main(int argc, char** argv) {
	
	Person father(1,"Kitae");
	Person daughter(father);
	
	cout << "daughter 객체 생성 직후 --- " << endl;
	father.show();
	daughter.show();
	daughter.changeName("Grace");
	
	cout<<"daughter 이름을 Grace로 변경한 후 ---" << endl;
	father.show();
	daughter.show();
	 
	return 0;
}

//깊은 복사 생성자를 가진 정상적인 person 클래스
#include <iostream>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Person{
	char *name;
	int id;
	public :
		Person(int id,char*name);
		~Person();
		void changeName(char *name);
		void show(){cout << id << ','<< name << endl;}
};

Person:: Person(int id, char *name)
{
	this->id=id;
	int len= strlen(name);
	this->name=new char[len+1];
	strcpy(this->name,name);
}

Person:: ~Person(){
	if(name)
		delete[] name;
}

void Person:: changeName(char*name){
	if(strlen(name)> strlen(this->name)){
		return;
	}
	strcpy(this->name,name);
}


int main(int argc, char** argv) {
	
	Person father(1,"Kitae");
	Person daughter(father);
	
	cout << "daughter 객체 생성 직후 --- " << endl;
	father.show();
	daughter.show();
	daughter.changeName("Grace");
	
	cout<<"daughter 이름을 Grace로 변경한 후 ---" << endl;
	father.show();
	daughter.show();
	 
	return 0;
}

//얕은 복사 생성자를 사용하여 프로그램이 비정상 종료 되는 경우
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

class Circle{	
	private :
		int radius;
	public:
		Circle (Circle& c);
		Circle(){
			radius=1;
		}
		Circle(int radius){
			this->radius=radius;
		}
		double getArea(){
			return 3.14*radius*radius;
		}
};


Circle::Circle(Circle& c){
	this->radius=c.radius;
	cout<<" 복사 생성자 실행 radius = " << radius << endl; 
}



int main(int argc, char** argv) {
	
	Circle src(30);
	Circle dest(src);
	
	cout << "원본의 면적 = " << src.getArea() << endl;
	cout << "사본의 면적 = " << dest.getArea() << endl;
	
	return 0;
}

//Circle 클래스의 복사 생성자와 객체 복사 

'언어 > C++' 카테고리의 다른 글

4단원 예제문제  (1) 2019.11.14
5단원 실습문제 (참조, 복사생성자 )  (1) 2019.11.12
함수와 참조  (0) 2019.11.12
히스토그램  (0) 2019.11.11
원의 개수 와 면적 구하기  (0) 2019.11.11