Write input member functions for the student and grad_student classes that read input data for each data member of the respective classes.
The print() member function is implemented in both the student and grad_student classes as follows:
For grad_student::print() to invoke the student::print() function, the scope resolved identifier student::print() must be used. Otherwise, there will be an infinite loop. To see which versions of these functions get called, and to demonstrate some of the conversion relationships between base and publicly derived classes, we write a simple test program:
C++ Test pointer components
The diagram below details the elements of the C++ Test Pointer.
The C++ code demonstrates the principles of pointer conversion in C++ when dealing with inheritance.
Let's break it down, along with some corrections and explanations for clarity.
Key Points of the C++ Code:
Objects and Pointers:
student s is a base class object.
grad_student gs is a derived class object (assuming grad_student publicly inherits from student).
ps is a pointer to the base class student.
pgs is a pointer to the derived class grad_student.
Implicit Pointer Conversion:
A pointer to a derived class object can be implicitly converted to a pointer to its base class. This happens in the line:
ps = pgs = &gs;
Here, pgs (a pointer to grad_student) is assigned to ps (a pointer to student), which is allowed because grad_student publicly inherits student.
Polymorphism:
Virtual functions are essential for polymorphism. If student::print() is declared as virtual, then the derived class grad_student::print() will override it, and the appropriate function will be called depending on the actual type of the object that the pointer points to.
Corrected Code Example:
#include "student.h" // Ensure this contains relevant class definitions
int main() {
// Create base and derived class objects
student s("Mae Pohl", 100, 3.425, fresh);
grad_student gs("Morris Pohl", 200, 3.2564, grad, ta, "Pharmacy", "Retail Pharmacies");
// Pointers to base and derived objects
student* ps = &s;
grad_student* pgs = nullptr;
// Call base class print() using base class pointer
ps->print(); // Calls student::print
// Assign derived class pointer to base class pointer
ps = pgs = &gs;
// Call print() through base class pointer
ps->print(); // If virtual, calls grad_student::print; otherwise, student::print
// Call print() through derived class pointer
pgs->print(); // Calls grad_student::print
}
Assumptions and Recommendations:
Virtual Functions:
To achieve the correct polymorphic behavior, declare print() in the student class as virtual:
class student {
public:
virtual void print();
// Other members
};
Pointer Symbols:
Replace & with & and -> with ->. These appear to be artifacts of incorrectly parsed HTML entities.
Inheritance:
Ensure grad_student inherits publicly from student:
class grad_student : public student {
// Derived class-specific members
public:
void print() override; // Overrides student::print
};
Use override:
In C++11 and later, use the override specifier in derived class methods to ensure you're correctly overriding a base class method.
Output (if `print()` is virtual):
The first call (ps->print();) outputs the student class's print() function.
The second call (ps->print();) outputs the grad_student class's print() function (polymorphic behavior).
The third call (pgs->print();) directly outputs the grad_student class's print() function.
This function declares both objects and pointers to them. The conversion rule is that a pointer to a publicly derived class may be converted implicitly to a pointer to its base class. In our example, the pointer variable ps can point at objects of both classes,
but the pointer variable pgs can point only at objects of type grad_student. We wish to study how different pointer assignments affect the invocation of a version of print().
ps -> print();
This invokes student::print(). It is pointing at the object s of type student.
ps = pgs = &gs;
This multiple assignment statement has both pointers pointing at an object of type grad_student. The assignment to ps involves an implicit conversion.
ps -> print();
This again invokes student::print(). The fact that this pointer is pointing at a grad_student object gs is not relevant.
pgs -> print();
This invokes grad_student::print(). The variable pgs is of type pointer to grad_student and, when invoked with an object of this type, selects a member function from this class.
Input Member Function - Exercise
Click the Exercise link below to write input member functions for the student and grad_student classes that read input data for each data member of the respective classes. Input Member Function - Exercise