Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr....

26
Inheritance in Object- Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani

Transcript of Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr....

Page 1: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Inheritance in Object-OrientedProgramming Languages with an

Emphasis on C++.

Supervised by : Dr. Driss Kettani

Page 2: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Team Members

Hadrouni Najm (Senior Student)El Khalifi Bachir (Junior Student)Rhomri Abdelghani (Senior Student)

Page 3: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Outline

Introduction Composition Composition Syntax

Inheritance Advantages Of Inheritance Single Inheritance Multiple Inheritance

Page 4: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Outline (cont.)

Types Of Inheritance Public Inheritance Private Inheritance Protected Inheritance Conclusion Composition vs. Inheritance

Page 5: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Introduction

Need For Code Reuse. Different Approaches. Composition Inheritance

The Need For Inheritance.

Page 6: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Composition

Composition is used intuitively with Standard Data type

Composition is to embed within a class an object from another class.

Definition

Page 7: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Composition Syntax

class Y {public:X x; // Embedded objectY() { i = 0; }};

class X {int i;public:X() { i = 0; }};

Page 8: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Inheritance

Definition

Inheritance is to use a class as a Base class and Derive a new class from it.

Base

DerivedThe Derived class Inherites all Data and behaviors of the Base Class. (Is Vs Is-Like)

Page 9: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Advantages of inheritance

When a class inherits from another class, there are threethree benefits:

You can reuse the methods and data of the existing class

You can extend the existing class by adding new data and new methods

You can modify the existing class by overloading its methods with your own implementations

Page 10: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Single Inheritance

Implement an “is-a” relationship

Single inheritance occurs when a derived class inherits from only one class.

Page 11: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Single Inheritance

•Derive a Class from a Base Class.

•Can be either Is or Is-Like Inheritance

Shape

draw ( ) erase( ) m ove( ) getColor( ) setColor( )

Circle Square Triangle

Page 12: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Multiple Inheritance

C++ allows you to practice multiple inheritance. Multiple inheritance allows a class

to inherit from multiple classes. Multiple inheritance is error prone

and is to be avoided.

Page 13: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Multiple inheritance

Clock Radio

Clock Radio

Page 14: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Multiple Inheritance

class ClockRadio: public Clock, public Radio {...}

class Clock { ... virtual string get_time() { cout<<“The Time is:“<<Time;}};

class Radio { ... virtual string get_frequency() { cout<<“The frequency is :“<< frequency;}};

Page 15: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Types of Inheritance

C++ allows some types of inheritance:

Public

Private

Protected

Page 16: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Public Inheritance

public members of base class also become public members of derived class:

class X { int i;public: X() { i = 0; } void set(int j) { i = j; } int permute() { return i = i * i; }};

Page 17: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Public Inheritance

class Y : public X { int i; // Different from X's ipublic: Y() { i = 0; } int change() { i = permute(); // Different name call return i; } Here, the permute( ) function is carried through

to the new class interface, but the other member functions of X are used within the members of Y.

Page 18: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Public Inheritance

void set(int ii) { i = ii; X::set(ii); // Same-name }};int main() { Y D; D.change(); D.read(); D.permute(); // Redefined functions hide base versions: D.set(12);} ///:~

Page 19: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Private Inheritance

o We are creating a new class that has all of the data and functionality of the base class, but that functionality is hidden.

o The class user has no access to the underlying functionality, and an object cannot be treated as a instance of the base class

Page 20: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Private Inheritance

class Pet {public: char eat() const { return 'a'; } int speak() const { return 2; } float sleep() const { return 3.0; } float sleep(int) const { return 4.0; }};

Page 21: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Private Inheritance

class Goldfish : Pet { // Private inheritancepublic: Pet::eat; // Name publicizes member Pet::sleep; // Both overloaded members exposed};

int main() { Goldfish bob; bob.eat();bob.sleep(1);} ///:~

Page 22: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Protected Inheritance

#include <fstream>using namespace std;

class Base { int i;protected: int read() const { return i; } void set(int ii) { i = ii; }public: Base(int ii = 0) : i(ii) {} int value(int m) const { return m*i; }};

Page 23: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Protected Inheritance

class Derived : public Base { int j;public: Derived(int jj = 0) : j(jj) {} void change(int x) { set(x); }};

int main() { Derived d; d.change(10);} ///:~

Page 24: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

“This is private as far as the class user is concerned, but available to anyone who inherits from this class”

Protected Inheritance

Page 25: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

Composition vs. Inheritance “Is a” and “Is like” relationships

Use of : Inheritance Relationship in which a class is derived from

another class

“Has a” relationships Use of : Composition

Relationship in which a class contains other classes as members

Page 26: Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani.

References

Thinking In C++ by Bruce Eckel

C++ Programming Language by Bjarne Stroustrup

Charlie Calverts C++ Builder