Professional Documents
Culture Documents
Inheritance in C++
Inheritance in C++
Inheritance in C++
WHAT IS AN INHERTANCE?
The derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly
created derived class.
Dog is a mammal. It has all features of Cat is a mammal. It has all features of
mammals in addition to its own unique mammals in addition to its own unique
features features
FEATURES /ADVANTAGES OF
INHERITANCE
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance and easy
to extend
Capable of expressing the inheritance relationship
and its transitive nature which ensures closeness
with real world problems .
SYNTAX
To create a derived class from an already existing base class the syntax
is:
….
…..
};
ACCESS CONTROL AND INHERITENCE
A derived class can access all the protected and public members of its base class.
It can not access private members of the base class.
PUBLIC
BASE CLASS
PROTECTED
PRIVATE
CHILD CLASS
We can summarize the different access types according to who can access them in
the following way:
NOTE: Constructors and destructors of the base class are never inherited.
VISIBILTY MODES AND INHERITANCE
A child class can inherit base class in three ways. These are:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
SINGLE INHERITENCE
EXAMPLE COURSE
class student
{ class course : public student
private: {
char name[20]; long course_code;
float marks; char course_name;
protected: public:
void result(); course();
public: void commence();
student(); void cdetail();
void enroll(); }
void display();
}
MULTILEVEL INHERITENCE
SOFA
EXAMPLE
OFFICE
class furniture class sofa: public furniture class office: private sofa
{ { {
char type; int no_of_seats; int no_of_pieces;
char model[10]; float cost; char delivery_date[10];
public: public: public:
furniture(); void indata(); void readdetails()
void readdata(); void outdata(); void displaydetails();
void dispdata(); }; }
}
MULTIPLE INHERITENCE
EXAMPLE
COMPANY
class chaiperson
{ class company: private
class director
long chairid; chairperson, public director
{
char name[20]; {
long directorid;
protected: int companyid;
char dname[20];
char description[20]; char city[20];
public:
void allocate(); char country[20];
director();
public: public:
void entry();
chairperson(); void ctentry();
void display();
void assign(); void ctdisplay();
};
void show(); };
};
HIERARCHICAL INHERITENCE
class toys
{ class softtoys: public toys class electronictoys: public
char tcode[5]; { toys
protected: chat stname[20]; {
float price; float weight; char etname[20];
void assign(float); public: int no_of_batteries;
public: softtoys(); public:
toys(); void stentry(); void etentry();
void tentry(); void stdisplay(); void etdisplay();
void tdisplay(); }; };
};
HYBRID INHERITENCE
It combines two or more types of inheritance. In this type of
inheritance we can have a mixture of number of
inheritances.
CONSTRUCTORS AND DESTRUCTORS IN
BASE AND DERIVED CLASSES