Virtual Functions

Sort by

recency

|

448 Discussions

|

  • + 0 comments

    class Person { public: virtual void getdata() = 0; virtual void putdata() = 0;

    protected:
    string name{};
    int age{};
    

    };

    class Professor : public Person {

    private: int publications{}; static int prof_seq_id; int prof_cur_id{};

    public: Professor() :prof_cur_id {prof_seq_id} { prof_seq_id++; }

    void getdata() override { cin >> name; cin >> age; cin >> publications; }

    void putdata() override { cout << name << " " << age << " " << publications << " " << prof_cur_id << endl; }

    };

    int Professor::prof_seq_id = 1;

    class Student : public Person {

    private: vector marks; int stud_cur_id {}; int sum; static int stud_seq_id;

    public:
    Student()
    :stud_cur_id{stud_seq_id}
    {
       stud_seq_id++;
    }
    
    void getdata() override
    {
       cin >> name;
       cin >> age;
    
       int num;
       for(int i = 0; i < 6; i++)
       {
        cin >> num;
        marks.emplace_back(num);
       }   
    }
    
    void putdata() override
    {
        sum = accumulate(marks.begin(), marks.end(), 0);
        cout << name << " " << age << " " << sum << " " << stud_cur_id << endl;
    }
    

    };

    int Student::stud_seq_id = 1;

  • + 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;
            }
    };
    
  • + 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;

  • + 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;

  • + 0 comments
    class Person{
        public:
            string name;
            int age;
        public:
            Person():name(""), age(0){}
            Person(string& name, int age):name(name), age(age){}
            virtual void getdata(){
                cin >> name >> age;
            }
            virtual void putdata(){
                cout << name << " " << age << " ";
            }
    };
    
    class Student: public Person{
        private:
            vector<int> arr;
            static int cur_id;
        public:
            Student():Person(), arr(6, 0){}
            Student(string&name, int age, vector<int>& arr, int cur_id):Person(name, age), arr(arr){}
            void getdata()
            {
                Person::getdata();
                for(int i = 0; i < arr.size(); i++)
                {
                    cin >> arr[i];
                }
            }
            void putdata()
            {
                Person::putdata();
                int total = 0;
                for(int i = 0; i < arr.size(); i++)
                {
                    total += arr[i];
                }
                cout << total << " " << ++cur_id << endl;
            }
    };
    
    class Professor: public Person{
        private:
            int publicatios;
            static int cur_id;
        public:
            Professor():Person(), publicatios(0){}
            Professor(string& name, int age, int publications, int cur_id):Person(name, age), publicatios(publications){}
            void getdata()
            {
                Person::getdata();
                cin >> publicatios;
            }
            void putdata()
            {
                Person::putdata();
                cout << publicatios << " " << ++cur_id << endl;
            }
        
    };
    int Professor::cur_id = 0;
    int Student::cur_id = 0;