Problem statement:
You are given two classes, Person and Student, where Person is the base class and Student is the derived class.
Your task is to complete the implementation of the Student class by inheriting from the Person class and adding an additional int member variable studentId.
You need to implement the Student class constructor and a void printPerson() function that prints the firstName, lastName, and id of the student.
Sample Input:
The input consists of three lines:
The first line contains the firstName of the student.
The second line contains the lastName of the student.
The third line contains the id of the student
Heraldo
Memelli
8135627
Sample Output:
The output should be printed to the console. Each line should contain one attribute of the student: firstName, lastName, and id.
Name: Memelli, Heraldo
ID: 8135627
Explanation:
In this example, we create a Student object with the given attributes. The printPerson() function is called, which prints the firstName and lastName in reverse order and the id of the student.
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string firstName;
string lastName;
int id;
public:
Person(string firstName, string lastName, int identification) {
this->firstName = firstName;
this->lastName = lastName;
this->id = identification;
}
void printPerson() {
cout << "Name: " << lastName << ", " << firstName << endl;
cout << "ID: " << id << endl;
}
};
class Student : public Person {
private:
int studentId;
public:
Student(string firstName, string lastName, int identification, int studentId)
: Person(firstName, lastName, identification) {
this->studentId = studentId;
}
void printPerson() {
Person::printPerson();
cout << "Grade: " << studentId << endl;
}
};
int main() {
string firstName, lastName;
int id, studentId;
cin >> firstName >> lastName >> id >> studentId;
Student* stu = new Student(firstName, lastName, id, studentId);
stu->printPerson();
return 0;
}
Code Explanation
The code starts by including the necessary header files, including
The Person class is defined with three protected member variables: firstName, lastName, and id. It also has a constructor that initializes these member variables.
The Person class has a printPerson() function that prints the firstName, lastName, and id of the person.
The Student class is defined as a derived class of Person using the public access specifier. It adds an additional private member variable studentId.
The Student class has a constructor that takes the firstName, lastName, id, and studentId as parameters. It calls the Person class constructor to initialize the base class member variables.
The Student class also overrides the printPerson() function of the Person class. It first calls the base class printPerson() function using Person::printPerson(), and then prints the studentId.
In the main() function, the input is read using cin and stored in the appropriate variables.
A new Student object is created using the provided input values.
The printPerson() function of the Student object is called, which prints the person's name, ID, and student ID.