Đa hình (polymorphism) trong C++


#include<iostream>
using namespace std;
//tao base class person
class person
{
    public:
        int id;
        string name;
    void input();
    void output();
};
void person::input()
{
    cout<<"id:";
    cin>>id;
    cin.ignore();
    cout<<"Name:";
    getline(cin,name);
}
void person::output()
{
    cout<<id<<" "<<name<<endl;
}
//Tao sub-class student
class student:public person
{
    public:
        int studId;
        float GPA;
    void input()
    {
        //goi phuong thuc cua lop cha
        person::input();
        cout<<"student id:";
        cin>>this->studId;
        cout<<"GPA:";
        cin>>this->GPA;
    }
    void output()
    {
        //goi phuong thuc cua lop cha
        person::output();
        cout<<this->studId<<" "<<this->GPA<<endl;
    }
};
int main()
{
    student st;
    st.input();
    st.output();
    return 0;
}

Trương Đình Huy