×

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

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

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.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 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.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 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.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 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.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.