Programming and Graphics

(Kiana) #1

6.16 Class templates 193


student * pntR = &R;
student * pntN = &N;

pntR->payment();
pntN->payment();

return 0;
}

Running this program prints on the screen:


Resident tuition: 2567.65
Non-resident tuition: 4879.99
Resident tuition: 2567.65
Non-resident tuition: 4879.99

Note that pointers of residents and non-residents are defined on the base student
class. Thanks to the virtual function declaration, these pointers assume a proper
identity when interacting with functions of their respective derived class.


Problems


6.15.1. Add to the student, resident, and nonresident classes constructors to
specify the student names.


6.15.2. Add to the student, resident, and nonresident classes a second virtual
function of your choice.


6.16 Class templates............................


In Section 4.10, we defined function templates with the objective of consolidat-
ing code. If a function operates on integers and the same function operates on
real numbers, we can consolidate the two functions into a template that does
both. Class templates are designed with a similar goal in mind.


In Section 6.11, we discussed the class of points in thexyplane. The
following code generalizes this class into a template. The class definition is:


/*--------------------------------------------------
This program illustrates the use of class templates
-------------------------------------------------*/

#include <iostream>
using namespace std;
Free download pdf