1 / 18

Object Oriented Programming Paradigm Lesson 07

Object Oriented Programming Paradigm Lesson 07. Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh. Inheritance. Inheritance.

maik
Download Presentation

Object Oriented Programming Paradigm Lesson 07

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming ParadigmLesson 07 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

  2. Inheritance

  3. Inheritance • Inheritance is a form of software reuse where a new class is created to absorb an existing class’s data and behaviors, and enhance them with new capabilities • Basic idea • The new class, the derived class, inherits the members of the existing class, known as the base class

  4. Inheritance • Inheritance: It is a process of creating new classes, called derived classes from existing or base classes. • The derived class inherits all the capabilities of the base class but can add additions to its own. • Base class will remain unchanged. • Inheritance provides code reusability, i-e debugged code is used in class and reused in derived class in different situation.

  5. Inheritance

  6. Derived Class and Base Class • C++ • Base and Drive Classes • Java • Sub Classand Super Class

  7. Introduction (continued) • A direct base class is the base class from which a derived class explicitly inherits. • An indirect base class is inherited from two or more levels up in the class hierarchy. • In single inheritance, a class is derived from one base class. • With multiple inheritance, a derived class inherits from multiple base classes.

  8. Class Hierarchy (Members of University Community)

  9. Access Specifiers • public:: Accessible within the class as well as from outside the class. • Note, base-class objects are NOT objects of their derived classes. • private:: The members declared as "private" can be accessed only within the same class and not from outside the class. • protected:: The members declared as "protected" cannot be accessed from outside the class, but can be accessed from a derived class. • This is used when inheritance is applied to the members of a class.

  10. Access Specifiers

  11. Derived Class • Member functions of derived class cannot directly access private members of base class • Example:– • Manager member functions in previous example cannot read manager’s own name! • Because data members of a class are by default private

  12. Protected Class • A base class’s protectedmembers can be accessed by • members and friends of the base class, and • members and friends of any class derived from the base class. • Derived-class member functions can refer to publicand protectedmembers of the base class. • By simply using their names

  13. Public, Protected, Private Inheritance class A { public: int i; protected: int j; private: int k; }; Class B : public A { // … }; Class C : protected A { // … }; Class D : private A { // … }; • Class A declares 3 variables • i is public to all users of class A • j is protected. It can only be used by methods in class A or its derived classes • k is private. It can only be used by methods in class A • Class B inherits publicly from A • i is again public to all users of class B • j is again protected. It can be used by methods in class B or its derived classes • Class C uses protected inheritance from A • i is now protected in C, so the only users of class C that can access i are the methods of class C • j is again protected. It can be used by methods in class C or its derived classes • Class D uses private inheritance from A • i and j are private in D, so users of D cannot access them, only methods of D itself

  14. Inheritance • Single Inheritance is method in which a derived class has only one base class. • In the example the derived class "Cube" has only one base class "Value“.

  15. Inheritance in_basic.cpp #include <iostream.h> class Value { protected: int val; public: void set_values(int a) {val=a;} }; class Cube: public Value { public: int cube() {return(val*val*val);} }; int main() { Cube cub; cub.set_values(5); cout<<"The cube of 5 is: "<<cub.cube() <<endl; } The Cube of 5 is: 125

  16. Inheritance • Multiple Inheritance is a method by which a class is derived from more than one base class. • In the example the derived class "Area" is derived from two base classes "Square" and "CShow".

  17. Inheritance in_mult.cpp #include <iostream.h> class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { public: void show(int i); }; void CShow::show (int i) { cout << "The area of the square is" << i << endl; } class Area: public Square, public CShow { public: int area() { return (l *l); } }; int main () { Area r; r.set_values (5); r.show(r.area()); } The area of the square is:: 25

  18. Multiple Inheritance

More Related