Soft Ware/C++ 언어!!

생성자, 소멸자 , 복사 생성자

달려가보자 2011. 10. 7. 07:13
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Person
{
int age;
char *name;
public:
Person(int age,char *name);
Person(const Person& Data);
~Person() { delete []name; }
int GetAge() const { return age;}
const char * GetName() const { return name; } 

};

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

inline Person::Person(const Person& Data)
{
age = Data.age;
name = new char[strlen(Data.name)+1];
strcpy(name,Data.name);
}

class Student : public Person
{
char *major;
public:
Student(int _age,char *_name, char* _major) : Person(_age, _name)
{
major = new char[strlen(_major)+1]; 
strcpy(major,_major);
}
Student(Student& Data) : Person(Data)
{
major = new char [strlen(Data.major)+1];
strcpy(major,Data.major);
}
~Student() { delete []major; }
void ShowData()
{
cout<<"나이 : "<<GetAge()<<endl;
cout<<"이름 : "<<GetName()<<endl;
cout<<"전공 : "<<major<<endl;
}
};

int main()
{
Student Hong1(20,"Hong Gil Dong","computer");
Hong1.ShowData();
Student Hong2=Hong1;
Hong2.ShowData();
return 0;
}

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

cin 에 대해서 !!!  (0) 2011.10.19
삼항연산자!!  (0) 2011.10.19
this 키워드 !!  (0) 2011.10.06
소멸자!!  (0) 2011.10.06
생성자!!  (0) 2011.10.06