Answer to Question #218846 in C++ for Hemambar

Question #218846

Create a person class with name as data member provide four functions in getname() to get input from user and put name to display output write two virtual functions getdata() and outstanding() where both these functions takes no argument and getdata() returns no value but outstanding() returns Boolean value . Derive student and professor from person. student class should contain gpa as its float type and overrides the virtual functions appropriately . In professor class provide number of applications as data members and override the virtual functions appropriately . Write a main program to declare a array of pointers to person object and depending on the choice from user create either a student object or professor object and store it in person array for n persons and display the details along with whether the person is outstanding or not



1
Expert's answer
2021-07-24T07:02:50-0400
#include <iostream>

using namespace std;


class person {
private:
    string name;
public:
    void getname() {
        cout << "\nEnter name: ";
        cin >> name;
    }

    void putname() {
        cout << "\nName is : " << name << endl;
    }

    virtual void getdata() {

    }

    virtual bool outstanding() {

    }
};

class student : public person {
private:
    float gpa;
public:
    void getdata() {
        cout << "\nEnter gpa:";
        cin >> gpa;
    }

    bool outstanding() {
        return true;
    }
};

class professor : public person {
private:
    int app_no;
public:
    void getdata() {
        cout << "\nEnter number of applications: ";
        cin >> app_no;
    }

    bool outstanding() {
        return true;
    }
};

int main() {
    int ch;
    cout << "1. Student\n2. Professor\n";
    cout << "\nEnter your choice: ";
    cin >> ch;

    if (ch == 1) {
        student s[5];
        for (int i = 0; i < 5; i++) {
            s[i].getname();
            s[i].putname();
            s[i].getdata();
            s[i].outstanding();
        }
    } else if (ch == 2) {
        professor p[5];
        for (int i = 0; i < 5; i++) {
            p[i].getname();
            p[i].putname();
            p[i].getdata();
            p[i].outstanding();
        }
    }
    return 0;
}
Example:
1. Student
2. Professor


Enter your choice: 1


Enter name: Azimjon


Name is : Azimjon


Enter gpa:2.3


Enter name: Sheroz


Name is : Sheroz


Enter gpa:3.3


Enter name: Bobur


Name is : Bobur


Enter gpa:4.0


Enter name: Abror


Name is : Abror


Enter gpa:3.8


Enter name: Kakashi


Name is : Kakashi


Enter gpa:3.5

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS