×

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

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

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

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

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.

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.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

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

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.