Hackerrank-Virtual Functions Question
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Person{
protected:
int age;
string name;
public:
void virtual putdata(void)=0;
void virtual getdata(void)=0;
};
static int cur_id_1=1;
static int cur_id_2=1;
class Professor:public Person{
private:
int publications;
public:
void getdata()
{
cin>>name>>age>>publications;
}
void putdata()
{
cout<<name<<" "<<age<<" "<<publications<<" "<<cur_id_1<<endl;
cur_id_1++;
}
};
class Student:public Person{
private:
int marks[6],total=0;
public:
void getdata(void)
{
cin>>name>>age;
for(int i=0;i<6;i++)
{
cin>>marks[i];
}
}
void putdata(void)
{
cout<<name<<" "<<age<<" ";
for(int i=0;i<6;i++)
{
total+=marks[i];
}
cout<<total<<" "<<cur_id_2<<endl;
cur_id_2++;
}
};
int main(){
int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];
for(int i = 0;i < n;i++){
cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;
}
else per[i] = new Student; // Else the current object is of type Student
per[i]->getdata(); // Get the data from the user.
}
for(int i=0;i<n;i++)
per[i]->putdata(); // Print the required output for each object.
return 0;
}
Comments
Post a Comment