بسم الله الرحمن الرحيم:
many people ask about Inheritance, actually, it is avery nice property that c++ offer for users.
Inheritance is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class.
In which we have a parents and a child class.
The class being inherited from is the Base Class (Superclass).
The class that inherits is the Derived Class (Subclass).
The derived class is specialized by adding properties specific to it.
for example if we have this parent class:
class Time
{
public:
void Set ( int hours, int minutes, int seconds);
void Increment ();
void Write () const;
Time ( int initHrs, int initMins, int initSecs);
// Constructor
Time (); // Default constructor
private:
int hrs;
int mins;
int secs;
};
our child class will be as follows:
#include “time.h”
enum ZoneType{EST, CST, MST, PST, EDT, CDT, MDT, PDT};
class ExtTime : public Time // Time is the base class
{
public:
void Set(int hours, int minutes, int seconds, ZoneType timeZone);
void Write () const;
ExtTime ( int initHrs, int initMins, int initSecs, ZoneType initZone);
ExtTime ();
private:
ZoneType zone; // Additional data member
};رجاء تنسيق الكود
in this example we can see how Inheritance can be used, actually it is used in big programs , where there are always many changes, so in such a big programs we have to minimize the number of setps to change things in our program.