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
#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;
}
Comments
Leave a comment