본문 바로가기

프로그래밍/C++

코드를 보고 문제 정의하기


<과제 내용>

다음 소소코드를 작성하기 위한 문제를 정의해 보세요.

 

<소스코드>

#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 ageCStudent클래스의 char major[20]가 있다.

출력에는 agemajor가 표시된다.

 

<문제 정의>

나이값을 가지고 있는 클래스로부터 나이값을 전공값을 가지고 있는 학생클래스로 상속받아 나이와 전공을 출력하는 소스코드를 정의하라.

'프로그래밍 > C++' 카테고리의 다른 글

const 변수  (0) 2012.02.24
static 변수  (0) 2012.02.24
예외처리 예제  (0) 2012.02.24
예외처리  (0) 2012.02.24
파일 입출력 예제  (0) 2012.02.24