<과제 내용>
다음 소소코드를 작성하기 위한 문제를 정의해 보세요.
<소스코드>
#include <iostream>
using namespace std;
class CPerson
{
private:
int age;
public:
CPerson(int age){ this->age = age; }
int getAge(){ return age; }
};
class CStudent : protected CPerson
{
private:
char major[20];
public:
CStudent(char* major, int age) : CPerson(age){
strcpy(this->major, major);}
char* getMajor(){
return major;}
void showData(){
cout<<"age : "<< cout<<"major : "};
int main(){
CStudent s("정보", 18);
s.showData();
return 0;
}
<소스코드 분석>
CStudent클래스는 CPerson클래스로부터 상속받는다.
변수는 CPerson클래스의 int age와 CStudent클래스의 char major[20]가 있다.
출력에는 age와 major가 표시된다.
<문제 정의>
나이값을 가지고 있는 클래스로부터 나이값을 전공값을 가지고 있는 학생클래스로 상속받아 나이와 전공을 출력하는 소스코드를 정의하라.