inheritance oop concept in c++

19
Presentation Presentation OOP OOP Author: Author: Asad-Ur-Rehman Asad-Ur-Rehman

Upload: softex-software-house

Post on 15-Jul-2015

241 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Inheritance OOP Concept in C++

PresentationPresentation

OOPOOP Author:Author:

Asad-Ur-RehmanAsad-Ur-Rehman

Page 2: Inheritance OOP Concept in C++

Inheritance: Extending Inheritance: Extending ClassesClasses

Page 3: Inheritance OOP Concept in C++

IntroductionIntroduction

Reusability is an important feature of Reusability is an important feature of OOP.OOP.

C++ strongly supports the concept C++ strongly supports the concept of reusability.of reusability.

Page 4: Inheritance OOP Concept in C++

IntroductionIntroduction

The mechanism of deriving a new The mechanism of deriving a new class from an old one is called class from an old one is called inheritance (or derivation).inheritance (or derivation).

The old class is referred to as base The old class is referred to as base class.( superclass, parent)class.( superclass, parent)

The new class is called the derived The new class is called the derived class or subclass.(child)class or subclass.(child)

continue …

Page 5: Inheritance OOP Concept in C++

IntroductionIntroduction

The derived class inherits some or all The derived class inherits some or all of the attributes and functions from of the attributes and functions from the base class.the base class.

A class can also inherit properties A class can also inherit properties from more than one class or from from more than one class or from more than one level.more than one level.

continue …

Page 6: Inheritance OOP Concept in C++

Single InheritanceSingle Inheritance

A derived class with only one base A derived class with only one base class.class.

A

B

Page 7: Inheritance OOP Concept in C++

Multiple InheritanceMultiple Inheritance

A derived class with several base A derived class with several base classes.classes.

A

C

B

Page 8: Inheritance OOP Concept in C++

Multilevel InheritanceMultilevel Inheritance

The mechanism of deriving a class The mechanism of deriving a class from another derived class.from another derived class.

A

C

B

Page 9: Inheritance OOP Concept in C++

Derived ClassesDerived Classes

A derived class can be defined by A derived class can be defined by specifying its relationship with the specifying its relationship with the base class in addition to its own base class in addition to its own details.details.

classclass derived-class-namederived-class-name :: visibility-mode visibility-mode base-class-namebase-class-name{{

………………////………………// members of derived class// members of derived class………………////

};};

Page 10: Inheritance OOP Concept in C++

Derived ClassesDerived Classes

classclass derived-class-namederived-class-name :: visibility-mode visibility-mode base-class-namebase-class-name

{{………………////………………// members of derived class// members of derived class………………////

};};

The colon indicates that the derived-class-name is derived from the base-class-name

The visibility mode is optional and , if present, may be either private or public.

The default visibility mode is private.

Visibility mode specifies whether the features of the base class are derived privately or publicly.

continue …

Page 11: Inheritance OOP Concept in C++

Derived ClassesDerived Classes

When a base class is privately derived by a When a base class is privately derived by a derived class, “public members” of the base class derived class, “public members” of the base class become “private members” of the derived class.become “private members” of the derived class.

Therefore the members of the derived class can Therefore the members of the derived class can only access the public members of the base class.only access the public members of the base class.

They are inaccessible to the objects of the They are inaccessible to the objects of the derived class.derived class.

No member of the base class is accessible to the No member of the base class is accessible to the objects of the derived class.objects of the derived class.

continue …

Page 12: Inheritance OOP Concept in C++

Derived ClassesDerived Classes

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

They are accessible to the objects of the They are accessible to the objects of the derived class.derived class.

continue …

Page 13: Inheritance OOP Concept in C++

Derived ClassesDerived Classes

The private members of the base class are The private members of the base class are not inherited in both the cases not inherited in both the cases (publicly/privately inherited).(publicly/privately inherited).

The private members of a base class will The private members of a base class will never become the members of its derived never become the members of its derived class.class.

continue …

Page 14: Inheritance OOP Concept in C++

InheritanceInheritance

In inheritance, some of the base class data In inheritance, some of the base class data elements and member functions are inherited into elements and member functions are inherited into the derived class.the derived class.

We can add our own data and member functions We can add our own data and member functions for extending the functionality of the base class.for extending the functionality of the base class.

It is a powerful tool for incremental program It is a powerful tool for incremental program development.development.

Page 15: Inheritance OOP Concept in C++

Effect of Inheritance on the visibility of MembersEffect of Inheritance on the visibility of Members

Private

Protected

Public

Private

Protected

Public

Private

Protected

Private

Protected

Public Public

Not inheritableNot inheritable

Class B

Class D1 : public B Class D2 : private B

Class X : public D1, protected D2

Page 16: Inheritance OOP Concept in C++

VisibilityVisibility

Base class Base class visibilityvisibility

Derived class visibilityDerived class visibility

Public Public DerivationDerivation

Private Private DerivationDerivation

Protected Protected DerivationDerivation

Private Private Not Not InheritedInherited

Not Not InheritedInherited

Not Not InheritedInherited

Protected Protected ProtectedProtected PrivatePrivate ProtectedProtected

Public Public PublicPublic PrivatePrivate ProtectedProtected

Page 17: Inheritance OOP Concept in C++

class Shapeclass Shape

{{

protected:protected:

float width, height;float width, height;

public:public:

void set data (float a, float b)void set data (float a, float b)

{{

width = a; height = b; width = a; height = b;

}}

}; };

class Rectangle: public Shapeclass Rectangle: public Shape

{ {

public:public:

float area () float area ()

{{

return (width * height); return (width * height);

}}

};};

class Triangle: public Shapeclass Triangle: public Shape

{{

public:public:

float area () float area ()

{{

return (width * height) / 2;return (width * height) / 2;

}}

};};

int main ()int main ()

{ {

Rectangle rect; Triangle tri;Rectangle rect; Triangle tri;

rect.set data (5,3); rect.set data (5,3);

tri.set data (2,5);tri.set data (2,5);

cout << rect.area() << endl; cout << rect.area() << endl; cout << tri.area() << endl; cout << tri.area() << endl; return 0; return 0;

}}

Output: 15Output: 15

55

Page 18: Inheritance OOP Concept in C++

Resolution in InheritanceResolution in Inheritance

class Aclass A{{ public:public:

void display ()void display () { cout << “Class A \n“;}{ cout << “Class A \n“;}};};class B : public Aclass B : public A{{

public:public: void display ()void display ()

{ cout << “Class B \n“;}{ cout << “Class B \n“;}};};

void main( )void main( ){{ B b;B b; b.display( );b.display( ); // in B// in B b.A::display( );b.A::display( ); // in A// in A b.B::display( );b.B::display( ); // in B// in B}}

continue …

Page 19: Inheritance OOP Concept in C++

Thank YouThank You