본문 바로가기

과제모음

[C++]Inheritance(상속)

반응형

[[연습문제 1]]

>>Human.h<<

#ifndef _HUMAN_H_
#define _HUMAN_H_

class Human
{
public:
    void SetHuman(char *p, int sage);
    int GetAge() const;
    const char * GetName()const;
    
protected:
    
private:
    char name[20];
    int age;
};

#endif

>>Student.h<<

#if ! defined _STUDENT_H
#define _STUDENT_H_

#include "Human.h"
class Student:public Human
{
public:
    void SetStudent(char *_name, int _age, int hak, int jum);
    void ShowStudent() const ;
    
protected:
private:
    int Hakbun;
    int jumsu;
};

#endif

>>main.cpp<<

#include <iostream>
#include "Human.h"
#include "Student.h"


int main()
{
    Student s1,s2,s3;
    s1.SetStudent("김수현", 25, 1111, 100);
    s2.SetStudent("장동건", 27, 1112, 90);
    s3.SetStudent("김태희", 32, 1113, 50);

    s1.ShowStudent();
    s2.ShowStudent();
    s3.ShowStudent();
    return 0;
}

>>Human.cpp<<

#include <iostream>
#include <string.h>
#include "Human.h"

void Human::SetHuman(char *p, int sage)
{
    strcpy(name, p);
    age = sage;
}
int Human::GetAge()const
{
    return age;
}
const char * Human::GetName()const
{
    return name;
}

>>Student.cpp<<

#include "Student.h"
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

void Student::SetStudent(char *_name, int _age, int hak, int jum)
{
    SetHuman(_name, _age);
    Hakbun = hak;
    jumsu = jum;
}
void Student::ShowStudent()const
{
    cout << "이름 : " << GetName() << endl;
    cout << "나이 : " << GetAge() << endl ;
    cout << "학번 : " << Hakbun << endl;
    cout << "점수 : " << jumsu << endl << endl;
}


반응형

'과제모음' 카테고리의 다른 글

[C++]상속3  (0) 2010.01.22
[C++]Inheritance(상속2)  (0) 2010.01.22
[C++]복사생성자2  (0) 2010.01.22
[C++]복사생성자  (0) 2010.01.22
[C++]Const & this & Static 변수  (0) 2010.01.22