How to call a parent class function from derived class function?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to call a parent class function from derived class function?

How to Call a Parent Class Function from Derived Class Function?

So you've got a parent class and a derived class, and you want to call a function defined in the parent class from within the derived class. Fear not, because I'm here to guide you through this common issue with some easy solutions. Let's dive right in!

Understanding the Problem

In object-oriented programming, inheritance allows us to create a hierarchy of classes where the derived class inherits properties and behaviors from the parent class. However, there may be scenarios where you need to use a function defined in the parent class within the derived class.

Let's take a look at an example to better understand the problem:

class Parent {
public:
    void print() {
        cout << "This is the parent class." << endl;
    }
};

class Child : public Parent {
public:
    void print() {
        cout << "This is the child class." << endl;
    }
};

In this example, we have a Parent class with a print() function. We also have a Child class that is derived from Parent and overrides the print() function.

The goal here is to make a call to the parent's print() function from within the child's print() function.

The Super Keyword (C++11 and above)

Starting from C++11, we have the super keyword to reference the parent class. You can use it to call the parent class function within the derived class.

Here's how you can achieve this using the super keyword:

class Child : public Parent {
public:
    void print() {
        Parent::print();  // Call parent's print() function using the super keyword
        cout << "This is the child class." << endl;
    }
};

In the above example, Parent::print() is used to call the print() function of the parent class. This ensures that both the parent and child class print statements are executed when invoking Child::print().

The Scope Resolution Operator

If you're working with an older version of C++ that doesn't support the super keyword, you can still achieve the desired result using the scope resolution operator.

Here's an example illustrating this approach:

class Child : public Parent {
public:
    void print() {
        Parent::print();  // Call parent's print() function using the scope resolution operator
        cout << "This is the child class." << endl;
    }
};

By prefixing Parent:: to the function name, you explicitly indicate that you want to call the print() function defined in the parent class.

Final Thoughts

And just like that, you now know how to call a parent class function from a derived class function! Whether you have the super keyword or need to use the scope resolution operator, these techniques will help you achieve the desired behavior in your code.

Remember, understanding the relationship between parent and derived classes is crucial when working with inheritance. Utilizing the appropriate syntax ensures that your program behaves as expected.

So go ahead and enhance your code by utilizing the power of inheritance and function calls between parent and derived classes! Happy coding! 🔥

Have you faced any challenges while working with inherited classes? How did you solve them? Share your experiences in the comments below and let's learn together!

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my