inheritance: extending classes prepared by prepared by : vinay alexander ( विनय...

48
Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER (वववव ववववववववववव) PGT(CS) PGT(CS) KV JHAGRAKHAND KV JHAGRAKHAND

Upload: jemima-lewis

Post on 22-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Inheritance: Extending classes

Prepared ByPrepared By :

VINAY ALEXANDER

(वि�नय अले�क्जे�ण्डर)

PGT(CS)PGT(CS)KV JHAGRAKHANDKV JHAGRAKHAND

Prepared ByPrepared By :

VINAY ALEXANDER

(वि�नय अले�क्जे�ण्डर)

PGT(CS)PGT(CS)KV JHAGRAKHANDKV JHAGRAKHAND

Contents.1)Introduction

2)Need for inheritance

3)Types of inheritance

4)Definition of derived and base classes.

5)Visibility modes

6)Inheritance and constructors and destructors.

7)Virtual base Classes

8)Nesting of classes

9)Relation between classes

Introduction.

The capability of one class to inherit the properties from another class is called inheritance.

The class whose properties are inherited , is called Base class or Super class and the class that inherits these properties is called Derived class or Sub class

The most important advantage of inheritance is code reusability. Once a base class

is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money, and efforts and increases a program’s reliability.

Inheritance: It is the mechanism of deriving new class from existing class. It provides the idea of reusability.

A

B

Base , super,parent class

derived , sub, child class

1. CAPABILITY TO EXPRESS THE INHERITANCE RELATIONSHIP: This ensures the closeness with the real-world models i.e. it lets you generates a model that is closer to the real-world.

• Ex: The class car inherits from another class automobiles which itself inherits from another class vehicles.

2. REUSABILITY:• The most important advantage of inheritance is

reusability.• Inheritance allows the addition of new features to

an existing class without modifying it. • A new class (derived class) can be derived from an

existing class and add new features to it.

ADVANTAGES OF REUSABILITY:• Once a base class is written and debugged, it

can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time , money and efforts and increases a program’s reliability. The Main advantages of reusability are

• Faster development time• Easier maintenance• Easy to extend

3. TRANSITIVE NATURE OF INHERITANCE:• If a class B inherits properties of another

class A, then all subclasses of B will automatically inherit the properties of A.

• This property is called transitive nature of inheritance.

• The benefit of this nature is that if any correction takes place in class A will also be reflected in all the classes that are inherited from it.

Types of inheritance

Single level

B

A

Multilevel Multiple

Hierarchical hybrid

B

A

C

A B

C

A

B C D

E F G H I J

B

A

C

B A B

C

C

Types of inheritance.

1)Single Inheritance: When a subclass is inherited from only one base class it is known as Single Inheritance.

X

Y

BASE CLASS

DERIVED CLASS

2) MULTIPLE INHERITANCE: When a derived class is inherited from multiple

base classes is known as Multiple Inheritance

BASE CLASS

DERIVED CLASS

X Y

BASE CLASS

Z

3) HIERARCHICAL INHERITANCE:When many derived classes are inherited from

a single base class it is called Hierarchical Inheritance

x Y

Z

W

BASE CLASS

DERIVED CLASSES

4) MULTILEVEL INHERITANCE:When a subclass or derived class is inherited

from another derived class is known as Multilevel Inheritance

X

Y

Z

Base class of Y

Derived class of XBase class of Z

Derived class of Y

5) HYBRID INHERITANCE: Hybrid Inheritance combines two or more forms of Inheritance.

i.e. Inheritance-When a subclass inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is called hybrid inheritance

W

X Y

Z

a

X Y Z

A

B C

b

Definition of derived and base classes.

The Visibility Mode controls the visibility and availability of inherited base class members in the derived class.

1)Single Inheritance: SYNTAX of Defining of derived class:

class derived-class:<visibility mode> <base-class>

Example: class Sub: public Super

{

};

=>The derived class has access privilege only to the non private members of the base class.

=>If no visibility mode is specified , then by default the visibility mode is considered as Private.

2)Multiple Inheritance:

class derived_class:vis_mode base1,vis_mode base2

{

.

};

Class Sub: public SuperA,private SUperB

{

//Members

};

• 3)Multilevel Inheritance: SYNTAX

• Class derived1:visibility_mode base

• { .

• .

• }

• Class derived2:visibilty_mode base

• { .

• .

• };

Visibility Modes.The visibility modes basically control the access specifier to be for inheritable members of base class, in derived class.

Role of access specifiers in inheritable classes.

Visibility Mode Inheritable public members become

Inheritable protected members become

Inheritable private members become

Public Public Protected Private

Protected

Protected Protected Private

Private Private Private Private

1) Public visibility mode:The public derivation means that the

derived class can access the public members of the base class but not the private members of the class. with publicly derived class , the public members of the base class become the public members of the derived class , and the protected members of the base class become the protected members of the derived class.Class Super Class Sub

X privateY publicZ protected

Y publicZ protected

2) Private visibility mode:The private derivation means the derived class can access the public and protected members of the class privately. i.e public and protected members of the base class become private members of the derived class. They cannot be inherited further if the derived happens to be the base class of any other class.

Class Super

{Private: int x;void check(void);Public: int y;void display(void);Protected: int z; void getval(void);};Class Sub: Public Super{ private: int s; void init(void);pubilc:int b; readit(void);Protected: int c; writeit(void);};

3) Protected visibility mode: The protected derivation of a class means that the derived class can access the public and protected members of the base class protectedly.i.e the public and protected members of the base class become protected members of the derived class.

Class Super

{

};

Class Sub: protected Super

{

};

Deriving publicly is a way of saying “ is a type of”.

=> Inheritance and the Base class1. Member intended to be inherited and at the same

time intended to be available to every function , even to non-members ,should be declared as public members in the base class.

2. Member intended to be inherited but not intended to be public, should be declared as protected members in the base class.

3. Member not intended to be inherited should be declared as private members in the base class.

=> The significance of visibility mode:

ACCESSIBILITY OF BASE CLASS MEMBERS.

ACCESS SPECIFIER

Accessible from own class

Accessible from derived class

Accessible from objects outside class

PUBLIC Yes Yes Yes

PROTECTED Yes Yes No

PRIVATE Yes No No

INHERITANCE AND CONSTRUCTORS AND DESTRUCTORS:

When an object of a derived class is created, the program first calls the constructor of base class, then the constructor for the derived class. This is because the constructor of derived class may build upon data members from base class; hence base class object has to be constructed first. When an object of a derived class expires , first the derived class destructor is invoked , followed by the base class destructor.

.class Super { . . //same as before };Class Sub: public Super{};int main(){Sub Ob1;}While Ob1 is created, first the constructor Super:: Super( ) is invoked and then Sub::sub( ) is invoked. Also when ob1 expires, at the main ( ) ,firstly Sub::~Sub() is invoked and then Super:: ~Super( ) is invoked.Sometimes , the base class contractors require arguments to construct their object.In such situation, it is the responsibility of the derived class constructor to pass those arguments required by the corresponding base class.

Derived:: Derived(type x, type y,….): Base(x,y){

}

derived class constructor accepts argument for itself as well as for its base class

Then the derived class constructor invoked base class constructor by passing appropriate arguments that it received for base constructor

=>Passing Arguments through to a Base class ConstructorClass Base{int a;Float b;Public: Base(int I, float j){A=I;B=j;};Class Derived: public Base{Public: Derived (int p,float q):Base(p, q){ }}; Even if the derived

constructor does not need a parameter for itself , yet it accepts parameters for it base class

Class Base{int a;Float b;Public: Base(int I, float j){A=I;B=j;};Class Derived: public Base{ int x; float y;Public: Derived (int i,float j,int p,float q):Base(p, q){ }}; the derived constructor is

accepting parameters for itself as well as for its base class

Inheritance and Access Control.1)Access control in publicly derived classes: A class is publicly derived when the keyword public precedes the base class name in the class definition.

Note: The private members of the base class can be accessed indirectly using public or protected member function of the base class.

Eg. Syntax:

class Manager: public Employee, public Person

{ .

.

}

2)Access control in privately derived classes: A class is privately derived when the keyword private precedes the base class name in the class definition.

Note: The public and protected members of the base class becomes private members of the derived class in private derivation. the object of the derived class cannot access them directly. Eg.

class Manager: private Employee

{ .

.

}

Note:

1.The private members of the base class are visible in the derived class but they are not directly accessible.int test;Class Base { int test; public: void getit() { cin>>test; } };Class Derived: public Base { public: void check() { test++;//not allow };

2. You cannot deny access to certain members of a Base class when inheritance publicly.

Class Base{ public: int x,y,z; };Class Derived: public Base { public: int a; Private: Base::x;//not allowed };

3. You can selectively allow access to some of the base class members when deriving privately. Example:

Class Base{ public: int x,y,z; };Class Derived: private Base { public: Base::x;// allowed int a; };

Abstract class: A class that serves only as a base class from which other classes are derived , but no objects of this base class type exit, is known as Abstract class.

Making a private Member Inheritable:(a). By making the visibility mode of the private member as public.(b). By making the visibility mode of the private member as protected.

=> PrivateProtected

Public

PrivateProtected

Public

PrivateProtected

Public

PrivateProtected

Public

Not InheritanceBase class

=> Shadowing/Overriding Base Class Functions in Derived Class: When a derived class function has the same name as that if its base class member function(i.e. redefined in derived class), the derived class member function shadows/hides the base’s class inherited function and this situation is known as function OVERRIDING.

=> Unveiling shadowed Inherited Members: A using declaration is a way or make available pre-defined members to the current scope.

syntax;:using <parent class name>::<Member name>Using A::f1;

Constructors in multiple inheritance: The base classes are constructed in the order in which they appear in the declaration of the derived class.

The base class constructors are called and executed before executing the statements in the body of the derived constructor.

VIRTUAL BASE CLASS: An element of ambiguity can be introduced into a c++ program when multiple base classes are inherited.

Base D1 D2 D3

• #include<iostream.h>

Class Base{ public:int a

};

Class D1:public Base

{

public: int b;

};

Class D2:public Base

{ public:int c;

};

Class D3:public D1, public D2

{

public: int total;

};

void main(){D3 ob;ob.a=25; //ambiguousob.b=50;ob.c=75;ob.total=ob.a+ob.b+ob.c; //ambiguouscout<<ob.a<<”\t”<<ob.b<<“\

t”<<ob.c<<“\t”<<ob.total<<“\n”;

}

1.Solved the above problem using scope resolution operator.

void main(){D3 ob;Ob.D1::a=25; //SCOPE resolved , used D1’s aob.b=50;ob.c=75;ob.total=ob.D1::a+ob.b+ob.c; cout<<ob.D1::a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”;}

2.Solved the above problem using virtual Base class.

The dreaded diamond refers to a class structure in which a particular class appears more than once in a class’s inheritance hierarchy.

Base

D1 D2

D3

The use virtual keyword in the classes just below the top of the dreaded diamond and not in the join class.

Base

virtual D1 virtual D2

D3

#include<iostream.h>

Class Base{ public:int a

};

Class D1:virtual public Base

{

public: int b;

};

Class D2:virtual public Base

{ public:int c;

};

Class D3:public D1, public D2

{

public: int total;

};

void main(){ clrscr();D3 ob;ob.a=25; //Now unambiguousob.b=50;ob.c=75;ob.total=ob.a+ob.b+ob.c; //unambiguouscout<<ob.a<<”\t”<<ob.b<<“\

t”<<ob.c<<“\t”<<ob.total<<“\n”;

}

=>NESTING OF CLASSES: When a class contains objects of another class type as its member or when a class contains another class within its memory, it is known as nesting of classes. it is also known as containership or containment or aggregation.For Example:Class X{…..};Class Y {….};Class Z{ X Ob1;//object of X class Y Ob2; //object of Y class };

Relationship between classes1)IS-A Relationship- When a class inherits from

another class it is known as a IS-A relationship.2)HAS-A Relationship- When a class contains

object of another class type it is known as a HAS-A relationship.

The class having HAS-A relationship with other class has the Ownership of the contained object. Ownership define the responsibility for the creation and the destruction of an objet.

3)HOLDS-A Relationship- It is similar to HAS-A relationship but ownership is missing in HOLDS-A relationship. A class that indirectly contains another object .i.e. via pointer or reference .

Summary.1.Inheritance is the capability of one class to

inherit the properties of another class.

2.Inheritance supports reusability of code and is able to simulate the transitive nature of real life objects.

3.A class from which another class is inheriting its properties is called base class and the class inheriting properties is known as a sub class or derived class.

4.When a class inherits from a single class it is known as single inheritance.

5.When a class inherits from multiple base classes it is known as multiple inheritance.

6.When several classes inherit from the same class it is hierarchical inheritance.

7.When a subclass is the base class of another

class it is known as multilevel inheritance.

5. When a class inherits from multiple base classes and all of its base classes are subclasses of the same class it is hybrid inheritance.

6. A class can derive itself publicly, privately and protectedly.

7. In the publicly derived class the public and protected members of the base class become private members.

8. In privately derived class the public and the protected members of the base class become private members.

9. In the protectedly derived class the public and the protected members of the class become protected members.

10. The derived class constructor is responsible for invoking (and passing arguments to) the base class constructor.

11. The derived class can directly access only the public and protected members of the base class.

12. To make the private member of a class inheritable declare it under protected section of the base class.

13. When a class inherits from more than one base class this is called multiple inheritance.

14. When a derived class and its base class have common ancestor then ambiguity may arise as the derived class contains multiple copies of common ancestor. This can be resolved either by using scope resolution operator :: or by declaring the common ancestor as virtual.