Download presentation
Presentation is loading. Please wait.
Published byJocelin Jordan Modified over 9 years ago
1
Inheritance in Object-Oriented Programming Languages with an Emphasis on C++. Supervised by : Dr. Driss Kettani
2
Team Members Hadrouni Najm (Senior Student) El Khalifi Bachir (Junior Student) Rhomri Abdelghani (Senior Student)
3
Outline Introduction Composition Composition Syntax Inheritance Advantages Of Inheritance Single Inheritance Multiple Inheritance
4
Outline (cont.) Types Of Inheritance Public Inheritance Private Inheritance Protected Inheritance Conclusion Composition vs. Inheritance
5
Introduction Need For Code Reuse. Different Approaches. Composition Inheritance The Need For Inheritance.
6
Composition Composition is used intuitively with Standard Data type Composition is to embed within a class an object from another class. Definition
7
Composition Syntax class Y { public: X x; // Embedded object Y() { i = 0; } }; class X { int i; public: X() { i = 0; } };
8
Inheritance Definition Inheritance is to use a class as a Base class and Derive a new class from it. The Derived class Inherites all Data and behaviors of the Base Class. (Is Vs Is-Like)
9
Advantages of inheritance three When a class inherits from another class, there are three 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
10
Single Inheritance Implement an “is-a” relationship Single inheritance occurs when a derived class inherits from only one class.
11
Single Inheritance Derive a Class from a Base Class. Can be either Is or Is-Like Inheritance
12
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.
13
Multiple inheritance ClockRadio Clock Radio
14
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; } };
15
Types of Inheritance C++ allows some types of inheritance: Public Private Protected
16
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; } };
17
Public Inheritance class Y : public X { int i; // Different from X's i public: 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.
18
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); } ///:~
19
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
20
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; } };
21
Private Inheritance class Goldfish : Pet { // Private inheritance public: Pet::eat; // Name publicizes member Pet::sleep; // Both overloaded members exposed }; int main() { Goldfish bob; bob.eat(); bob.sleep(1); } ///:~
22
Protected Inheritance #include 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; } };
23
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); } ///:~
24
“This is private as far as the class user is concerned, but available to anyone who inherits from this class” Protected Inheritance
25
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
26
References Thinking In C++ by Bruce Eckel C++ Programming Language by Bjarne Stroustrup Charlie Calverts C++ Builder
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.