9.6 FUNCTION OVERRIDING

In Program 9.1 (inher1.cpp), you will notice that we are displaying the time in haphazard manner. Time was displayed using the method show() of the base class Time0 and AM or PM was shown by the code “cout << t.strg << endl;”. Why not to write a method in class clock. Yes, it will be a good idea. What name should we give? show()?, but that name is already in use in class time0, the base class. Here, C++ says that you can use the same name in the derived class.

Consider the code as

class Time0  { protected:        int hr;        int min;        int sec;    public :        void settime(int h, int m, int s);        void show();  };class Clock : public Time0  { private :       char strg[5]; // for AM PM    public :        void ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.