(12470)
main함수가 실행되도록 파일을 클래스별로 나누어 코드를 작성하시오.
자식의 수에는 제한이 없도록 포인터 배열로 구현한다. 크기 제한은 20명으로 한다.
<main.cpp>
#include "wife.h"
#include "family.h"
#include "child.h"
using namespace std;
int main() {
Man *m = new Man("김부자");
Wife *f = new Wife("강예쁜");
Family *myFamily = new Family(m, f); // 생성자에서 서로 포인터를 연결해 준다.
Child *c1 = new Child("김장남");
Child *c2 = new Child("김차남");
Child *c3 = new Child("김삼남");
myFamily->AddChild(c1);
myFamily->AddChild(c2);
myFamily->AddChild(c3);
c1->Tellme(); // 나는 김장남, 아버지는 김부자, 어머니는 강예쁜, 형제들은 김차남, 김삼남이 있습니다.
m->Tellme(); // 나는 김부자, 내 아내는 강예쁜, 아이들은 김장남, 김차남, 김삼남
return 0;
}
<family.cpp>
#include <iostream>
#include "man.h"
#include "wife.h"
#include "family.h"
#include "child.h"
using namespace std;
Family::Family(Man* m, Wife* w) {
mom = w;
dad = m;
cnt = 0;
m->Setfam(this);
w->Setfam(this);
}
void Family::AddChild(Child* c) {
i[cnt] = c;
i[cnt]->Setfam(this);
cnt++;
}
string Family::getch_f(int n) {
string c;
c = i[n]->getch();
return c;
}
string Family::getm() {
string m;
m = mom->getmom();
return m;
}
string Family::getd() {
string d;
d = dad->getdad();
return d;
}
<man.cpp>
#include <iostream>
#include <string>
#include "man.h"
#include "family.h"
using namespace std;
Man::Man(string s) {
name = s;
}
void Man::Setfam(Family *fam) {
f = fam;
}
void Man::Tellme() {
string w;
string c0 = f->getch_f(0);
string c1 = f->getch_f(1);
string c2 = f->getch_f(2);
w = f->getm();
cout << "³ª´Â " << name << ", ³» ¾Æ³»´Â " << w << ", ¾ÆÀ̵éÀº " << c0 <<
", " << c1 << ", " << c2 << endl;
}
string Man::getdad() {
return name;
}
<wife.cpp>
#include <iostream>
#include "wife.h"
#include "family.h"
using namespace std;
Wife::Wife(string s) {
name = s;
}
void Wife::Setfam(Family* fam) {
f = fam;
}
string Wife::getmom() {
return name;
}
<child.cpp>
#include <iostream>
#include <string>
#include "child.h"
#include "family.h"
using namespace std;
Child::Child(string s) {
name = s;
}
void Child::Setfam(Family *fam) {
f = fam;
}
void Child::Tellme() {
string m, d;
string c1, c2;
m = f->getm();
d = f->getd();
c1 = f->getch_f(1);
c2 = f->getch_f(2);
cout << "나는 " << name << ", 아버지는 " << d << ", 어머니는 " << m << ", 형제들은 " << c1 << ", " << c2 << endl;
}
string Child::getch() {
return name;
}
<family.h>
#ifndef _FAMILY_H
#define _FAMILY_H
using namespace std;
class Man;
class Wife;
class Child;
class Family {
private:
Child* i[20];
int cnt; //ÀÚ½Ä ¼ö Ä«¿îÆ®
Wife* mom;
Man* dad;
public:
Family(Man* m, Wife* w);
void AddChild(Child* c);
string getch_f(int n);
string getm();
string getd();
};
#endif
<man.h>
#ifndef MAN_H
#define MAN_H
using namespace std;
class Family;
class Man {
private:
string name;
Family* f;
public:
Man(string s);
void Setfam(Family* fam);
void Tellme();
string getdad();
};
#endif
<wife.h>
#ifndef WIFE_H
#define WIFE_H
using namespace std;
class Family;
class Wife {
private:
string name;
Family* f;
public:
Wife(string s);
void Setfam(Family* fam);
string getmom();
};
#endif
<child.h>
#ifndef CHILD_H
#define CHILD_H
using namespace std;
class Family;
class Child {
private:
string name;
Family *f;
public:
Child(string s);
void Tellme();
void Setfam(Family* fam);
string getch();
};
#endif
<실행결과>
나는 김장남, 아버지는 김부자, 어머니는 강예쁜, 형제들은 김차남, 김삼남
나는 김부자, 내 아내는 강예쁜, 아이들은 김장남, 김차남, 김삼남
'C++ > 클래스 헤더파일 분리와 상호참조' 카테고리의 다른 글
클래스 상호참조 (수강신청 예제) (0) | 2021.01.27 |
---|---|
클래스 상호참조 (분대와 분대원 예제) (0) | 2021.01.23 |
클래스 상호참조 (애견학교 예제) (0) | 2021.01.23 |