다중상속
Last updated
Last updated
#ifndef __PERSON__
#define __PERSON__
class Person {
char* name;
public:
Person(const char* name);
virtual ~Person();
void sayName();
virtual void whoAreYou() = 0; // 순수가상함수
};
#endif#include "Person.h"
#include <iostream>
#include <cstring>
using namespace std;
Person::Person(const char* name) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
cout << "Person()" << endl;
}
Person::~Person() {
delete[] name;
}
void Person::sayName() {
cout << "이름 : " << name << endl;
};#ifndef __STUDENT__
#define __STUDENT__
#include "Person.h"
class Student : public Person {
char* school;
public:
Student(const char*name, const char* school);
virtual ~Student();
void saySchool();
virtual void whoAreYou();
};
#endif#include "Student.h"
#include <iostream>
#include <cstring>
using namespace std;
Student::Student(const char*name, const char* school)
: Person(name)
{
this->school = new char[strlen(school) + 1];
strcpy(this->school, school);
cout << "Student()" << endl;
}
Student::~Student() {
delete[] school;
}
void Student::saySchool() {
cout << "학교 : " << school << endl;
}
void Student::whoAreYou() {
sayName();
saySchool();
}#ifndef __EMPLOYEE__
#define __EMPLOYEE__
#include "Person.h"
class Employee : public Person {
char* company;
public:
Employee(const char*name, const char*company);
virtual ~Employee();
void sayCompany();
virtual void whoAreYou();
};
#endif#include "Employee.h"
#include <iostream>
#include <cstring>
using namespace std;
Employee::Employee(const char*name, const char*company)
: Person(name)
{
this->company = new char[strlen(company) + 1];
strcpy(this->company, company);
cout << "Employee()" << endl;
}
Employee::~Employee() {
delete[] company;
}
void Employee::sayCompany() {
cout << "회사 :" << company << endl;
}
void Employee::whoAreYou() {
sayName();
sayCompany();
}#ifndef __PART_TIME_STUDENT__
#define __PART_TIME_STUDENT__
#include "Student.h"
#include "Employee.h"
class PartTimeStudent : public Student, public Employee {
public:
PartTimeStudent(const char* name, const char*school, const char* company);
virtual void whoAreYou();
};
#endif#include "PartTimeStudent.h"
#include <iostream>
using namespace std;
PartTimeStudent::PartTimeStudent(const char* name, const char*school, const char* company)
: Student(name, school), Employee(name, company)
{
cout << "PartTimeStudent()" <<endl;
}
void PartTimeStudent::whoAreYou() {
Student::sayName();
saySchool();
sayCompany();
}#include <iostream>
using namespace std;
#include "PartTimeStudent.h"
int main() {
cout << "객체 생성 순서" << endl;
PartTimeStudent man1("남기용", "방송통신대", "이지코드");
cout << "객체 생성 완료" << endl << endl;
man1.whoAreYou();
cout << endl;
man1.Student::whoAreYou();
cout << endl;
man1.Employee::whoAreYou();
return 0;
}