SlideShare a Scribd company logo
1 of 25
   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

What's hot

Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsEng Teong Cheah
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 

What's hot (20)

Friend functions
Friend functions Friend functions
Friend functions
 
Inheritance
InheritanceInheritance
Inheritance
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Inheritance
InheritanceInheritance
Inheritance
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Inheritance
InheritanceInheritance
Inheritance
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 

Viewers also liked

Viewers also liked (10)

Genetic Basis of Inheritance
Genetic Basis of InheritanceGenetic Basis of Inheritance
Genetic Basis of Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Heredity
HeredityHeredity
Heredity
 
Principle of inheritance
Principle of inheritancePrinciple of inheritance
Principle of inheritance
 
Principles of inheritance and variation
Principles of inheritance and variationPrinciples of inheritance and variation
Principles of inheritance and variation
 
Genetics - Mendellian Principles of Heredity
Genetics - Mendellian Principles of HeredityGenetics - Mendellian Principles of Heredity
Genetics - Mendellian Principles of Heredity
 
Principles of Heredity
Principles of HeredityPrinciples of Heredity
Principles of Heredity
 
10.2 inheritance
10.2 inheritance10.2 inheritance
10.2 inheritance
 
Chapter 3: HEREDITY AND VARIATIONS
Chapter 3: HEREDITY AND VARIATIONSChapter 3: HEREDITY AND VARIATIONS
Chapter 3: HEREDITY AND VARIATIONS
 
Heredity, inheritance, and variation
Heredity, inheritance, and variationHeredity, inheritance, and variation
Heredity, inheritance, and variation
 

Similar to Inheritance

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objectsDeepak Singh
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
Class&objects
Class&objectsClass&objects
Class&objectsharivng
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 

Similar to Inheritance (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
inheritance
inheritanceinheritance
inheritance
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
class c++
class c++class c++
class c++
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Class and object
Class and objectClass and object
Class and object
 
Class&objects
Class&objectsClass&objects
Class&objects
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
C++ classes
C++ classesC++ classes
C++ classes
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Inheritance

  • 1.
  • 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.