×

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class). In this article, we will discuss single-level inheritance with its key properties, access modifiers, algorithms, and examples.

Single Level Inheritance:

Single-level inheritance is the process by which a derived class inherits from a single base class. By giving the derived class access to the public and protected members of the base class, it promotes code reuse and modularity.

Key Properties:

  • Base Class: The class having inheritable properties is known as the base class.
  • Derived Class: The derived class is the one that inherits the base class's properties.

Access modifiers:

  • public: Members who were public and protected in the base class remain public and protected in the derived class.
  • protected: The derived class grants protection to members of the base class who are both public and protected.
  • private: Public and protected members of the base class become private in the derived class.
  • Redundancy is decreased in the derived class by reusing base class code.

Algorithm:

  • Define a Base Class with a method that prints a message.
  • Define a Derived Class that inherits from the Base Class.
  • Add a method in the Derived Class to print another message.
  • Create an Object of the Derived Class in main().
  • Call the methods of both the Base and Derived Classes using the Derived Class object.
  • Program ends.

Pseudo Code:

BEGIN

DEFINE class Parent

FUNCTION show()

PRINT "This is the parent class."

DEFINE class Child INHERITS Parent

FUNCTION display()

PRINT "This is the child class."

FUNCTION main()

CREATE object of Child class

CALL object.show() // Calls base class function

CALL object.display() // Calls derived class function

END FUNCTION

END

Example 1:

Let us take an example to illustrate the Single-level inheritance in C++.

#include <iostream>

using namespace std;

// Base class

class Parent {

public:

void show() {

cout << "This is the parent class." << endl;

}

};

// Derived class inheriting from Base class

class Child : public Parent {

public:

void display() {

cout << "This is the child class." << endl;

}

};

int main() {

Child obj;

obj.show(); // Inherited function

obj.display(); // Own function

return 0;

}

Output:

This is the parent class.

This is the child class.

Example 2:

The base class "Animal" and the derived class "Dog" from a real-world example will be covered. The derived class inherits properties from the base class and adds its own behavior.

#include <iostream>

using namespace std;

// Base class

class Animal {

public:

void eat() {

cout << "Animal is eating." << endl;

}

};


// Derived class inheriting from Animal

class Dog : public Animal {

public:

void bark() {

cout << "Dog is barking." << endl;

}

};

int main() {

Dog myDog;

myDog.eat(); // Inherited function from Animal class

myDog.bark(); // Function of Dog class

return 0;

}

Output:

Animal is eating.

Dog is barking.

Explanation:

Base Class (Animal):

  • It contains a function eat() that prints "Animal is eating.".

Derived Class (Dog):

  • It inherits from the Animal class.
  • It has its own function bark(), which prints "Dog is barking.".

Main Function (main()):

  • An object myDog of the Dog class is created.
  • The eat() function (from Animal) and bark() function (from Dog) are called using the object.

Example 3:

Let us take another example to illustrate the Single-level inheritance in C++.

#include <iostream>

using namespace std;

// Base class

class Vehicle {

public:

void startEngine() {

cout << "Engine started." << endl;

}

};


// Derived class inheriting from Vehicle

class Car : public Vehicle {

public:

void honk() {

cout << "Car is honking: Beep Beep!" << endl;

}

};

int main() {

Car myCar;

myCar.startEngine(); // Inherited function from Vehicle class

myCar.honk(); // Function of Car class

return 0;

}

Output:

Engine started.

Car is honking: Beep Beep!

Explanation:

Base Class (Vehicle):

  • It contains a method startEngine() that prints "Engine started."

Derived Class (Car):

  • Inherits from the Vehicle class.
  • Has its own method honk(), which prints "Car is honking: Beep Beep!".

Main Function (main()):

  • An object myCar of the Car class is created.
  • The startEngine() method (from Vehicle) and honk() method (from Car) are called using the object.

Conclusion:

In conclusion, a child class can inherit from a single base or parent class due to the basic idea and characteristic of single-level inheritance in C++ object-oriented programming. Its capacity to effectively manage a long inheritance hierarchy perfect for multi-level and multiple inheritance is one of the most important features of C++ programming. A whole new code structure is produced without altering the current one by replacing outdated functionalities with new ones. One of the practical aspects of this method is the creation of a class hierarchy; for instance, the descendant classes “Dog” and “Cat” may use the “Animal” class. Thus, innovations in the field are made possible by a single traditional legacy.

Frequently Asked Question’s:

1. What does C++ single-level inheritance mean?

Code reuse is made possible via single-level inheritance, which permits a derived class to inherit from a single base class and use the parent class’s attributes and behaviors.

2. What benefits can one-level inheritance offer?

It guarantees better modular design by encouraging code reuse, lowering redundancy, enhancing maintainability, and permitting the extension of current functionality without changing the base class.

3. How does single-level inheritance work in C++?

The class Derived: access_specifier Base is used to allow a derived class to inherit from a base class and access non-private members of the base class.

4. Which access specifiers are used in single-level inheritance processes?

The public, protected, and private access specifiers control the accessibility of the inherited members of the derived class to ensure data protection and encapsulation.

5. Can functions of a derived class supersede those of a base class in single-level inheritance?

Yes, a derived class can override a base class function using function overriding if the base class function is declared as virtual, permitting runtime polymorphism.


Related Topics

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.