Sharp Tutorial
inheritance in C++

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

introduction to c++

  • C++ Home
  • Introduction to C++
  • History features
  • Sample Programs
  • cout , cin , endl
  • Data type and Operators
  • if else in c++
  • loop in c++

Oops in C++

  • Object / Class
  • Constructor
  • Function & Constructor Overloading
  • Inheritance
  • Overriding

Advanced C++

  • Strings in C++
  • Exception Handling
  • Special Functions
  • C++ Programs

inheritance in c++

C++ supports Inheritance , the main feature of object oriented programming through which the sub class inherits the features of its base class. 

Inheritance is the (super-sub) or (parent-child) or (base-derived) class relationship.

How to achieve inheritance in c++

1 single inheritance

#include<iostream>
using namespace std;
class A
{
public:
int i=10;
void display()
{
cout<<"i in class A ="<<i <<endl;
}
};

class B:A // Here Class B inherits class A
{
public:
int j=20;
void show()
{
cout<<"i=" <<i << "j="<<j; // i is inherited from A class.
}
};
int main()
{
A a1;
a1.display();
B b1;
b1.show();
return 0;
}

Above program will give the following output.

2 multilevel inheritance

#include<iostream>
using namespace std;
class A
{
public:
int i=10;
};
class B:A
{
public:
int j=20;
};
class C:B
{
int k=30;
// i and j both may be used here as inheriting.
};

3 multiple inheritance

#include<iostream>
using namespace std;
class A
{
public:
int i=10;
};
class B
{
public:
int j=20;
};
class C:(B,A)
{
int k=30;
// i and j both may be used here as inheriting.
};

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial