본문 바로가기

과제모음

[C++]복사생성자2

반응형

[[확인문제 1]]

>>Address.h<<

#ifndef _Address_H_
#define _ADDRESS_H_

class Address
{
private:
 char name[20];
 char phone[20];
 char *address;
 
public:
 Address(char *pN, char *pP, char *pA);
 Address(const Address &add);
 ~Address();
 void OutAddress()const;
};
#endif

>>main.cpp<<

#include <iostream>
#include "Address.h"

int main()
{
 Address a1("홍길동", "011-333-5555", "서울 종로구 효제동");

 Address a2 = a1;
 a1.OutAddress();
 a2.OutAddress();

 return 0;
}

>>Address.cpp<<

#include <iostream>
#include <string.h>
#include "Address.h"
using std::cout; using std::endl;

Address::Address(char *pN, char *pP, char *pA)
{
 strcpy(name, pN);
 strcpy(phone, pP);
 this->address = new char [strlen(pA)+1];
 strcpy(address, pA);
}
Address::Address(const Address &add)
{
 strcpy(name, add.name);
 strcpy(phone, add.phone);
 this->address = new char[strlen(add.address)+1];
 strcpy(address, add.address);
 cout << "복사 생성자\n";
}
Address::~Address()
{
 delete [] address;
 cout << "소멸자\n";
}
void Address::OutAddress()const
{
 cout << endl;
 cout << "이    름 : " << name << endl;
 cout << "전화번호 : " << phone << endl;
 cout << "주    소 : " << address <<endl;
}

[[확인문제 2]]

>>Memo.h<<

#ifndef _MEMO_H_
#define _MEMO_H_

class Memo
{
private:
 char *memo;
 char *Smemo;
 char *Gmemo;
 
public:
 Memo(char *pM, char *pS, char *pG);
 ~Memo();
 Memo(const Memo &cpm);
 void outMemo()const;
 void outLen()const;
 
};
#endif

>>main.cpp<<

#include <iostream>
#include "Memo.h"
using std::cout; using std::endl;

void output(Memo out);
void outputLength(Memo outlen);

int main()
{
 Memo m1("Happy Birthday to you~!!", "아무개","홍길동");
 Memo m2("오늘 날씨가 정말 좋네요....^*^", "한가한", "오기로");
 
 output(m1);
 output(m2);
 
 Memo m3 = m2;
 output(m3);
 
 cout << endl << endl;
 outputLength(m1);
 outputLength(m2);
 outputLength(m2);
 
 return 0;
}

void output(Memo out)
{
 out.outMemo();
}

void outputLength(Memo outlen)
{
 outlen.outLen();
}

>>Memo.cpp<<

#include <iostream>
#include <string.h>
#include "Memo.h"
using std::cout; using std::endl;
Memo::Memo(char *pM, char *pS, char *pG)
{
 this->memo = new char[strlen(pM)+1];
 strcpy(memo, pM);
 this->Smemo = new char[strlen(pS)+1];
 strcpy(Smemo, pS);
 this->Gmemo = new char[strlen(pG)+1];
 strcpy(Gmemo, pG);
}
Memo::~Memo()
{
 delete [] memo;
 delete [] Smemo;
 delete [] Gmemo;
 
}
Memo::Memo(const Memo &cpm)
{
 
 this->memo = new char[strlen(cpm.memo)+1];
 strcpy(memo, cpm.memo);
 this->Smemo = new char[strlen(cpm.Smemo)+1];
 strcpy(Smemo, cpm.Smemo);
 this->Gmemo = new char[strlen(cpm.Gmemo)+1];
 strcpy(Gmemo, cpm.Gmemo);
}

void Memo::outMemo()const
{
 cout << endl << Smemo << "님의 메모출력\n";
 cout << memo << endl;
 cout << Gmemo << "님께 전송되었습니다\n";
}
void Memo::outLen()const
{
 cout << "메모길이 : " <<  strlen(memo)<<endl;
}


반응형

'과제모음' 카테고리의 다른 글

[C++]Inheritance(상속2)  (0) 2010.01.22
[C++]Inheritance(상속)  (0) 2010.01.22
[C++]복사생성자  (0) 2010.01.22
[C++]Const & this & Static 변수  (0) 2010.01.22
[C++]상수(Const)  (0) 2010.01.22