Virtual Functions

Sort by

recency

|

446 Discussions

|

  • + 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;
    
  • + 0 comments
    class Person{
        protected:
            string name;
            int age;
        public:
            virtual void getdata(){};
            virtual void putdata(){};
    };
    
    class Professor : public Person{
        private:
            int publications;
            static int id;
            int cur_id;
        public:
            Professor(){
                cur_id=++id;
            }
            void getdata(){
                cin >> name >> age >> publications;
            }
            void putdata(){
                cout << name << " " << age << " " << publications << " " << cur_id << endl;
            }
    };
    
    int Professor::id=0;
    
    class Student : public Person{
        private:
            int marks[6];
            static int id;
            int cur_id;
            int sum;
        public:
            Student(){
                cur_id=++id;
                sum=0;
            }
            void getdata(){
                cin >> name >> age;
                for(int i=0;i<6;i++){
                    cin >> marks[i];
                }
            }
            void putdata(){
                for(int j=0;j<6;j++){
                    sum+=marks[j];
                }
                cout << name << " " << age << " " << sum << " " << cur_id << endl;
            }
    };
    
    int Student::id=0;
    
  • + 0 comments
    class Person {
    public:
        string name;
        int age;
        virtual void getdata() {
            cin >> this->name >> this->age;
        }
        virtual void putdata() {
            cout << this->name << " " << this->age << endl;
        }
    };
    class Professor : public Person {
    public:
        Professor() {
            this->cur_id = ++id;
        }
        int publications;
        static int id;
        int cur_id;
        void getdata() {
            cin >> this->name >> this->age >> this->publications;
        }
        void putdata() {
            cout << this->name << " "
                << this->age << " "
                << this->publications << " "
                << this->cur_id << endl;
        }
    };
    int Professor::id = 0;
    class Student : public Person {
    #define NUM_OF_MARKS 6
    public:
        Student() {
            this->cur_id = ++id;
        }
        int marks[NUM_OF_MARKS];
        static int id;
        int cur_id;
        void getdata() {
            cin >> this->name >> this->age;
            for (int i=0; i<NUM_OF_MARKS; i++) {
                cin >> marks[i];
            }
        }
        void putdata() {
            int marksSum = 0;
            for (int i=0; i<NUM_OF_MARKS; i++) {
                marksSum += marks[i];
            }
            cout << this->name << " "
                << this->age << " "
                << marksSum << " "
                << this->cur_id << endl;
        }
    };
    int Student::id = 0;