Virtual Functions

  • + 0 comments

    class Person {

    public: string name; int age;

    public:
    virtual void getdata() = 0;
    virtual void putdata() = 0;
    
    virtual ~Person() = default;
    

    };

    class Professor: public Person {

    int publication; static int cur_id;

    public:
    void getdata() override
    {
        cin >> name >> age >> publication;
    }
    void putdata() override
    {
        cout << name <<" "<< age << " "<< publication << " " << ++cur_id <<endl;
    }
    

    };

    class Student:public Person {

    int marks[6]; static int cur_id;

    public:
    void getdata() override
    {
        cin >> name >> age;
        for(int i = 0; i < 6; i++)
            {
                cin >> marks[i];
            }
    }
    
    void putdata() override
    {
    int total = 0;
        for(int i = 0; i < 6; i++)
        {
            total +=marks[i];
        }
        cout<<name <<" "<<age<<" " << total << " " << ++cur_id << endl;
    }
    

    };

    int Professor::cur_id = 0; int Student::cur_id = 0;