Virtual Functions

  • + 0 comments
    //used static variables to track professors and studentents seprately
    static int count_professor = 0 , count_student = 0;
    
    class Person{
        protected:
            string name;
            int age , cur_id;
        public:
            virtual void getdata() = 0;
            virtual void putdata() = 0;
    };
    
    class Professor : public Person{
        private:
            int publications;
        public:
            void getdata(){
                cin>>name;
                cin>>age;
                cin>>publications;
                cur_id = ++(::count_professor);
            }
            void putdata(){
                cout<<name<<" "<<age<<" "<<publications<<" "<<cur_id<<endl;
            }
    };
    class Student : public Person{
        private:
            int marks;
        public:
            void getdata(){
                cin>>name;
                cin>>age;
                int temp;
                marks = 0;
                for(int i = 0; i < 6;i++){   
                    cin>>temp;
                    marks += temp;
                }
                cur_id = ++(::count_student);
            }
            void putdata(){
                cout<<name<<" "<<age<<" "<<marks<<" "<<cur_id<<endl;
            }
    };