Problem statement:
You are given a partially implemented class Person and two derived classes Professor and Student.
Your task is to complete the implementation by adding virtual functions and implementing method overriding.
The Person class has a few member variables and a pure virtual function getdata() which is overridden in the derived classes.
Your goal is to implement the derived classes and their respective getdata() functions according to the given specifications.
Sample Input:
The input consists of several lines. The first line contains an integer N, which represents the number of objects to be created. This is followed by N lines, each containing information about a Person, Professor, or Student object.
For a Person object, the line contains the person's name and age separated by a space.
For a Professor object, the line contains the professor's name, age, publications, and the current id (separated by spaces).
For a Student object, the line contains the student's name, age, and an array of N integers representing the marks of the student.
4
Person Sam 18
Student John 15 100 90 80
Professor Mike 45 10
Student Bob 20 75 80 85
Sample Output:
For each object, print the corresponding information in a specific format as described in the problem statement.
Person: Sam, 18
Student: John, 15, Average = 90
Professor: Mike, 45, Publications = 10
Student: Bob, 20, Average = 80
Explanation:
In this example, we have four objects to be created:
Person: The name is "Sam" and the age is 18.
Student: The name is "John", the age is 15, and the marks are 100, 90, and 80. The average mark is (100+90+80)/3 = 90.
Professor: The name is "Mike", the age is 45, and the number of publications is 10.
Student: The name is "Bob", the age is 20, and the marks are 75, 80, and 85. The average mark is (75+80+85)/3 = 80.
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int age;
public:
virtual void getdata() = 0;
virtual void putdata() = 0;
};
class Professor : public Person {
private:
int publications;
static int cur_id;
int id;
public:
void getdata() {
cin >> name >> age >> publications;
id = ++cur_id;
}
void putdata() {
cout << "Professor: " << name << ", " << age << ", Publications = " << publications << endl;
}
};
int Professor::cur_id = 0;
class Student : public Person {
private:
int marks[6];
static int cur_id;
int id;
public:
void getdata() {
cin >> name >> age;
for (int i = 0; i < 6; i++) {
cin >> marks[i];
}
id = ++cur_id;
}
void putdata() {
int sum = 0;
for (int i = 0; i < 6; i++) {
sum += marks[i];
}
cout << "Student: " << name << ", " << age << ", Average = " << sum / 6 << endl;
}
};
int Student::cur_id = 0;
int main() {
int n;
cin >> n;
Person* persons[n];
for (int i = 0; i < n; i++) {
int personType;
cin >> personType;
if (personType == 1) {
persons[i] = new Professor();
} else if (personType == 2) {
persons[i] = new Student();
}
persons[i]->getdata();
}
for (int i = 0; i < n; i++) {
persons[i]->putdata();
}
return 0;
}
Code Explanation
The code starts by defining the Person class as an abstract base class with two pure virtual functions: getdata() and putdata(). These functions will be overridden by the derived classes.
Next, the Professor class is defined, which inherits from the Person class. It adds an additional member variable publications and a static member variable cur_id to keep track of the professor's ID.
The getdata() function for the Professor class reads the input for name, age, and publications, and assigns them to the corresponding member variables. The id is incremented using the cur_id and assigned to the professor.
The putdata() function for the Professor class prints the professor's information in the specified format.
The Student class is defined similarly to the Professor class. It adds an array marks to store the student's marks and a static member variable cur_id to keep track of the student's ID.
The getdata() function for the Student class reads the input for name, age, and marks, and assigns them to the corresponding member variables. The id is incremented using the cur_id and assigned to the student.
The putdata() function for the Student class calculates the average mark by summing up the marks and dividing by the number of subjects. It then prints the student's information in the specified format.
In the main() function, the number of objects n is read from input.
An array of Person pointers persons is created to store the objects.
A loop is used to iterate n times to create objects based on the given input type. If the type is 1, a Professor object is created. If the type is 2, a Student object is created.
The getdata() function is called for each object to read the respective input data.
Another loop is used to call the putdata() function for each object to print their information.
The program ends by returning 0.
Example Input:
4
1
Mike 45 10
2
John 15 90 85 80 95 88 92
1
Jane 35 15
2
Bob 20 78 85 90 92 88 95
Example Output:
Professor: Mike, 45, Publications = 10
Student: John, 15, Average = 88
Professor: Jane, 35, Publications = 15
Student: Bob, 20, Average = 87