180 likes | 287 Vues
Explore how inheritance allows classes to inherit data and behaviors, enhance them, and create new capabilities, facilitating code reusability. Learn about base classes and derived classes, access specifiers, and class hierarchy examples.
E N D
Object Oriented Programming ParadigmLesson 07 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh
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
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.
Derived Class and Base Class • C++ • Base and Drive Classes • Java • Sub Classand Super Class
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.
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.
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
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
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
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“.
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
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".
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