Top Banner
73

INHERITANCE

Jan 22, 2016

Download

Documents

keona

INHERITANCE. A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE) The existing class is referred to as the Base Class/ Parent Class/ Super Class. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INHERITANCE
Page 2: INHERITANCE

A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE)

The existing class is referred to as the Base Class/ Parent Class/ Super Class.

The new class the is being defined by inheriting from the existing class is referred to as the Derived Class/ Child Class/ Sub Class

Syntax:Class <name of derived class>:<access specifier> <name of base class>{……

// definition of class ….

};

Page 3: INHERITANCE

Eg: Class B : Public A{// new features of class B};

Diagrammatic representation of Inheritance:

A

B

Page 4: INHERITANCE

class A{ int x;

public: void setx(const int=0);int getx() const; };

class B: public A{ int y;public: void sety(const int =0);int gety() const; };

void main(){ cout<<sizeof(A)<<endl<<sizeof(B);B B1;B1. setx(1);B1.sety(3);cout<<B1.getx();cout<<endl<<B1.gety(); }

Page 5: INHERITANCE

Effects of Inheritance:Inheritance affects the size and

behaviour of the derived class in 2 ways:

1. An object of the derived class will contain:

Data Members of Derived Class+

Data Members of Base ClassThus, An object of the Derived Class is generally

larger than an object of the Base Class.

Exception???

Page 6: INHERITANCE

2. We can call a member function of the Derived Class using an object of the Derived Class…. The same object can be used to call the public members of the Base Class also.

Does not happen in some circumstances!!!!____________________________________________________

Inheritance implements a is-a relationship.

The process of Inheritance is also called SPECIALIZATION. Why???

Main benefit of Inheritance???

Page 7: INHERITANCE

Container Class: A class that contains an

object of another class or a pointer to a data structure that contains a set of objects of another class.

Containership implements a has-a relationship.

Page 8: INHERITANCE

Base and Derived Class Objects: An object of the Derived Class is not at all

related to another simultaneously existing object of the base class.

x

x

y

A1

B1

101

201

Page 9: INHERITANCE

Accessing Members of the Base Class in the Derived Class

Only Public members of the Base Class can be accessed in the functions of Derived Class.

Eg: if definition is:void B::sety(){x=y;}

ERROR!!! Private members of the Base Class cannot be accessed.

Eg: ‘A:: setx()’ and ‘A::getx()’ can be accessed in the member functions of the Derived Class . Why???

Page 10: INHERITANCE

Private members of the Base Class remain Private with respect to the member functions of the Derived Class.

Eg: void B::sety(const int q){

y=q;setx(y)

}

Thus, Data Security is implemented in C++.

Inheritance is used to add facilities to an existing class without reprogramming or recompiling it.

Page 11: INHERITANCE

class B;class A

{ friend class B;int x; };

class B{ void fB (A *p)

{ p->x=0; } };class C : public B

{ void fC(A *p) { p->x=0; } };

FRIENDSHIP IS NOT INHERITED!

Page 12: INHERITANCE

BASE CLASS AND DERIVED CLASS POINTERS

• A Base Class Pointer can point to an object of Derived Class.

• A Derived Class Pointer cannot point to an object of the Base Class.

• A Base Class Pointer can safely point to an object of a Derived Class without the need foe typecasting.(Exceptions will be considered later)

• A Derived Class Pointer can be made to point at an object of the Base Class only forcibly by typecasting. (Can result in run-time errors!)

Page 13: INHERITANCE

class A{ public: int x; };class B : public A{ public : int y; };void main(){ A * Aptr;

B B1;Aptr=&B1;// Base class ptr points to derived class object

Aptr->x=10;//Access Base class member thru base class ptr

Aptr->y=20;//Error! Y not found in class A}

Page 14: INHERITANCE

• Aptr is of type ‘A *’. It is supposed to point at objects of base class A. Therefore, It cannot access ‘y’. ( There is no member of name ‘y’ in class A).

• The fact that Aptr points at an object of class B is of no significance.

• Although ‘Aptr ‘ points at ‘B1’(occupies 4 bytes), it is able to access only the value contained in the first 2 bytes.

• Thus, Aptr cannot access area in the memory that hasn’t been allocated.

A Base class pointer can safely point to an object of the derived class.

Page 15: INHERITANCE

x

y

AptrB1

201

201

Base class pointer points to an object of the derived class

Page 16: INHERITANCE

void main(){

A A1;B * Bptr;Bptr=&A1; // Error!! Cannot convert from B* to A*Bptr->x=10;//ok. Derived class pointer

//accesses base class memberBptr->y=20; //ok. Derived class pointer

//accesses derived class member}

Page 17: INHERITANCE

• Bptr is of type ‘B *’. It is supposed to point to objects of derived class B. It can access ‘y’ also.

• Bptr is pointing to A1(occupies 2 bytes). There is a member of name ‘y’ in class B.

• Thus, Bptr is able to access memory that has not been allocated.

A Derived class Pointer cannot safely point to an object of the base class.

Page 18: INHERITANCE

x

y

Bptr

A1201

201

Derived class pointer points to an object of the Base class

Y would be here in case of derived class objects

Page 19: INHERITANCE

• A derived class pointer can be made to point at an object of base class by explicit typecasting.

void main(){

A A1;B * Bptr; // derived class pointerBptr=(B *)&A1; // Explicit typecasting to make the //derived class pointer point at

//base class object}

Explicit Address manipulation should be avoided and is dangerous!

Page 20: INHERITANCE

Consider the following class definitions:class A{ int x;

public: void setx(const int =0);// rest of class A };

class B: public A{ int y;

public: void sety(const int=0);// rest of class B};

Page 21: INHERITANCE

void main(){

A * Aptr;B B1;Aptr=&B1;// ok Base class ptr points to derived class objAptr->setx(10); //ok access base class member thru base //class ptrAptr->sety(20); //Error! Sety() is not a member of class A

}

Page 22: INHERITANCE

void main(){

A A1;B * Bptr;Bptr=&A1;// Error! Cannot convert A* to B*Bptr->setx(10);//ok derived class pointer accesses base //class memberBptr->sety(20);// ok derived class pointer accesses //derived class member

}

Page 23: INHERITANCE

Defining a member function in the derived class with the name and signature matching those of the base class function.

Results in two functions with the same name and signature. ( one in base class + one in derived class)

Page 24: INHERITANCE

class A{ public: void show()

{ cout<<“ show function in Class A called”; } };

class B:public A{ public: void show() // overriding A::show()

{ cout<<“ show function in Class B called”; } };

Page 25: INHERITANCE

void main(){B B1;B1.show();// B::show() is calledA A1;A1.show();// A::show() is calledB1.A::show();// calling the overridden function

forcibly //with respect to object of the derived class.

}

Page 26: INHERITANCE

Whenever a function is called with respect to an object, the compiler first searches for the function prototype in the same class.

Only if the search fails, the compiler goes up the class hierarchy to look for the function prototype.

i.e. The show() function of class A is virtually hidden by the show function of class B (for the first call of show() in prev eg)

Page 27: INHERITANCE

Function Overriding is a form of overloading. Justify!

Though the signatures of the overriding function and overridden function are the same, The This pointer brings in the apparent difference.

Actual prototype of A::show() becomes void show(A * const);

Actual prototype of B::show() becomes void show(B * const);

Page 28: INHERITANCE

BASE CLASS INITIALIZATION When an object of derived class is created,

the compiler implicitly embeds a call to the base class constructor and then the derived class constructor with respect to the object.

Suppose A is the base class and B is its derived class:

B B1; is converted intoB B1;B1.A();B1.B();

Destructors are called in reverse order.

Page 29: INHERITANCE

class A{ int x;public: A (const int=0);

void setx(const int =0);int getx() const; };

A::A(const int p){ x=p;}void A::setx(const int p){ x=p; }int A::getx()const{ return x;}

class B:public A{ int y;public: B (const int=0);

void sety(const int =0);

int gety() const; };B::B(const int q){ y=q;}void B::sety(const int q){ y=q; }int B::gety()const{ return y;}

void main(){ B B1(20);

cout<<B1.getx()<<endl<<B1.gety()<< endl;}

Page 30: INHERITANCE

B B1(20); gets converted intoB B1; // Memory allocated for objectB1. A(); //Base class constructor calledB1.B(20); //derived class constructor

called

How should the base class constructor be modified to make proper base class initialization?

Class B : public A{ public: B(const int=0,const int =0); …….};B::B(const int p, const int q):A(p){ y=q; }

Page 31: INHERITANCE

void main(){ B B1(10,20);

cout<<B1.getx()<< endl<< B1.gety()<< endl;

}

B B1 (10,20) gets converted to B B1;B1. A(10);B1. B(20);

Page 32: INHERITANCE

It is not necessary that the first parameter should be passed to the Base class constructor.

Any of the parameter in the list can be passed to the base class constructor.

Page 33: INHERITANCE

The PROTECTED access specifier

‘Protected ‘ members are inaccessible to nonmember functions.

They are accessible to the member functions of their own class and to member functions of the derived classes.

Page 34: INHERITANCE

class A{ private: int x;

protected: int y;public: int z;};

class B: public A{ public: void xyz();};

void B::xyz() // member function of derived class{ x=1; // Error: Private member of base classy=2; //Ok: Protected member of base classz=3; // Ok: Public member of base class }

Page 35: INHERITANCE

void main(){

A * Aptr;Aptr->x=10;Aptr->y=20; Aptr->z=30;

}

// Non member function

// Error: Private member// Error: Protected member//Ok: Public member

Page 36: INHERITANCE

Deriving by Different Access SpecifiersDeriving by the Public Access Specifier

- Deriving by the public access specifier retains the access level of the Base class members.Private Members of the Base class:

* Members of the derived class cannot access them.

* Member functions of the subsequently derived classes cannot access them.

* Non-member functions cannot access them.

Page 37: INHERITANCE

Deriving by the Public Access Specifier….

Protected Members of the Base class: * Members of the derived class can

access them. * Member functions of the

subsequently derived classes can also access them.

* Non-member functions cannot access them.

Page 38: INHERITANCE

Deriving by the Public Access Specifier….

Public Members of the Base class: * Members of the derived class can

access them. * Member functions of the

subsequently derived classes can access them.

* Non-member functions can access them.

Page 39: INHERITANCE

class A { private: int x;protected: int y;public : int z; };

class B:public A{ //B is public derived class of Apublic: void f1()

{ x=1; //ERROR! Private member remains private

y=2; //ok. Protected member remains protected

z=2; } }; //ok. Public member remains public

class c:public B{public: void f2()

{ x=1; //ERROR! Private member remains private

y=2; //ok. Protected member remains protected

z=3;}}; //ok. Public member remains public

Page 40: INHERITANCE

void xyz(B *Bptr) // non-member function{

Bptr->x=10; //Error! Private member remains private

Bptr->y=20; //Error! Protected member remains

protectedBptr->z=30; //ok. Public member remains public

}

Page 41: INHERITANCE

A base class pointer can point to an object of a derived class that has been derived by using the public access specifier.

void xyz() // non member function{

B B1; //object of public derived classB1.z=100;// ok. z is public member of class B

A *Aptr; //pointer of base classAptr=&B1; //ok. Can make a base class pointer point at //an object of a public derived class.Aptr->z=100; //ok. Can access inherited public

//member of the base class through a //base class pointer.

}

Page 42: INHERITANCE

Deriving by Different Access SpecifiersDeriving by the Protected Access

Specifier- Deriving by the protected access specifier reduces the access level of the public Base class members to protected, while the access level of protected and private base class members remain unchanged.

Page 43: INHERITANCE

Deriving by the Protected Access Specifier

Private Members of the Base class: * Members of the derived class cannot

access them. * Member functions of the

subsequently derived classes cannot access them.

* Non-member functions cannot access them.

Page 44: INHERITANCE

Deriving by the Protected Access Specifier….

Protected Members of the Base class: * Members of the derived class can

access them. * Member functions of the

subsequently derived classes can also access them.

* Non-member functions cannot access them.

Page 45: INHERITANCE

Deriving by the Protected Access Specifier….

Public Members of the Base class: * Members of the derived class can

access them. * Member functions of the

subsequently derived classes can access them.

* Non-member functions cannot access them.

Page 46: INHERITANCE

class A { private: int x;protected: int y;public : int z; };

class B:protected A{ //B is a protected derived class of Apublic: void f1()

{ x=1; //ERROR! Private member remains private

y=2; //ok. Protected member remains protected

z=2; } }; //ok. Public member remains public

class c:public B{public: void f2()

{ x=1; //ERROR! Private member remains private

y=2; //ok. Protected member remains protected

z=3;}}; //ok. Protected member remains protected

Page 47: INHERITANCE

void xyz(B *Bptr) // non-member function{

Bptr->x=10; //Error! Private member remains privateBptr->y=20;//Error! Protected member remains protectedBptr->z=30; //Error! Public member becomes protected

}

Page 48: INHERITANCE

A base class pointer cannot point to an object of a derived class that has been derived by using the protected access specifier.

void xyz() // non member function{

B B1; //object of derived classB1.z=100;// ERROR! Cannot access public member of base //class through an object of the protected derived class.

A *Aptr; //pointer of base classAptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a protected derived class.Aptr->z=100; //ok. Can access inherited public member of the //base class through a base class pointer.

}

Page 49: INHERITANCE

Deriving by Different Access SpecifiersDeriving by the Private Access Specifier

- Deriving by the private access specifier reduces the access level of the public and protected Base class members to private, while the access level of private base class members remain unchanged.

Page 50: INHERITANCE

Deriving by the Private Access SpecifierPrivate Members of the Base class:

* Members of the derived class cannot access them.

* Member functions of the subsequently derived classes cannot access them.

* Non-member functions cannot access them.

Page 51: INHERITANCE

Deriving by the Private Access Specifier….

Protected Members of the Base class: * Members of the derived class cannot

(can) access them. * Member functions of the

subsequently derived classes cannot access them.

* Non-member functions cannot access them.

Page 52: INHERITANCE

Deriving by the Private Access Specifier….

Public Members of the Base class: * Members of the derived class cannot

(can) access them. * Member functions of the

subsequently derived classes cannot access them.

* Non-member functions cannot access them.

Page 53: INHERITANCE

class A { private: int x;protected: int y;public : int z; };

class B:private A{ //B is a private derived class of Apublic: void f1()

{ x=1; //ERROR! Private member remains private

y=2; //ERROR!Protected member becomes private

z=2; } }; //ERROR!Public member becomes private

class c:public B{public: void f2()

{ x=1; //ERROR! Private member remains private

y=2; //ERROR! Private member remains private

z=3;}}; // ERROR! Private member remains private

Page 54: INHERITANCE

void xyz(B *Bptr) // non-member function{

Bptr->x=10; //Error! Private member remains privateBptr->y=20; //Error! Protected member becomes privateBptr->z=30; //Error! Public member becomes private

}

Page 55: INHERITANCE

A base class pointer cannot point to an object of a derived class that has been derived by using the private access specifier.

void xyz() // non member function{

B B1; //object of derived classB1.z=100;// ERROR! Cannot access public member of base //class through an object of the private derived class.

A *Aptr; //pointer of base classAptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a private derived class.Aptr->z=100; //ok. Can access inherited public

//member of the base class through //a base class pointer.}

Page 56: INHERITANCE

The following two declarations are equivalent:

Class B:private A{….};

Class B:A{….};

Page 57: INHERITANCE

Multiple Inheritance Multi level Inheritance Hierarchical Inheritance Hybrid Inheritance

Page 58: INHERITANCE

A class derives from more than one base class.

Class C derives from Class A and Class B

A B

C

Page 59: INHERITANCE

The general syntax for multiple inheritance :class <name of derived class>:

<access specifier> <name of first base class>,<access specifier> <name of second base class>,<access specifier> <name of third base class>…{….

};

Page 60: INHERITANCE

class A{ int x;public: void setx(const int=0);void getx() const; };class B{ int y;public: void sety(const int=0);void gety() const;};class C: public A,public B{ int z;public: void setz(const int=0);int getz() const; };

Page 61: INHERITANCE

void main(){ C c1;cout<<sizeof(c1)<<endl;c1.setx(10);c1.sety(20);c1.setz(30);cout<<c1.getx()<<endl<<c1.gety()<<endl<

<c1.getz();}

Page 62: INHERITANCE

Output:12102030

Page 63: INHERITANCE

An object of a class defined by multiple inheritance contains not only the data members defined in the derived class, but also the data members defined in all the base classes.

Size of an object: sum of sizes of all the data members of all the base classes + sum of the sizes of data members of all the derived classes.

Thus, we can call the member function of the base class using an object of the derived class as well.

Page 64: INHERITANCE

Ambiguities in Multiple Inheritance: Identical members in more than one

base class.class A{ public: void show(){ cout<<“ show of class A called”; } };class B{ public: void show(){ cout<<“ show of class B called”; } };class C: public A, public B {};void main(){ C C1;C1.show();}

Page 65: INHERITANCE

Ambiguity can be resolved in 2 ways:

Use the scope resolution operator to specify the class to which the member function belongs.

Override the multiple inherited base class member

Page 66: INHERITANCE

Diamond shaped inheritance:

B C

D

A

Page 67: INHERITANCE

class A { public: void show(); };class B: public A {};class C: public A{};class D:public B, public C {};void main(){D D1;D1.show();}

Problems???? Solutions????

Page 68: INHERITANCE

Occurs when a class inherits from a derived class.

It can be extended to any level

B

C

A

Page 69: INHERITANCE

class A{ public: void FA()

{ cout<<“FA called”;}};class B: public A{ public: void FB()

{ cout<<“FB called”;}};class C: public B{ public: void FC()

{ cout<<“FC called”;}};Void main(){ C C1;C1.FA();C1.FB();C1.FC(); }

Page 70: INHERITANCE

A single class serves as a base class for more than one derived class.

This gives the best illustration of the virtues of code reusability.

The need to duplicate common features in more than one class is eliminated.

B C

A

Page 71: INHERITANCE

class A{public:void FA()

{ cout<<“FA called”;}};class B:public A{public:void FB()

{ cout<<“FB called”;}};class C:public A{public:void FC()

{ cout<<“FC called”;}};

void main(){ B B1;C C1;B1.FA();B1. FB();C1. FA();C1.FC();}

Page 72: INHERITANCE

A mixture of all the other kinds of inheritances.

B C

A D

Page 73: INHERITANCE

ORDER OF INVOCATION OF CONSTRUCTORS AND DESTRUCTORS

Constructors are invoked in the following order:1. Virtual base class constructors in the order of inheritance.2. Non-virtual base class constructors in the order of inheritance.3. Member objects constructor in the order of declaration.4. Derived class constructor

Destructors are invoked in the reverse order.