0% found this document useful (0 votes)
271 views

Function Overriding Using C++

Function overriding in C++ allows a function in a derived class to override a function in the base class with the same name, return type, and parameters. This is achieved at runtime. Function overriding is useful when the derived class needs to provide a different implementation of the function compared to the base class. To call the overridden function from the derived class, the scope resolution operator (::) must be used.

Uploaded by

vishal mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views

Function Overriding Using C++

Function overriding in C++ allows a function in a derived class to override a function in the base class with the same name, return type, and parameters. This is achieved at runtime. Function overriding is useful when the derived class needs to provide a different implementation of the function compared to the base class. To call the overridden function from the derived class, the scope resolution operator (::) must be used.

Uploaded by

vishal mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Function Overriding using C++

Function Overriding
• When the base class and derived class have
member functions with exactly the same
name, same return-type, and same arguments
list, then it is said to be function overriding.
• The following example shows how function
overriding is done in C++, which is an
objectoriented programming language −
What is function overriding?
• Function overriding is a concept in object-oriented programming
which allows a function within a derived class to override a function
in its base class, but with a different signature (and usually with a
different implementation).
• A key aspect of this is that it is not possible for the derived class to
“undo” the changes to the base class, or at least it is not possible
without editing the base class further.
• A common use of function overriding is to provide a default
implementation in the base class, and then overriding with a
specific implementation in the derived class.
Function Overloading vs. Function Overriding
• Function overloading is achieved at compile time and can be done in the base
class and derived class. It helps to provide multiple definitions of the functions
by changing the signature of the functions such as data type of parameters or
return types. 
• Function overriding is achieved at runtime. In overriding, the base class is
redefined in the derived class with the same return type and parameters.
Other differences between function overriding and function overloading in C++
are:
• 1. Inheritance
– Function overriding can be used only with class inheritance while function overloading does not require
class inheritance. 
• 2. Function Signature
– Overloaded functions differ in signature either in the number of parameters or the type of parameters. In
function overriding, the function signatures remain the same.
• 3. Function Scope
– Overridden functions vary in scope while overloaded functions have the same scope.
• 4. Function Behavior
– Function overriding is essential when a derived class function must perform differently or with added
functionality than the base class function. Function overloading is implemented when functions with the
same name need to have different behaviours depending upon the parameters passed to them.
What are the differences between function
overriding and overloading?
• Function overriding occurs when you create function
with the same name as a function that already exists
in a base class.
• When this happens, the new function will replace the
existing function and can be used in place of the
original function.
• Overloading occurs when you create functions with
the same name but different parameters.
• When this happens, the new function will be called in
addition to the original function, and both functions
can be used in any context without any problem.
#include <iostream> }
using namespace std; };

class A { int main() {


public: B obj;
void display() { obj.display();
cout<<"Base class"; return 0;
} }
}; It will produce the following
output
class B:public A {
public: Derived Class
void display() {
cout<<"Derived Class";
Access Overridden Functions in C++
• You must use the scope resolution operator, “::” to access the
overridden function. Another way to access the overridden function is
by using the pointer of the base class to point to an object of the
derived class and calling the function through the pointer.
#include <iostream.h> int main() {
class A { B obj,obj1;
public: obj.display();
void display() { obj1.A::display();
cout<<"Base class"; return 0;
} }
}; It will produce the following
output
class B:public A {
public: Derived Class
void display() { Base Class
cout<<"Derived Class";
} };
Working of the Access of overridden function
Working of the overridden function call from
the Derived class

You might also like