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.
// class Person --> Main class classPerson{public:intage;stringname;virtualvoidgetdata()=0;// actually pure virtual functions virtualvoidputdata()=0;};// class ProfessorclassProfessor:publicPerson{public:intpublications;staticintcur_id;// inttemp_id;// for storing cur_id and assigning it then ++ //constructor to ++ the cur idProfessor(){temp_id=++cur_id;}voidgetdata();voidputdata();};intProfessor::cur_id=0;// basically check the input and output and try to understan what's going on , then you can understand that " 1/2 is for either professor/student , but in o/p cur_id will also 1/2 is for because we have to initialize a static var. for each class starting from 0 , and will be ++ as per input -----> for all this we have to use a constructor which will automatically ++ that , if that class object is created . "voidProfessor::getdata(){cin>>name>>age>>publications;}// voidProfessor::putdata(){cout<<name<<" "<<age<<" "<<publications<<" "<<temp_id<<endl;}// NOW STUDENT CLASSclassStudent:publicPerson{public:intmarks[6];staticintcur_id;inttemp_id;// same explanation check upper Student(){temp_id=++cur_id;}voidgetdata();voidputdata();};intStudent::cur_id=0;// voidStudent::getdata(){//cout<<"taking";cin>>name>>age;for(inti=0;i<6;i++){cin>>marks[i];}}// voidStudent::putdata(){inttotal_sum=0;for(inti=0;i<6;i++){total_sum=total_sum+marks[i];}cout<<name<<" "<<age<<" "<<total_sum<<" "<<temp_id<<endl;}
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 →