물려받다.
“UnivStudent 클래스가 Person 클래스를 상속한다.”
UnivStudent 클래스가 Person 클래스를 상속하게 되면, UnivStudent 클래스는 Person 클래스가 지니고 있는 모든 맴버를 물려받는다.
class Person
{
private:
int age;
char name[50];
public:
Person(int myage, char *myname) : age (myage) ...
void WhatYourName() const ...
void HowOldAreYou() const ...
}
class UnivStudent : public Person
{
private:
char major[50]; //전공과목
public:
UnivStudent(char *myname ...
Void WhoAreYou() const
{
WhatYourName();
HowOldAreYou();
cout << "my major is ...
}
}
위의 클래스 정의에서 다음의 선언이 의미하는 바는 ‘public 상속’이다.
UnivStudent 클래스에는 WhatYourName 함수와 HowOldAreYou 함수가 정의되어 있지 않음에도 불구하고, 이 두 함수를 호출할 수 있는 이유는 UnivStudent 클래스가 Person 클래스를 상속했기 때문이다.
앞서 정의한 UnivStudent 클래스의 생성자는 다음과 같이 정의되어 있다.
UnivStudent(char *myname, int myage, char *mymajor)
: Person(myage, myname)
{
strcpy(major, mymajor);
}
Question A:
Answer A:
Question B:
Answer B:
위에 두 질문은 UnivStudent 생성자에 적용되어 있다.