×

Method overriding in C++

What is method overriding?

Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With it, you can provide a specific function implementation that its base class has already implemented. In order to access the overridden function, one must use the scope resolution operator "::"

Example:1

#include <iostream>
using namespace std;
class Animal {
		public:
			//defined a class eats in the class function.
void eat(){
cout<<"Eating...";
		}
};
class Dog: public Animal
{
 public:
	 //creating the same function as in the base class.
 void eat()
		{
			 cout<<"Eating bread...";
		}
};
int main(void) {
	//created an object for the dog class.
	 Dog d = Dog();
	 //When implemented eat function in the eat function, the dog class will be printed   
	 d.eat();
	 return 0;
}

Output:

Method overriding in C++

Explanation:

In the above code, we created a class Animal and declared the member function eat. Then we declared another class, Dog. We inherited the animal class properties into the Dog class. In the dog class, we created the same member function as in the aminal function. The function will be overwritten when we create the member function the same as the base class. This process is known as method overriding. We have created an object for the dog class in the main function. So when we printed the eat function using the dog object, the statement in the eat function in the dog class will be printed.

Example:2

#include <iostream>
using namespace std;


class Base {
   public:
		 
    void print() {
        cout << "Base Function" << endl;
    }
};


class Derived : public Base {
   public:
		 //creating the same function as in the base class.
    void print() {
        cout << "Derived Function" << endl;
    }
};


int main() {
    Derived derived1;
		//created an object for the derived class.
		//When implemented a print function in the derived class, then the print function in the derived class will be printed    
    derived1.print();
    return 0;
}

Output:

Method overriding in C++

 Explanation:

In the above code, we created a class Base and declared member function print. Then we declared another class Derived. We have inherited the Base class properties into the Dervied class. In the Derived class, we created the same member function as in the Base class. We have created an object for the Derived class in the main function. So when we printed the print function using the Derived object, then the statement in the print function in the Derived class got printed.


Related Topics

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

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.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

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

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.