Upload
jocelin-jordan
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
Inheritance in Object-OrientedProgramming 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)
Outline
Introduction Composition Composition Syntax
Inheritance Advantages Of Inheritance Single Inheritance Multiple Inheritance
Outline (cont.)
Types Of Inheritance Public Inheritance Private Inheritance Protected Inheritance Conclusion Composition vs. Inheritance
Introduction
Need For Code Reuse. Different Approaches. Composition Inheritance
The Need For Inheritance.
Composition
Composition is used intuitively with Standard Data type
Composition is to embed within a class an object from another class.
Definition
Composition Syntax
class Y {public:X x; // Embedded objectY() { i = 0; }};
class X {int i;public:X() { i = 0; }};
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)
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
Single Inheritance
Implement an “is-a” relationship
Single inheritance occurs when a derived class inherits from only one class.
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
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.
Multiple inheritance
Clock Radio
Clock Radio
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;}};
Types of Inheritance
C++ allows some types of inheritance:
Public
Private
Protected
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; }};
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.
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);} ///:~
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
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; }};
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);} ///:~
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; }};
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);} ///:~
“This is private as far as the class user is concerned, but available to anyone who inherits from this class”
Protected Inheritance
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
References
Thinking In C++ by Bruce Eckel
C++ Programming Language by Bjarne Stroustrup
Charlie Calverts C++ Builder