Chapter 9 Inheritance: Noor Alam Khan BS-CS University of Swat Pakistan
Chapter 9 Inheritance: Noor Alam Khan BS-CS University of Swat Pakistan
Chapter 9 Inheritance: Noor Alam Khan BS-CS University of Swat Pakistan
Inheritance
Second most important feature of OOP The code of existing classes is used for making new classes. Save time for writing and debugging.
Inheritance
To inherit means to receive. It is the process by which new class can be created form the existing class. The new class is written in such a way that it can access or use the members of an existing class. This new class is called derived or child class and the existing class is called base or parent class.
Inheritance (cont..)
The base class is once defined and then it is reused by several derived classes. So in this way one have to define only the derived classes again and again. The derived class can access data member and member function of the base class. It can also have its own data member and member functions.
Inheritance (cont..)
The figure show that the relationship b/w the base class and derived class. The arrow show that the derived class can access the members of the base class but the converse is not true.
Types of Inheritance
Based on the number of base classes from which a class is derived, there are two types of inheritance. Single inheritance:
The new class is derived from only one base class.
Multiple Inheritance:
The new class is derived from more then one base class.
Access Specifier
It is also called member access specifier. It is used to determine whether the member of the class can be accessed from outside the class or not. Public members of the base class are accessible by all function program. Private members of the base class are accessible only by member function and friend of the base class.
Access Specifier(cont.)
Protected member of the base class is accessible by member and friend of the derived class. This means that protected members ate public for the derived class but for the rest of the program these are private.
Example 9-01
#include<iostream.h> #include<conio.h> class student { private: char name[15],address[15]; public: void input(void) { cout<<"Enter name"<<endl; cin>>name; cout<<"Enter address"<<endl; cin>>address; } void show(void) { clrscr(); cout<<"Name = "<<name<<endl; cout<<"Address = "<<address<<endl; } };
Example 9-01(cont)
class marks: public student { private: int s1,s2,s3,total; public: void input_marks(void) { cout<<"Enter marks of sub 1"<<endl; cin>>s1; cout<<"Enter marks of sub 2"<<endl; cin>>s2; cout<<"Enter marks of sub 3"<<endl; cin>>s3; total=s1+s2+s3; } void show_detail(void); };
Example 9-01(cont)
void main() { marks mmm;// object the derived class access the functions of the base class. clrscr(); mmm.input(); mmm.input_marks(); mmm.show_detail(); getch(); } void marks::show_detail() { show(); cout<<"Marks of 1st sub: "<<s1<<endl; cout<<"Marks of 2nd sub: "<<s2<<endl; cout<<"Marks of 3rd sub: "<<s3<<endl; cout<<"Total marks : "<<total<<endl; }
Types of inheritance
Three types based on the access Specifier when a derived class inherits from a base class, the access specifiers may change depending on the method of inheritance.
Public Private Protected
Types of inheritance(cont)
// Inherit from Base publicly class Pub: public Base { }; // Inherit from Base privately class Pri: private Base { }; // Inherit from Base protectedly class Pro: protected Base { };
class Def: Base // Defaults to private if you do not choose an inheritance type, C++ defaults to private inheritance { };
Public inheritance
Public inheritance is by far the most commonly used type of inheritance. In fact, very rarely will you use the other types of inheritance, so your primary focus should be on understanding this section. Fortunately, public inheritance is also the easiest to understand. When you inherit a base class publicly, all members keep their original access specifications. Private members stay private, protected members stay protected, and public members stay public.
Public inheritance
This is fairly straightforward. The things worth noting are:
Derived classes can not directly access private members of the base class. The protected access specifier allows derived classes to directly access members of the base class while not exposing those members to the public. The derived class uses access specifiers from the base class. The outside uses access specifiers from the derived class.
Example 09-02
#include<iostream.h> #include<conio.h> class A { private: int a1,a2; protected: int pa1,pa2; public: void ppp(void) { cout<<"Value of pa1 of class A = "<<pa1<<endl; cout<<"Value of pa2 of class A = "<<pa2<<endl; } };
Example 09-02
class B: public A { public: void get(void) { cout<<"Enter value pa1"<<endl; cin>>pa1; cout<<"Enter vlaue pa2"<<endl; cin>>pa2; } };
Example 09-02
void main() { B ob; clrscr(); ob.get(); ob.ppp(); getch(); }
Private inheritance
private inheritance, all members from the base class are inherited as private. This means private members stay private, and protected members are the only accessible members.
Example 09-03
#include<iostream.h> #include<conio.h> class A { private: int a1,a2; protected: int pa1,pa2; public: void ppp(void) { cout<<"Value of pa1 of class A = "<<pa1<<endl; cout<<"Value of pa2 of class A = "<<pa2<<endl; } };
Example
class B: private A { public: void get(void) { cout<<"Enter value pa1"<<endl; cin>>pa1; cout<<"Enter vlaue pa2"<<endl; cin>>pa2; cout<<"Value of pa1 of class A = "<<pa1<<endl; cout<<"Value of pa2 of class A = "<<pa2<<endl; } };
Example
void main() { B ob;clrscr(); ob.get(); getch(); }
Protected inheritance
Protected inheritance is the last method of inheritance. It is almost never used, except in very particular cases. With protected inheritance, the protected members are only accessible , and public and private members are not accessible.
Example 09-04
#include<iostream.h> #include<conio.h> class A { private: int a1,a2; protected: int pa1,pa2; public: void ppp(void) { cout<<"Value of pa1 of class A = "<<pa1<<endl; cout<<"Value of pa2 of class A = "<<pa2<<endl; } };
Example 09-04
class B: protected A { public: void get(void) { cout<<"Enter value pa1"<<endl; cin>>pa1; cout<<"Enter vlaue pa2"<<endl; cin>>pa2; cout<<"Value of pa1 of class A = "<<pa1<<endl; cout<<"Value of pa2 of class A = "<<pa2<<endl; } }; void main() { B ob;clrscr(); ob.get(); getch(); }
Summary
The way that the access specifiers, inheritance types, and derived classes interact causes a lot of confusion. To try and clarify things as much as possible: First, the base class sets its access specifiers. The base class can always access its own members. The access specifiers only affect whether outsiders and derived classes can access those members. Second, derived classes have access to base class members based on the access specifiers of the immediate parent. The way a derived class accesses inherited members is not affected by the inheritance method used! Finally, derived classes can change the access type of inherited members based on the inheritance method used. This does not affect the derived classes own members, which have their own access specifiers. It only affects whether outsiders and classes derived from the derived class can access those inherited members.
Summary Example
class Base { public: int m_nPublic; private: int m_nPrivate; protected: int m_nProtected; }; Base can access its own members without restriction. The public can only access m_nPublic. Derived classes can access m_nPublic and m_nProtected.
Summary Example(cont)
class D2: private Base { public: int m_nPublic2; private: int m_nPrivate2; protected: int m_nProtected2; } D2 can access its own members without restriction. D2 can access Bases members based on Bases access specifiers. Thus, it can access m_nPublic and m_nProtected, but not m_nPrivate. Because D2 inherited Base privately, m_nPublic, m_nPrivate, and m_nProtected are now private when accessed through D2. This means the public can not access any of these variables when using a D2 object, nor can any classes derived from D2.
Summary Example(cont)
class D3: public D2 { public: int m_nPublic3; private: int m_nPrivate3; protected: int m_nProtected3; };
Summary Example(cont)
D3 can access its own members without restriction. D3 can access D2s members based on D2s access specifiers. Thus, D3 has access to m_nPublic2 and m_nProtected2, but not m_nPrivate2. D3 access to Base members is controlled by the access specifier of its immediate parent. This means D3 does not have access to any of Bases members because they all became private when D2 inherited them.
Example 09-05
#include<iostream.h> #include<conio.h> class BB { public: BB(void) { cout<<"Constructor of Base Class"<<endl; } }; class DD: public BB { public: DD(void) { cout<<"Constructor of Derived Class"<<endl; } }; void main() { DD abc; getch(); }
Multiple Inheritance
In multiple inheritance, a class is derived by using more than one classes. The derived class receives the members of two or more base classes. The programmer uses the existing base classes in the program coding to solve problems.
Multiple Inheritance?
Base Class A
Base Class B
Derived Class C
Multiple Inheritance?
General syntax: class sub_class: specifier1 b_class1, specifier2 b_class2, ..
{
----------members of derived class ----------};
example
class student { private: char name[15], address[15]; public: void input(); void print(); }; class marks { private: int s1,s2, s3, total; public: void inputmarks();
void showmarks();
Example
#include< iostream.h> #include< conio. h > #include< string. h > class student { private: char name[15], address[15]; public: void input() { cout<<"Enter name ?"; cin>>name; cout<<"Enter address ?"; cin>>address; } void print() { cout<<"Name : "<<name<<endl; cout<<"Address : "<<address<<endl; } };
class marks { private: int s1, s2, s3, total; public: void inputmarks() { cout<<"Enter marks of sub1 ?"; cin>>s1; cout<<"Enter marks of sub2 ?"; cin>>s2; cout<<"Enter marks of sub3 ?"; cin>>s3; total= s1+s2+s3; } void showmarks() { cout<<"Marks of 1st sub: "<<s1<<endl; cout<<"marks of 2nd sub: "<<s2<<endl; cout<<"marks of 3rd sub: "<<s3<<endl; cout<<"total marks : "<<total<<endl; } };
class show: public student, public marks { public: show_rec() { cout<<"\n the complete record is : "<<endl; cout<<"=========================="<<endl; print(); showmarks(); } }; void main() { show mo; clrscr(); mo.input(); mo.inputmarks(); mo.show_rec(); getch(); }
Thank you
Noor Alam Khan BS-CS University of Swat Pakistan. [email protected] www.facebook.com/kingluckynoor