Vous êtes sur la page 1sur 50

C++

Inheritance
Introduction
The capability of one class to inherit
properties from another class is called
Inheritance.
Inheritance is the mechanism of creating
new specialized classes (child class)from
the pre existing classes.
Concept of INHERITANCE
Base class
Property A
Property B
Property C
Derived From
Derived class
Property D
Property A
Property B
Property C
Defined in
Derived Class
Defined in Base Class
But accessible from
Derived Class
Levels of INHERITANCE
(A)- Single Inheritance.
(B)- Multilevel Inheritance.
(C)- Multiple Inheritance.
(D)- Hierarchical Inheritance.
(E)- Hybrid Inheritance.
Single Inheritance
In this INHERITANCE have only one Base class and
one Derived class.
BASE CLASS
DERIVED CLASS
Multilevel Inheritance
In this INHERITANCE we have a chain of all classes and one BASE LINE ,
In this we must have to use three classes, there is one Base class and more
than one Derived classes.
BASE CLASS
DERIVED CLASSES
Class A
Class B
Class C
Multiple Inheritance
In this INHERITANCE we have more than one independent
Base classes and one Derived class, connected with all
Base classes.
BASE CLASSES
DERIVED CLASS
Class A Class B
Class C
Hierarchical Inheritance
This INHERITANCE is just opposite of Multiple inheritance.
Class A
Class B Class C
BASE CLASS
DERIVED CLASES
Hybrid Inheritance
This is a combine form of Multiple and Hierarchical
Inheritance.
Class B Class C
Class D
Class A
Derived Base class
BASE CLASS
DERIVED CLASS
Visibility Modifier :
Public : A member declared as public is
accessible by the member functions within its
class, by member functions of the class derived
from it and also by any outside function.

Private : A member declared as private is
accessible by the member functions of its class
only.(They cannot be inherited by child class
and they cannot be accessed by outside
functions).
Protected : A member declared as protected is
accessible by the member functions within its
class and any class immediately derived from
it.(i.e. they are inheritable) but are not
accessible by outside functions

#include <iostream.h
class Counter
{
protected:
int count; //count
public:
Counter() : count(0) //constructor
{ }
int get_count() //return count
{ return count; }
Counter operator ++ () //increment count
{
Counter temp; //make a temporary Counter
temp.count =++count; //give it same value as this obj
return temp; //return the copy
}
void setCount(int c)
{count=c;}
};

////////////////////////////////////////////////////////////////

class Dcounter: public counter
{
public:
Counter operator -- () //increment count
{
Counter temp; //make a temporary Counter
temp.count = --count; //give it same value as this obj
return temp; //return the copy
}
};

////////////////////////////////////////////////////////////////
int main()
{
Dcounter dc1;
++dc1;
cout << \n dc1= << dc1.get_count(); //display

dc1.setCount(100);//inherited func. Gets called

cout << \n dc1= << dc1.get_count();

--dc1;

cout << \n dc1= << dc1.get_count(); //display again

return 0;
}
OUTPUT
dc1=1
dc1=100
dc1=99

Visibility mode :
When the base class is privately inherited by the
derived class,public members of the base class
becomes private members of the derived class.

When the base class is publicly inherited,public
members of the base class become public
members of the derived class.

When a base class is derived in protected
mode,both the public and protected members
of the class become protected members of the
derived class.
Access Specifier Accessible from
own class
Accessible from
derived class
Accessible from
outside world
Private
Yes No No
Protected
Yes Yes No
Public
Yes Yes Yes
Types of derivations
Public, Protected or Private
class Base{

private:
.
protected:
.
public:
...

};
public
class Derived: protected Base
private
{


Body of child class


};
Base Class
Visibility
Public Derivation Private
Derivation
Protected
Derivation
Private
Not Inheritable Not Inheritable Not Inheritable
Protected
Protected Private Protected
Public
Public Private Protected
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person{
protected:
char pname[25];
char dob[20];
public:
person()
{
strcpy(pname,"noname");
strcpy(dob,"nodate");
}
person(char p[],char d[])
{
strcpy(pname,p);
strcpy(dob,d);
}
void readP()
{
cout<<" Enter name:";
gets(pname);
cout<<"\n Enter date of birth:";
gets(dob);
}

void showP()
{
cout<<"\n Person Name: "<<pname;
cout<<"\n Date of birth: "<<dob;
}
};
class employee: public person
{
private:

int empno;
int sal;
public:
employee()
{
empno=0;
sal=0;
}
employee(int e,int s)
{
empno=e;
sal=s;
}

void readE()
{
cout<<"\n Enter employee no:";
cin>>empno;
cout<<"\n Enter salary:";
cin>>sal;
}
void showE()
{
cout<<"\n EmpNo= "<<empno<<endl;
cout<<"\n Sal= "<<sal<<endl;
}
};//class employee ends here

void main()
{
employee1 e1;
clrscr();
e1.showE();
getch();
}
Constructor and Destructor
calling in
Inheritance
#include<iostream.h>
class base {
public:
base()
{
cout << "Constructing base\n";
}
~base()
{
cout << "Destructing base\n";
}
};
class derived: public base {
public:
derived()
{
cout << "Constructing derived\n";
}
~derived()
{
cout << "Destructing derived\n";
}
};

int main()
{
derived ob;
clrscr();
getch();
return 0;
}
OUTPUT:
Constructing base
Constructing derived
Destructing derived
Destructing base
Note:
When an object of a derived class is created, the base class
constructor will be called first, followed by the derived class
constructor. When a derived object is destroyed, its destructor is
called first, followed by the base class' destructor. Put
differently, constructors are executed in their order of
derivation. Destructors are executed in reverse order of
derivation.
Passing parameters to base class
constructor via child class
derived-constructor(arg-list) : base1(arg-list), base2(arg-list), baseN(arg-list)
{
// body of derived constructor
}
class base {
protected:
int i;
public:
base(int x) { i=x; cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};
class derived: public base {
int j;
public:
// derived uses x; y is passed along to base.
derived(int x, int y): base(y)
{
j=x;
cout << "Constructing derived\n";
}
~derived()
{ cout << "Destructing derived\n"; }
void show() { cout << i << " " << j << "\n"; }
};
int main()
{
derived ob(3, 4);
ob.show(); // displays 4 3
return 0;
}
Granting Public Access to
members of the derived class
while using private derivation
class base {
int i; // private to base
public:
int j, k;
void seti(int x) { i = x; }
int geti() { return i; }
};
// Inherit base as private.

class derived: private base {
public:
/* The next three statements override base's inheritance as private and restore j,
seti(), and geti() to public access. */

base::j; // make j public again - but not k
base::seti; // make seti() public
base::geti; // make geti() public
// base::i; // illegal, you cannot elevate access
int a; // public
};
int main()
{
derived ob;
//ob.i = 10; // illegal because i is private in derived
ob.j = 20; // legal because j is made public in derived
//ob.k = 30; // illegal because k is private in derived
ob.a = 40; // legal because a is public in derived
ob.seti(10);
cout << ob.geti() << " " << ob.j << " " << ob.a;
return 0;
}
Virtual Function V/S Normal Function
NON VIRTUAL FUNC WHEN ACCESSED BY OBJECTS
class base {
public:
void func() {
cout << "This is base's func().\n";
}
};
class derived1 : public base {
public:
void func() {
cout << "This is derived1's func().\n";
}
};
int main()
{
base b;
derived1 d1;
b.func();
d1.func();
return 0;
}

Output:

This is base's func()
This is derived1's func()

VIRTUAL FUNC WHEN ACCESSED BY OBJECTS

class base {
public:
virtual void func() {
cout << "This is base's func().\n";
}
};
class derived1 : public base {
public:
void func() {
cout << "This is derived1's func().\n";
}
};
int main()
{
base b;
derived1 d1;
b.func();
d1.func();
return 0;
}

Output:

This is base's func()
This is derived1's func()

The output of previous two
programs show that there is no
difference between Non virtual
and Virtual Functions as long as
they are accessed using class
objects.
NON VIRTUAL FUNC WHEN ACCESSED USING POINTER
class base {
public:
void func() {
cout << "This is base's func().\n";
}
};
class derived1 : public base {
public:
void func() {
cout << "This is derived1's func().\n";
}
};
int main()
{
base b,*bp;
derived1 d1;
bp=&b;
bp->func();
bp=&d1;
bp->func();
return 0;
}

Output:

This is base's func()
This is base's func()

VIRTUAL FUNC WHEN ACCESSED USING POINTERS

class base {
public:
virtual void func() {
cout << "This is base's func().\n";
}
};
class derived1 : public base {
public:
void func() {
cout << "This is derived1's func().\n";
}
};
int main()
{
base b,*bp;
derived1 d1;
bp=&b;
bp->func();
bp=&d1;
bp->func();
return 0;
}

Output:

This is base's func()
This is derived1's func()

Static Binding
V/S
Dynamic Binding
Static Binding Dynamic Binding
It is also called early binding
or compile time binding
It is also called late binding or
run time binding
In this compilers
binds(attaches) the function
call statement to function
body during compilation.
In this compilers
binds(attaches) the function
call statement to function
body during execution.

It takes place in the case of
Non Virtual function
It takes place in the case of
Virtual function
In this compiler gives
emphasis to type of pointer
In this compiler gives
emphasis to the contents of
pointer

PURE VIRTUAL FUNCTIONS

If a virtual function does not have body and
equated to zero it is called pure virtual function.

virtual void func()=0;
Any class that contains even a single pure virtual
function is called an Abstract Class.


Abstract class cannot be instantiated i.e. we
cannot create objects for such a class.
Explain why destructors should
be virtual
class human {
public:
human() { cout << "Constructing human\n"; }
~human() { cout << "Destructing human\n"; }
};
class person: public human {

public:
person(){cout << "Constructing Person\n"; }
~person(){cout << "Destructing Person\n"; }
};
int main()
{
human *hp=new person();
delete hp;
return 0;
}

O/P: Destructing Human
In previous program destructor of parent
class was called by the compiler and child
class destructor was missed out
class human {
public:
human() { cout << "Constructing human\n"; }
virtual ~human() { cout << "Destructing human\n"; }
};
class person: public human {

public:
person(){cout << "Constructing Person\n"; }
~person(){cout << "Destructing Person\n"; }
};
int main()
{
human *hp=new person();
delete hp;
return 0;
}

O/P: Destructing Person
Destructing Human

Vous aimerez peut-être aussi