×

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

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with 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.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

Stringstream in C++ and its applications

In this tutorial, we will explore what the stringstream in C++ is. We will also learn its application. What is stringstream? With the aid of a stringstream, user can read from a...

2 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.