Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Single Level inheritance in C++ language

Posted on February 16, 2017January 20, 2020

Table of Contents

  • Single Level inheritance in C++ language
      • Program 2

Single Level inheritance in C++ language

In this tutorial, we will discuss the OOP concept of single-level inheritance in C++ language.

Single level inheritance is easy to understand as it is the most simple process compared to all other inheritances. It is when a class extends only to another class (Single child is derived from a single parent) .

Single level inheritance only has one parent and one child class. Child class has its own property and it’s authorized  to access the property of parent class. The diagram below shows that B extends only to one class which is A. Here,  A is the parent class or base class.

So, when a single class is derived from a single parent class, it is called a  single inheritance or simple inheritance.
 
Syntex of inheritance in C++
class base_A{
datamembers....
methods .....
};
class derived_B: public base _A{
datamembers....
methods .....
};
Single Level inheritance in C++ language
Diagram

Program 1
 
Find the sum of two numbers using single inheritance in C++
 
Single Level inheritance in C++ language
Example
 
 In the above program, class base has protected members and class derived has public members. The class derived inherits all the members of base class

Program 2

Find the cube of a value using single  inheritance in C++
 
Single Level inheritance in C++ language
Example

In the above program, class value and class cobe has protected and public memebers. The class cube inherits all the producted and public members of value class

Program 3
Single Level inheritance in C++ language
Example

When the above code is executed, it produces the following result

Single Level inheritance in C++ language
Output
Explanation for the above program
#include <iostream>
using namespace std;
class student //parant class
{// start of student class
public:
    int index_no;
    char name[20];  //assign the variable in class student
    void getdata() // create a method for get data
    {
        cout << “Enter your index no:- t”<< endl;
        cin>>index_no;
        cout << “Enter your name:- t”<< endl;
        cin>>name;
    }
}; // end of student class
class marks:public student  //class marks inherit properties from class students 
{  //start of class marks
public:
    int m1,m2,m3,m4,total;
    float percentage;//assign variables in class marks
    void getmarks() //create a method for get marks
    {
        getdata();
        cout << “Enter your marks 1:- t”<< endl;
        cin>>m1;
        cout << “Enter your marks 2:- t”<< endl;
        cin>>m2;
        cout << “Enter your marks 3:- t”<< endl;
        cin>>m3;
        cout << “Enter your marks 4:- t”<< endl;
        cin>>m4;
    }
void display()
{
    cout<<“index notnametmarks 1tmarks 2tmarks 3tmarks 4ttpercentage”<<endl;
    cout<<index_no<<“tt”<<name<<“t”<< m1<<“t”<< m2<<“t”<<m3<<“t”<<m4<<“t”<<“t”<<percentage;
}
}; //end of class marks
int main()
{
    marks std;   //create a object for class marks
    std.getmarks();   //call method using object
    std.display(); //call method using object
    return 0;
}
 
Single level inheritance in java          Inheritance in C++
 
Multi levelinheritance in Java           Multilevel inheritance in C++
 
 Inheritance in Java                            Multiple Inheritance in C++
 
Hierarchical Inheritance in Java
 
Interface in Java

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes