rtt

50
C++ C++ Inheritance Inheritance

description

ytjjnuy

Transcript of rtt

Page 1: rtt

C++C++InheritanceInheritance

Page 2: rtt

IntroductionIntroduction►The capability of one class to inherit The capability of one class to inherit

properties from another class is properties from another class is called called Inheritance. Inheritance.

► Inheritance Inheritance is the mechanism of is the mechanism of creating new specialized classes creating new specialized classes (child class)from the pre existing (child class)from the pre existing classes.classes.

Page 3: rtt

Concept of INHERITANCE Concept of INHERITANCE

Base class

Property A

Property BProperty C

Derived From

Derived class

Property DProperty AProperty B Property C

Defined in Derived Class

Defined in Base Class But accessible from Derived Class

Page 4: rtt

Levels of INHERITANCELevels of INHERITANCE►(A)- Single Inheritance.(A)- Single Inheritance.►(B)- Multilevel Inheritance.(B)- Multilevel Inheritance.►(C)- Multiple Inheritance.(C)- Multiple Inheritance.►(D)- Hierarchical Inheritance.(D)- Hierarchical Inheritance.►(E)- Hybrid Inheritance.(E)- Hybrid Inheritance.

Page 5: rtt

Single InheritanceSingle Inheritance• In this INHERITANCE have only one Base class and one Derived class.

BASE CLASS

DERIVED CLASS

Page 6: rtt

Multilevel InheritanceMultilevel 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

Page 7: rtt

Multiple InheritanceMultiple 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

Page 8: rtt

Hierarchical InheritanceHierarchical Inheritance•This INHERITANCE is just opposite of Multiple inheritance.

Class A

Class B Class C

BASE CLASS

DERIVED CLASES

Page 9: rtt

Hybrid InheritanceHybrid 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

Page 10: rtt

Visibility Modifier :Visibility Modifier :► PublicPublic : : A member declared as public is accessible A member declared as public is accessible

by the member functions within its class, by by the member functions within its class, by member functions of the class derived from it and member functions of the class derived from it and also by any outside function.also by any outside function.

► PrivatePrivate : : A member declared as private is accessible A member declared as private is accessible by the member functions of its class only.(They by the member functions of its class only.(They cannot be inherited by child class and they cannot cannot be inherited by child class and they cannot be accessed by outside functions).be accessed by outside functions).

► Protected Protected : : A member declared as protected is A member declared as protected is accessible by the member functions within its class accessible by the member functions within its class and any class immediately derived from it.(i.e. they and any class immediately derived from it.(i.e. they are inheritable) but are not accessible by outside are inheritable) but are not accessible by outside functionsfunctions

Page 11: rtt

#include <iostream.hclass 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;} };

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

Page 12: rtt

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 } };

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

Page 13: rtt

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

Page 14: rtt

Visibility mode :Visibility mode :►When the base class is privately inherited When the base class is privately inherited

by the derived class,’public members of the by the derived class,’public members of the base class becomes ‘private members’ of base class becomes ‘private members’ of the derived class.the derived class.

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

►When a base class is derived in protected When a base class is derived in protected mode,both the public and protected mode,both the public and protected members of the class become protected members of the class become protected members of the derived class.members of the derived class.

Page 15: rtt

Access Specifier

Accessible from own class

Accessible from derived class

Accessible from outside world

Private Yes No NoProtected Yes Yes NoPublic Yes Yes Yes

Page 16: rtt

Types of derivationsTypes of derivationsPublic, Protected or PrivatePublic, Protected or Private

Page 17: rtt

class Base{

private: …….

protected:…….

public:.…..

}; public class Derived: protected Base private{ ……Body of child class……

};

Page 18: rtt

Base Class Visibility

Public Derivation

Private Derivation

Protected Derivation

Private Not Inheritable Not Inheritable Not InheritableProtected Protected Private ProtectedPublic Public Private Protected

Page 19: rtt

#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); }

Page 20: rtt

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; }

Page 21: rtt

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();}

Page 22: rtt

Constructor and Destructor Constructor and Destructor calling in calling in

InheritanceInheritance

Page 23: rtt

#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"; }};

Page 24: rtt

int main(){ derived ob; clrscr(); getch(); return 0;}OUTPUT:Constructing baseConstructing derivedDestructing derivedDestructing baseNote: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.

Page 25: rtt

Passing parameters to base Passing parameters to base class constructor via child class constructor via child

class class derived-constructor(arg-list) : base1(arg-list), base2(arg-list), …baseN(arg-list){// body of derived constructor}

Page 26: rtt

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;}

Page 27: rtt

Granting Public Access to Granting Public Access to members of the derived class members of the derived class while using private derivationwhile using private derivation

Page 28: rtt

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 kbase::seti; // make seti() publicbase::geti; // make geti() public// base::i; // illegal, you cannot elevate accessint a; // public

};

Page 29: rtt

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;}

Page 30: rtt

Virtual Function V/S Normal Function

Page 31: rtt

NON VIRTUAL FUNC WHEN ACCESSED BY OBJECTSclass 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";}

};

Page 32: rtt

int main(){

base b;derived1 d1;b.func();d1.func();

return 0;}

Output:

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

Page 33: rtt

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";}

};

Page 34: rtt

int main(){

base b;derived1 d1;b.func();d1.func();

return 0;}

Output:

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

Page 35: rtt

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.

Page 36: rtt

NON VIRTUAL FUNC WHEN ACCESSED USING POINTERclass 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";}

};

Page 37: rtt

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()

Page 38: rtt

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";}

};

Page 39: rtt

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()

Page 40: rtt

Static Binding V/S

Dynamic Binding

Page 41: rtt

Static Binding Dynamic BindingIt 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

Page 42: rtt

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.

Page 43: rtt

Explain why destructors should be virtual

Page 44: rtt

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

Page 45: rtt

In previous program destructor of parent class was called by the compiler and child class destructor was missed out

Page 46: rtt

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 PersonDestructing Human

Page 47: rtt
Page 48: rtt
Page 49: rtt
Page 50: rtt