Sei sulla pagina 1di 45

Inheritance

Silicon Institute of Technology

Inheritance
by
Shivani Nanda,
Silicon Institute of Technology,
Patia Hills, Bhubaneswar-751024

Shivani Nanda 1
Inheritance
Inheritance
Silicon Institute of Technology

Inheritance is the process of creating new classes


from existing classes
class derivedclass_name : access baseclass_name
{ //body };

Access specifier must be either


public, private or protected
Default access-specifier of class : private

Inheritance: Single Inheritance


<public, private, protected>

Shivani Nanda 2
Inheritance
Access Specifier
Silicon Institute of Technology

Public Protected Private


inheritance inheritance inheritance
Public Public in Protected in Private in
member derived derived derived

Protected Protected in Protected in Private in


member derived derived derived

Private Hidden in Hidden in Hidden in


member derived derived derived

Shivani Nanda 3
Inheritance
public inheritance
Silicon Institute of Technology

class A{
private : int x;
public : int y;
protected: int z;
};
class B: public A
{ //body
// here x is inaccessible, y is public, z is protected
};

Shivani Nanda 4
Inheritance
Single Inheritance : Public Mode
Silicon Institute of Technology

class Base { cout<<d;


{ private: int i, j; k = 55;
protected : int k; cout<<i<<j<<k<<l
public: int l; } };
void set(int a, int b,int c,int d) int main()
{ i = a; j = b; k = c; l=d; } { Derived ob(5);
void showbase() ob.set(1,2,3,4);
{ cout<<i<<j<<k<<l; }}; ob.showbase();
class Derived : public Base ob.i = 22;
{ private: int d; ob.k = 33;
public: Derived(int x) ob.l = 44;
{ d = x; } ob.showderived();
void showderived() return 0;
}
Shivani Nanda 5
Inheritance
private inheritance
Silicon Institute of Technology

class A{
private : int x;
public : int y;
protected: int z;
};
class B: private A
{ //body
// here y and z are private
};

Shivani Nanda 6
Inheritance
Single Inheritance : Private Mode
Silicon Institute of Technology

class Base set(1,2,3,4);


{ private: int i, j; show();
protected : int k; k = 44; l = 55;
public: int l; cout<<l<<k; } };
void set(int a, int b,int c,int d) int main()
{ i = a; j = b; k = c; l=d; } {
void show() Derived ob(5);
{ cout<<i<<j<<k<<l; }}; ob.set(1,2,3,4);
class Derived : private Base ob.show();
{ private: int d; ob.showk();
public: Derived(int x) ob.l = 100;
{ d = x; } ob.k = 200;
void showk() return 0;
{ cout<<d; }
Shivani Nanda 7
Inheritance
Granting Access
Silicon Institute of Technology

To restore the inherited members to their original


access specification:
class Base{
public: int j;
//.
};
class derived: private Base{
public: //other declaration
Base:: j; //Restoring original access specification

private: //.
protected: //
};

Shivani Nanda 8
Inheritance
Silicon Institute of Technology

class Base int a; };


{ private: int i; // private to base int main()
public: {
int j, k; Derived ob;
void seti(int x) ob.i = 10;
{ i = x; } ob.j = 20;
int geti() ob.k = 30;
{ return i; } }; ob.a = 40;
class Derived : private Base { ob.seti(10);
public: cout<<ob.geti()<<"
Base :: j; <<ob.j<<" "<<ob.a;
Base :: seti; return 0;
Base :: j; }

Shivani Nanda 9
Inheritance
protected inheritance
Silicon Institute of Technology

class A{
private : int x;
public : int y;
protected: int z;
};
class B: protetced A{
//body
// here x is inaccessible,
y and z are protected
};

Shivani Nanda 10
Inheritance
Single Inheritance : Protected Mode
Silicon Institute of Technology

class Base showij(); } };


{ private: int a; int main()
protected: int i,j; { Base ob1;
public: void setij(int a,int b) ob1.i=3;
{ i = a; j = b; } cout<<ob1.i;
void showij() ob1.setij(1,2);
{ cout<<i<< <<j; }}; ob1.showij();
class Derived : protected Base { Derived ob2;
private: int k; ob2.setij(1,2);
public: void setk( ) ob2.showij();
{ setij(10,20); ob2.setk();
k = i * j; } ob2.showall();
void showall() return 0;
{ cout<<k<<" "; }
Shivani Nanda 11
Inheritance
Multiple Inheritance
Silicon Institute of Technology

When a derived class inherits from more than


one base class simultaneously, it is referred to
as multiple inheritance

In multiple inheritance, the derived class


inherits the members of all its base classes and
can directly access the public and protected
members of its base class<if inherited in
public mode>

Shivani Nanda 12
Inheritance
class person
Silicon Institute of Technology

{ int ssn, age;


char name[20],address[30];
public: void getval(int sn, int ag, char st[],char add[])
{ ssn = sn;
age = ag;
strcpy(name,st);
strcpy(address,add);}
void show_person();};
void person::show_person()
{ cout<<" Serial Number : "<<ssn<<endl;
cout<<" Name : "<<name<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Address : "<<address<<endl; }
Shivani Nanda 13
Inheritance
class company
Silicon Institute of Technology

{ char cname[20], caddress[30];


public: void setval(char cn[],char cadd[])
{ strcpy(cname,cn);
strcpy(caddress,cadd);}
void show_company(); };
void company::show_company()
{ cout<<" Company Name : "<<cname<<endl;
cout<<" Company Address : "<<caddress<<endl; }
class owner : public person, public company
{ int licenceno;
public: void getlicenceno(int lno)
{ licenceno = lno; }

Shivani Nanda 14
Inheritance
Silicon Institute of Technology

void show_licenceno()
{cout<<" Licence Number: "<<licenceno<<endl; } };
int main()
{ owner o1;
o1.getval(111,33,"Suzanne wesley","R.K.Puram");
o1.setval("MS Vision","Connaught Place");
o1.getlicenceno(667677);
o1.show_person();
o1.show_company();
o1.show_licenceno();
return 0;Nanda
Shivani } 15
Inheritance
Ambiguity in Multiple Inheritance
Silicon Institute of Technology

In multiple inheritance, an ambiguity may


arise when two or more base classes have a
member with same name.

In order to resolve ambiguities, the name of


the member is qualified with the name of the
base class by using the scope resolution
operator( :: )

Shivani Nanda 16
Inheritance
Ambiguity in Multiple Inheritance : Example
Silicon Institute of Technology

class bank{
protected: int code;char bname[15],baddress[25];
public: void getdetail(int c,char bn[],char badd[])
{ code = c;
strcpy(bname,bn);
strcpy(baddress,badd);} };
class person{ protected:int code, age;
char name[20],address[30];
public: void getval(int sn, int ag, char st[],char add[])
{ code = sn;
age = ag;
strcpy(name,st);
strcpy(address,add);} };
Shivani Nanda 17
Inheritance
class owner : public bank, public person
Silicon Institute of Technology

{int regno;
public: void getregno(int regnum)
{regno = regnum;}
void show_regno(); };
void owner::show_regno()
{ cout<<" Serial Number : "<<person::code<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Name : "<<name<<endl;
cout<<" Bank Code : "<<bank::code<<endl;
cout<<" Bank Name : "<<bname<<endl;
cout<<" Bank Address : "<<baddress<<endl;
cout<<" Registeration Number : "<<regno<<endl; }

Shivani Nanda 18
Inheritance
Silicon Institute of Technology

int main()
{
owner o1;
o1.getdetail(1345,"Bank of Baroda","Anand Vihar");
o1.getval(112,27,"Himanshu Ahuja","Anand Vihar");
o1.getregno(334224);
cout<<endl<<endl;
o1.show_regno();
cout<<endl<<endl;
return 0;
}

Shivani Nanda 19
Inheritance
Multilevel Inheritance
Silicon Institute of Technology

When one class is inherited from another class,


which in turn is inherited from some other
class, it is referred to as multilevel inheritance
Multilevel comprises two or more levels
person indirect base class

employee direct base class

faculty

Shivani Nanda 20
Inheritance
Multilevel Inheritance : Transitive Nature
Silicon Institute of Technology

The derived class is inherited from the direct


base class which in turn is derived from
another base class
Hence, the derived class has all the members
of its direct base class as well as its indirect
base class
This is known as transitive nature of
multilevel inheritance

Shivani Nanda 21
Inheritance
Multilevel Inheritance : Example
Silicon Institute of Technology

class person{ protected: int ssn, age;


char name[20],address[30];
public: void getval(int sn, int ag, char st[],char add[])
{ ssn = sn;
age = ag;
strcpy(name,st);
strcpy(address,add);}
void show_person(); };
void person::show_person()
{ cout<<" Serial Number : "<<ssn<<endl;
cout<<" Name : "<<name<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Address : "<<address<<endl; }

Shivani Nanda 22
Inheritance
Silicon Institute of Technology

class employee : public person


{ protected : int salary;
public: void getsal(int sal)
{salary = sal;} };
class faculty : public employee
{protected : char subject[20];
public: void getfaculty(char sub[])
{strcpy(subject,sub);}
void showfaculty(void); };
void faculty::showfaculty()
{ show_person();
cout<<" Salary : "<<salary<<endl;
cout<<" Subject Teaching : "<<subject<<endl; }

Shivani Nanda 23
Inheritance
Silicon Institute of Technology

int main()
{
faculty f1;
f1.getval(112,27,"Shantilal Garodia","Nehru Place");
f1.getsal(30000);
f1.getfaculty("Pschology");
cout<<endl<<endl;
f1.showfaculty();
cout<<endl<<endl;
return 0;
}

Shivani Nanda 24
Inheritance
Hierarchical Inheritance
Silicon Institute of Technology

In this type of inheritance more than one base


class is derived from a single base class
In hierarchical inheritance, a base class provides
members that are common to all of its derived
classes
student

graduate undergraduate

gradstu gradfac ugradstu ugradfac

Shivani Nanda 25
Inheritance
Hierarchical Inheritance
Silicon Institute of Technology

In hierarchical inheritance, each of the derived


class inherits all the members of its base class

In addition, all the derived classes can directly


access the public members of the base class

However, one derived class cannot access the


members of another derived class

Shivani Nanda 26
Inheritance
Hybrid Inheritance
Silicon Institute of Technology

Hybrid inheritance is said to be implemented


when different forms of inheritance are
combined to realize the relationships between
multiple classes

In other words when a class is derived


involving two or more forms of inheritance

Shivani Nanda 27
Inheritance
Hybrid Inheritance : Example
Silicon Institute of Technology

class student{ void get_marks(float a,


protected: int roll_no; float b,float c)
public: { sub1=a;
void get_roll(int a) sub2=b;
{ roll_no=a; } sub3=c; }
void put_roll(void) void put_marks(void)
{cout<<"Roll_no: {
<<roll_no } }; cout<<"Sub1="<<sub1;
class test : public student{ cout<<"Sub2="<<sub2;
protected: float sub1; cout<<"Sub3="<<sub3;
float sub2, sub3; }};
public:

Shivani Nanda 28
Inheritance
Silicon Institute of Technology

class sports put_roll();


{ protected: float score; put_marks();
public: put_score();
void get_score(float s) cout<<"Total="<<total;}};
{ score=s; } int main()
void put_score(void) { result s1;
{ cout<<"Score"<<score;}} s1.get_roll(501);
class result:public test,public sports s1.get_marks(60,75.7,89.34);
{ float total; public: s1.get_score(8.56);
void display(void) s1.display();
{ return 0;
total=sub1+sub2+sub3+score; }

Shivani Nanda 29
Inheritance
Multipath Inheritance : A form of Hybrid Inheritance
Silicon Institute of Technology

When a class is derived from two or more


classes that in turn are derived from the same
base class

In this form of hybrid inheritance the derived


class can attempt to directly access members
of its indirect base class

Shivani Nanda 30
Inheritance
Multipath Inheritance : Example
Silicon Institute of Technology

class A class D : public B,public C


{ protected: { protected:
int data; }; int data3;
class B : public A public: int getdata()
{ protected: { return data; }};
int data1; }; int main()
class C : public A { D obj1;
{ protected: cout<<"Data
<<obj1.getdata( ); }
int data2; };

Shivani Nanda 31
Inheritance
Multipath Inheritance : Ambiguity
Silicon Institute of Technology

In multipath inheritance the members of the indirect


base class are duplicated in the derived class through
the immediate base classes which lie in the
intermediate stage
person Indirect base class

employee student

apprentice Derived class

Shivani Nanda 32
Inheritance
Ambiguity resolution in multipath inheritance
Silicon Institute of Technology

One way to resolve the ambiguity by qualifying the


member with the name of either of the direct base
classes
The scope resolution operator is used to qualify the
member with the class name
class D : public B,public C
{ protected:
int data3;
public: int getdata()
{ return B::data; } };
Shivani Nanda 33
Inheritance
Virtual Base Classes
Silicon Institute of Technology

The scope resolution operator when used in


multipath inheritance enables reference to specific
class to access the members, however, it doe not
prevent the duplication of the indirect base class
members

The duplication of inherited members due to multiple


paths can be avoided by making the common base
class as virtual base class while declaring the
intermediate base class

Shivani Nanda 34
Inheritance
Virtual Base Classes
Silicon Institute of Technology

Virtual base class is an indirect base class declared


using access specifier virtual in order to prevent its
duplication

Specifying a base class as virtual ascertains that only


one copy of the base class members exists for its
derived classes

So a virtual base class can be derived several times


without its duplication

Shivani Nanda 35
Inheritance
Silicon Institute of Technology

class person{ protected: int ssn, age;


char name[20],address[30];
public: void getper(int sn, int ag, char st[],char add[])
{ ssn = sn;
age = ag;
strcpy(name,st);
strcpy(address,add);}
void show_person(); };
void person::show_person()
{ cout<<" Serial Number : "<<ssn<<endl;
cout<<" Name : "<<name<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Address : "<<address<<endl; }
Shivani Nanda 36
Inheritance
Silicon Institute of Technology

class employee : public virtual person


{ protected : int salary;
public: void getsal(int sal)
{salary = sal;}
void showsal(){ cout<<Salary : <<salary;} };

class student : public virtual person


{protected : char majordept[20];
public: void getmaj(char maj[])
{strcpy(majordept,maj);}
void showmaj() { cout<<Major : <<majordept;} };

Shivani Nanda 37
Inheritance
Silicon Institute of Technology

class employee : public employee, public student


{ protected : int duration;
public: void getdur(int d) {duration = d;}
void showdur(){ cout<<Duration :
<<duration<<years;} };
int main()
{ apprentice a1;
a1.getper(112,27,"Shantilal Garodia","Nehru Place,M);
a1.getsal(30000); a1.getmaj("Pschology");
a1.getdur(4); a1.showper();
a1.showper(); a1.showsal(); a1.showmaj(); a1.showdur();
return 0;
}
Shivani Nanda 38
Inheritance
Constructors and Destructors in Inheritance
Silicon Institute of Technology

A constructor is required in inheritance to initialize


the members of the base class through the derived
class

A derived class constructor initializes its new


members but to initialize the base class part it
requires the base class constructors

But we know that constructors and destructors are


not inherited to the derived class

Shivani Nanda 39
Inheritance
Order of Calling of Constructor and Destructor
Silicon Institute of Technology

class wood{ protected: char type[10];


public: wood( ) { strcpy(type,Teak);
cout<<BaseClass constructor ;}
~wood( ) {cout<<BaseClass destructor ;}};
class table:public wood
{ char dimension[10];
public: table( ) { strcpy(diemension,2*4);
cout<<DerivedClass constructor ;}
~table( ) {cout<<DerivedClass destructor ;}};
int main( ){
table t1; }

Shivani Nanda 40
Inheritance
Constructors and Destructors in Multiple Inheritance
Silicon Institute of Technology

class bank{ public:


bank( ) { cout<<Base Class constructor bank called ;}
~bank( ) {cout<<Base Class destructor bank called;}};
class person{public:
person( ) {cout<<Base Class constructor person called ;}
~person( ) {cout<<Base Class destructor person called;}};
class owner:public bank,public person{
owner( ) {cout<<Derived Class constructor owner called ;}
~owner( ) {cout<<Derived Class destructor owner called;}};
int main( ){
owner o1; }

Shivani Nanda 41
Inheritance
Constructors and Destructors of Virtual Base Classes
Silicon Institute of Technology

If a derived class inherits both a virtual base


class and a non-virtual base class, then the
constructor of the virtual base class is called
before the constructor of the non-virtual base
class
The destructors are called in the reverse order
of calling of the constructors i.e. the destructor
of the non-virtual base class is called before
the destructor of the virtual base class

Shivani Nanda 42
Inheritance
Parameterized Constructor of the Base class
Silicon Institute of Technology

Syntax to define a derived class constructor that


explicitly calls one or more base class constructor is
derived_class :: derived_class(parameter_list):
base_class1(parameter_list),,base_classk (parameter_list)

Parameterized constructor of the base class is


explicitly called in the header of the derived class
constructor
The arguments of the parameterized constructor can
be provided through the arguments of the derived
class constructor

Shivani Nanda 43
Inheritance
Silicon Institute of Technology

class aaa{ int i;


aaa(int x ) { i = x;
cout<<Value of i in the base class aaa <<i<<endl;}
~aaa( ) {cout<<Destructor aaa called;}};

class bbb{ int j;


bbb(int y ) { j = y;
cout<<Value of j in the base class bbb <<j<<endl;}
~bbb( ) {cout<<Destructor bbb called;}};

Shivani Nanda 44
Inheritance
Silicon Institute of Technology

class ccc : public aaa, public bbb


{ int k;
ccc(int z,int m,int n):aaa(z),bbb(m)
{ k = n;
cout<<Value of k in the derived class ccc <<k<<endl; }
~ccc( )
{ cout<<Destructor ccc called;} };
int main( )
{ ccc c1(1,2,3);
return 0; }

Shivani Nanda 45

Potrebbero piacerti anche