C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Inheritance - OOPs concept in C++


Tutorials Cpp

Topic

How is Inheritance used in OOP's concept of C++?



Explanation

Inheritance is a method by which new classes are created or derived from the existing classes. Using Inheritance some qualities of the base classes are added to the newly derived class, apart from its own features The advantage of using "Inheritance" is due to the reusability of classes in multiple derived classes. The ":" operator is used for inherting a class.

The following table lists the visibility of the base class members in the derived classes.

Base class visibility Derived class visibility
Public derivation. Private derivation. Protected derivation.
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected

Following are the different types of inheritance followed in C++.

Example:

#include <iostream.h>
class Value 
  {
   protected:
    int val;
   public:
    void set_values (int a)
      { val=a;}
  };
class Square: public Value 
  {
   public:
    int square()
      { return (val*val); }
  };
int main () 
{
  Square sq;
  sq.set_values (5);
  cout << "The square of 5 is::" << sq.square() << endl;
  return 0;
}

Result:
    The square of 5 is:: 25

In the above example the object "val" of class "Value" is inherited in the derived class "Square".








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting