SlideShare a Scribd company logo
Inheritance
   The OO principle of inheritance enables you
    to create a generalized class and then derive
    more specialized classes from it.
   Inheritance is the ability to take on the
    characteristics of the class or derived class on
    which it is based.
   Specifies an “is-a” kind of relationship
Person



      Employee     Student



Full-time   Part-time
Employee    Employee
Shape



Rectangle         Circle



 Square
   New classes that we create from the existing
    class are called derived classes; the existing
    classes are called base classes.
class className:memberAccessSpecifier baseClassName
{
   memberList;

};
    Where:
      memberAccessSpecifier – is public, private, or protected. When no
       memberAccessSpecifier is specified, it is assumed to be a private
       inheritance.
class Circle : public Shape
{
   .
   .
   .
};
class Circle : private Shape
{
   .
   .
   .
};
1.   The private members of a base class are
     private to the base class; hence the
     members of the derived class cannot
     directly access them. In other words, when
     you write the definitions of the member
     functions of the derived class, you cannot
     directly access the private members of the
     base class.
2.   The public members of a base class can be
     inherited either as public members or as
     private members by the derived class. That
     is, the public members of the base class can
     become either public or private members of
     the derived class.
3.   The derived class can include additional
     members – data and/or functions.
4.   The derived class can redefine the public
     member functions of the base class. That is,
     in the derived class, you can have a member
     function with the same name, number and
     types of parameters as function in the base
     class. However, this redefinition applies
     only to the object of the derived class, not to
     the objects of the base class.
5.   All member variables of the base class are
     also member variables of the derived class.
     Similarly, the member functions of the base
     class(unless redefined) are also member
     functions of the derived class. (Remember
     Rule 1 when accessing a member of the base
     class in the derived class.
class Derived:Base           class Base
{                            {
   int y;                       int x;
   public :                     public :
       void print() const;      void print()const;
};                           };
void Derived::print()const   void Base::print()const
{                            {
   cout<<y<<endl;               cout<<x<<endl;
}                            }
   To redefine a public member function of a
    base class in the derived class, the
    corresponding function in the derived class
    must have the same name, number, and
    types of parameters.
class RectangleType
{
   public:
       void setDimension(double, double);
       double getLength()const;
       double getWidth() const;
       double area()const;
       double perimeter()const;
       void print()const;
       RectangleType();
       RectangleType(double, double);
   private:
       double length;
       double width;
};
#include "RectangleType.h"         double RectangleType::getLength
#include<iostream>                   ()const
using namespace std;               {
                                     return length;
void RectangleType::setDimension   }
   (double l, double w)            double RectangleType::getWidth
{                                    ()const
   if (l>=0)                       {
         length = l;                 return width;
   else                            }
         length =0;
    if (w>=0)
         width = w;
    else
         width = 0;
}
double RectangleType::area()const   RectangleType::RectangleType(double l,
{                                      double w)
   return length * width;           {
}                                      setDimension(l,w);
double RectangleType::perimeter     }
   ()const                          RectangleType::RectangleType()
{                                   {
   return 2*(length + width);          length =0;
}                                      width =0;
void RectangleType::print() const   }
{
   cout<<"Length = "<<length
        <<"Width = " <<width;
}
 Define a class named BoxType
 BoxType contains data members that stores the length,
  width and height of a box.
 It has the following member functions :
     Function that sets the dimension of the box
     Function that sets a value for each data member of the class
     Function that returns the value of each data member of the
        class
       Function that prints the values of the data members of the class
       Function that computes and returns the area of the box
       Function that computes and returns the volume of the box
       Default constructor which initializes data members to 0
       Parameterized constructor which initializes data member to a
        value set by the object of the class
   In general, while writing the definitions of the
    member functions of a derived class to
    specify a call to a public member function of
    the base class we do the following:
     If the derived class overrides a public member
     function of the base class, then to specify a call to
     that public member function of the base class use
     the name of the base class followed by the scope
     resolution operator, ::, followed by the function
     name with the appropriate parameter list.
 If the derived class does not override a public
 member function of the base class, you may
 specify a call to that public member function by
 using the name of the function and the
 appropriate parameter list.
   Recall:
     private members of a class are private to the class and
      cannot be directly accessed outside the class. Only
      member functions of that class can access the private
      members.
     If public, anyone can access that member
     So for a base class to give access to a member to its
      derived class and still prevent its direct access outside the
      class, you must declare the member under the
      memberAccessSpecifier protected.
      ▪ The accessibility of a protected class is between public and private
      ▪ A derived class can directly access the protected members of the base
        class.
   Example:
    class B : memberAccessSpecifier A
    {
       :
       :
    };
   memberAccessSpecifier is either private, public
    or protected
   If memberAccessSpecifier is public – that is
    inheritance is public - then:
     The public members of A are public members of
      B. They can be directly accessed in class B.
     The protected members of A re protected
      members of B. They can be directly accessed by
      the member functions of B.
     The private members of A are hidden in B. They
      can be accessed by the member functions of B
      through the public and protected members of A.
   If memberAccessSpecifier is protected– that is
    inheritance is protected - then:
     The public members of A are protected members of B.
      They can be accessed by the member functions of B.
     The protected members of A are protected members
      of B. They can be accessed by the member functions
      of B.
     The private members of A are hidden in B. They can
      be accessed by the member functions of B through
      the private or protected members of A.
   If memberAccessSpecifier is private– that is
    inheritance is private - then:
     The public members of A are private members of B.
      They can be accessed by the member functions of B.
     The protected members of A are private members of
      B. They can be accessed by the member functions of
      B.
     The private members of A are hidden in B. They can
      be accessed by the member functions of B through
      the private or protected members of A.

More Related Content

PPTX
Class and object
PPTX
Inheritance
PPTX
Friend function & friend class
PPTX
OOP C++
PPT
Class and object in C++
PPTX
Polymorphism
PPTX
Inheritance, friend function, virtual function, polymorphism
PPTX
Inheritance
Class and object
Inheritance
Friend function & friend class
OOP C++
Class and object in C++
Polymorphism
Inheritance, friend function, virtual function, polymorphism
Inheritance

What's hot (20)

PPTX
Friend functions
PPT
Inheritance
PDF
Chapter23 friend-function-friend-class
PPT
Inheritance OOP Concept in C++.
PDF
Class and object in C++ By Pawan Thakur
DOCX
JAVA Notes - All major concepts covered with examples
PPTX
Inheritance
PDF
Lect 1-java object-classes
PPTX
inheritance c++
PPT
Friends function and_classes
PPTX
Encapsulation
PPT
Class and object in c++
PPSX
Seminar on java
PPT
Inheritance
PPTX
Learn C# Programming - Encapsulation & Methods
PPTX
Object as function argument , friend and static function by shahzad younas
PDF
CLASS & OBJECT IN JAVA
PPTX
inheritance in C++
PDF
How to write you first class in c++ object oriented programming
PPT
friend function(c++)
Friend functions
Inheritance
Chapter23 friend-function-friend-class
Inheritance OOP Concept in C++.
Class and object in C++ By Pawan Thakur
JAVA Notes - All major concepts covered with examples
Inheritance
Lect 1-java object-classes
inheritance c++
Friends function and_classes
Encapsulation
Class and object in c++
Seminar on java
Inheritance
Learn C# Programming - Encapsulation & Methods
Object as function argument , friend and static function by shahzad younas
CLASS & OBJECT IN JAVA
inheritance in C++
How to write you first class in c++ object oriented programming
friend function(c++)
Ad

Viewers also liked (20)

PPTX
บุคลากรครูที่เกษียณ
PPTX
My sister´s keeper
PPT
Pearson Career Workforce Education
PDF
Socialmedia2.0
PPT
APARTMENT IN WARSAW
PPTX
บุคลากรครูที่เกษียณ12
PPT
FM&amp;P 2011 - Diageo
DOC
Aa Sponsor11 Proposal Jamaica
PPTX
ท่องเที่ยวเชิงประวัติศาสตร์
PPT
20070901.mydomain
PPTX
Meals on Wheels
PPTX
Principales monedas
DOC
Cja 463 policy development paper
PPTX
Federal Legislative and Regulatory Update
PDF
Media Placement Portfolio Lindsay Krupa
PPTX
Actividades quandary
PPTX
Life with gene
PPTX
บุคลากรครูที่เกษียณ12
PPT
A common word
PPT
방배동빌라
บุคลากรครูที่เกษียณ
My sister´s keeper
Pearson Career Workforce Education
Socialmedia2.0
APARTMENT IN WARSAW
บุคลากรครูที่เกษียณ12
FM&amp;P 2011 - Diageo
Aa Sponsor11 Proposal Jamaica
ท่องเที่ยวเชิงประวัติศาสตร์
20070901.mydomain
Meals on Wheels
Principales monedas
Cja 463 policy development paper
Federal Legislative and Regulatory Update
Media Placement Portfolio Lindsay Krupa
Actividades quandary
Life with gene
บุคลากรครูที่เกษียณ12
A common word
방배동빌라
Ad

Similar to Inheritance (20)

PPT
classes data type for Btech students.ppt
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
PDF
chapter-7-classes-and-objects.pdf
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPT
inheritance
PPTX
OOPs & C++ UNIT 3
PPTX
class c++
PDF
Chapter18 class-and-objects
PPT
4 Classes & Objects
PPTX
Access controlaspecifier and visibilty modes
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PDF
PDF
Class and object
ODP
Class&objects
PPT
Classes and objects
PPTX
C++ presentation
PPTX
+2 CS class and objects
PPT
object oriented programming language by c++
PPTX
C++ classes
classes data type for Btech students.ppt
11 Inheritance.ppt
Inheritance in C++
chapter-7-classes-and-objects.pdf
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
inheritance
OOPs & C++ UNIT 3
class c++
Chapter18 class-and-objects
4 Classes & Objects
Access controlaspecifier and visibilty modes
cpp class unitdfdsfasadfsdASsASass 4.ppt
Class and object
Class&objects
Classes and objects
C++ presentation
+2 CS class and objects
object oriented programming language by c++
C++ classes

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Getting Started with Data Integration: FME Form 101
PDF
Empathic Computing: Creating Shared Understanding
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
August Patch Tuesday
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Mushroom cultivation and it's methods.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
Unlocking AI with Model Context Protocol (MCP)
Mobile App Security Testing_ A Comprehensive Guide.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Getting Started with Data Integration: FME Form 101
Empathic Computing: Creating Shared Understanding
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
NewMind AI Weekly Chronicles - August'25-Week II
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
August Patch Tuesday
Advanced methodologies resolving dimensionality complications for autism neur...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
A comparative study of natural language inference in Swahili using monolingua...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Mushroom cultivation and it's methods.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
A comparative analysis of optical character recognition models for extracting...
Machine Learning_overview_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars

Inheritance

  • 2. The OO principle of inheritance enables you to create a generalized class and then derive more specialized classes from it.  Inheritance is the ability to take on the characteristics of the class or derived class on which it is based.  Specifies an “is-a” kind of relationship
  • 3. Person Employee Student Full-time Part-time Employee Employee
  • 4. Shape Rectangle Circle Square
  • 5. New classes that we create from the existing class are called derived classes; the existing classes are called base classes.
  • 6. class className:memberAccessSpecifier baseClassName { memberList; };  Where:  memberAccessSpecifier – is public, private, or protected. When no memberAccessSpecifier is specified, it is assumed to be a private inheritance.
  • 7. class Circle : public Shape { . . . };
  • 8. class Circle : private Shape { . . . };
  • 9. 1. The private members of a base class are private to the base class; hence the members of the derived class cannot directly access them. In other words, when you write the definitions of the member functions of the derived class, you cannot directly access the private members of the base class.
  • 10. 2. The public members of a base class can be inherited either as public members or as private members by the derived class. That is, the public members of the base class can become either public or private members of the derived class.
  • 11. 3. The derived class can include additional members – data and/or functions. 4. The derived class can redefine the public member functions of the base class. That is, in the derived class, you can have a member function with the same name, number and types of parameters as function in the base class. However, this redefinition applies only to the object of the derived class, not to the objects of the base class.
  • 12. 5. All member variables of the base class are also member variables of the derived class. Similarly, the member functions of the base class(unless redefined) are also member functions of the derived class. (Remember Rule 1 when accessing a member of the base class in the derived class.
  • 13. class Derived:Base class Base { { int y; int x; public : public : void print() const; void print()const; }; }; void Derived::print()const void Base::print()const { { cout<<y<<endl; cout<<x<<endl; } }
  • 14. To redefine a public member function of a base class in the derived class, the corresponding function in the derived class must have the same name, number, and types of parameters.
  • 15. class RectangleType { public: void setDimension(double, double); double getLength()const; double getWidth() const; double area()const; double perimeter()const; void print()const; RectangleType(); RectangleType(double, double); private: double length; double width; };
  • 16. #include "RectangleType.h" double RectangleType::getLength #include<iostream> ()const using namespace std; { return length; void RectangleType::setDimension } (double l, double w) double RectangleType::getWidth { ()const if (l>=0) { length = l; return width; else } length =0; if (w>=0) width = w; else width = 0; }
  • 17. double RectangleType::area()const RectangleType::RectangleType(double l, { double w) return length * width; { } setDimension(l,w); double RectangleType::perimeter } ()const RectangleType::RectangleType() { { return 2*(length + width); length =0; } width =0; void RectangleType::print() const } { cout<<"Length = "<<length <<"Width = " <<width; }
  • 18.  Define a class named BoxType  BoxType contains data members that stores the length, width and height of a box.  It has the following member functions :  Function that sets the dimension of the box  Function that sets a value for each data member of the class  Function that returns the value of each data member of the class  Function that prints the values of the data members of the class  Function that computes and returns the area of the box  Function that computes and returns the volume of the box  Default constructor which initializes data members to 0  Parameterized constructor which initializes data member to a value set by the object of the class
  • 19. In general, while writing the definitions of the member functions of a derived class to specify a call to a public member function of the base class we do the following:  If the derived class overrides a public member function of the base class, then to specify a call to that public member function of the base class use the name of the base class followed by the scope resolution operator, ::, followed by the function name with the appropriate parameter list.
  • 20.  If the derived class does not override a public member function of the base class, you may specify a call to that public member function by using the name of the function and the appropriate parameter list.
  • 21. Recall:  private members of a class are private to the class and cannot be directly accessed outside the class. Only member functions of that class can access the private members.  If public, anyone can access that member  So for a base class to give access to a member to its derived class and still prevent its direct access outside the class, you must declare the member under the memberAccessSpecifier protected. ▪ The accessibility of a protected class is between public and private ▪ A derived class can directly access the protected members of the base class.
  • 22. Example: class B : memberAccessSpecifier A { : : };  memberAccessSpecifier is either private, public or protected
  • 23. If memberAccessSpecifier is public – that is inheritance is public - then:  The public members of A are public members of B. They can be directly accessed in class B.  The protected members of A re protected members of B. They can be directly accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the public and protected members of A.
  • 24. If memberAccessSpecifier is protected– that is inheritance is protected - then:  The public members of A are protected members of B. They can be accessed by the member functions of B.  The protected members of A are protected members of B. They can be accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.
  • 25. If memberAccessSpecifier is private– that is inheritance is private - then:  The public members of A are private members of B. They can be accessed by the member functions of B.  The protected members of A are private members of B. They can be accessed by the member functions of B.  The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.