We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>usingnamespacestd;classPerson{protected:intage;stringname;public:virtualvoidgetdata()=0;virtualvoidputdata()=0;};classProfessor:publicPerson{private:intpublications,cur_id;staticintpp;public:voidgetdata(){cin>>name>>age>>publications;cur_id=++pp;}voidputdata(){cout<<name<<" "<<age<<" "<<publications<<" "<<cur_id<<endl;}};intProfessor::pp=0;classStudent:publicPerson{private:intArr[6];staticintcount;intcur_id;public:voidgetdata(){cin>>name>>age;for(intx=0;x<6;x++)cin>>Arr[x];cur_id=++count;}voidputdata(){intsum=0;for(intx=0;x<6;sum+=Arr[x],x++);cout<<name<<" "<<age<<" "<<sum<<" "<<cur_id<<endl;}};intStudent::count=0;intmain(){intn,val;cin>>n;//The number of objects that is going to be created.Person*per[n];for(inti=0;i<n;i++){cin>>val;if(val==1){// If val is 1 current object is of type Professorper[i]=newProfessor;}elseper[i]=newStudent;// Else the current object is of type Studentper[i]->getdata();// Get the data from the user.}for(inti=0;i<n;i++)per[i]->putdata();// Print the required output for each object.return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Virtual Functions
You are viewing a single comment's thread. Return to all comments →