chapter-10-inheritance.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
Chapter 10- Inheritance II PUC, MDRPUC, Hassan Chapter-10 INHERITANCE Introduction: Inheritance is another important aspect of object oriented programming. C++ allows the user to create a new cl...
Chapter 10- Inheritance II PUC, MDRPUC, Hassan Chapter-10 INHERITANCE Introduction: Inheritance is another important aspect of object oriented programming. C++ allows the user to create a new class (derived class) from an existing class (base class). Inheritance: Inheritance is the capability of one class to inherit properties from another class. Base Class: It is the class whose properties are inherited by another class. It is also called Super class. Derived Class: It is the class that inherits the properties from base class. It is also called Sub class. Need of Inheritance: Suppose X is a class already defined and we need to redefine another class Y has same properties of X and in addition its own. Suppose if we use direct option without using inheritance, it has following problems. o Code written in X is repeated again in Y which leads to unnecessary wastage of memory. o Testing to be done separately for both class X and class Y leads to waste of time. The above problem can be solved by using the concept of inheritance. If we use the code of X even in Y without rewriting it. The class Y inherits all the properties of X. The class X is called base class and the class Y is called derived class. The main advantages of Inheritance are: o Reusing existing code o Faster development time o Easy to maintain o Easy to extend o Memory Utilization The main disadvantage of Inheritance are: o Inappropriate use of inheritance makes programs more complicated. o Calling member functions using objects creates more compiler overheads. 1|Page Chapter 10- Inheritance II PUC, MDRPUC, Hassan Defining Derived Classes: A derived class is a composite class – it inherits members from the base class and adds member of its own. The general form of the derived class is given below. IMAGE Here, o class Keyword o derived_class_name Name of the derived class o : Shows the derivation from the base class. o Visibility Mode Specifies the type of derivation o base_class_name Name of the base class. o The use of a constructor can be cleverly done especially in those problems where it is necessary to initialize certain data members compulsorily. Example: Public Derived Class Private Derived Class Protected Derived Class class father //Base class class father //Base class class father //Base class { { { private: private: private: char name; char name; char name; public: public: public: char caste; char caste; char caste; int age; int age; int age; void readdata( ); void readdata( ); void readdata( ); }; }; }; class son : public father class son : private father class son : protected father { { { private: private: private: char gender; char gender; char gender; public: public: public: void display( ); void display( ); void display( ); }; }; }; Visibility mode: Visibility mode can be public, private or protected. The private data of base class cannot be inherited. o public: If inheritance is done in public mode, public members of the base class become the public member of derived class and protected member of base class become the protected member of derived class.. 2|Page Chapter 10- Inheritance II PUC, MDRPUC, Hassan o private: If inheritance is done in a private mode, public and protected members of base class become the private members of derived class. o protected: If inheritance is done in a protected mode, public and protected members of base class become the protected members of the base class. Derived Class Visibility Mode public private protected public public private protected Base class private Not inherited Not inherited Not inherited protected protected private protected Public Inheritance: When a base class is inherited as public, all public members of the base class become public members of derived class. The private members of the base class remain private to that class, and are nor accessible by members of the derived class. Example: A program illustrates public inheritance. #include Important #include 5 Marks class shape //Base Class { public: int side1, side2; }; class rectangle : public shape //Derived Class { public: int area; void compute( ) { area = side1 * side2; } }; void main( ) { rectangle R; // R is the object of derived class R.side1 = 5; // Data directly accessed by object R.side2 = 6; OUTPUT: R.compute( ); cout