05-13 07:28
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] Example / Derived Class 본문

Programming/C&C++

[C/C++] Example / Derived Class

cinema4dr12 2014. 6. 12. 10:37

//
//  main.cpp
//  Test-002
//
//  Created by gchoi on 2014. 5. 6..
//  Copyright (c) 2014년 gchoi. All rights reserved.
//

#include <iostream>
#include <string>

using namespace std;

//////////////////////////////////////////////////
class Student {
private:
    int no;
    string name;
public:
    Student();
    Student(int no, string name);
    ~Student();
protected:
    void setNo(int no);
    void setName(string name);
    int getNo();
    string getName();
    void showData();
};

Student::Student() {
    this->no = 0;
    this->name = "";
}

Student::Student(int no, string name) {
    this->no = no;
    this->name = name;
}

Student::~Student() {
   
}

void Student::setNo(int no) {
    this->no = no;
}

void Student::setName(string name) {
    this->name = name;
}

int Student::getNo() {
    return this->no;
}

string Student::getName() {
    return this->name;
}

void Student::showData() {
    cout << "No : " << no << endl;
    cout << "Name : " << name << endl;
}



//////////////////////////////////////////////////
class Mathtest : public Student {
private:
    int math;
public:
    Mathtest();
    Mathtest(int no, string name, int math);
    void setNo(int no);
    void setName(string name);
    void setMath(int math);
    int getNo();
    string getName();
    int getMath();
    void showAllData();
};

Mathtest::Mathtest() {
    this->math = 0;
}

Mathtest::Mathtest(int no, string name, int math) : Student(no, name) {
    this->math = math;
}

void Mathtest::setNo(int no) {
    Student::setNo(no);
}

void Mathtest::setName(string name) {
    Student::setName(name);
}

int Mathtest::getNo() {
    return Student::getNo();
}

string Mathtest::getName() {
    return Student::getName();
}

void Mathtest::setMath(int math) {
    this->math = math;
}

int Mathtest::getMath() {
    return this->math;
}

void Mathtest::showAllData() {
    Student::showData();
    cout << "Math : " << math << endl;
}


//////////////////////////////////////////////////
int main(int argc, const char * argv[])
{
    Mathtest *gchoi = new Mathtest(15, "Geol Choi", 100);
    gchoi->setName("gchoi");
    gchoi->setNo(22);
   
    gchoi->showAllData();
    cout << gchoi->getNo() << endl;

    return 0;
}

Comments