Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Function Overriding in C++ Explained

Updated on 29 January, 2024

5.64K+ views
7 min read

What Is A Function?

A function is a piece that belongs to a code for executing a stipulated task in a program. It is important to have first-hand knowledge about inheritance to learn function overriding inheritance implementation is a mandate to this function. A C++ function is a collection of statements clubbed together for executing a task. All programs have a main() function, and particular programs have added functions. The function declaration sends the function name, return type, and parameters to the compiler to determine the function body by the function definition. A function can be a procedure, a method, or a subroutine, and all C++ functions consist of a header and a body. 

Syntax of a C++ function:-

return_type function_name(parameter list) {
function body
}

Components:-

  • Return Type- This denotes the function’s return value type.
  • Function Name- This represents the function’s name which makes up the function signature and the parameter list.
  • Parameters- This is a placeholder for the value that the function returns. When a function is called, the value passed to the parameter is known as the actual parameter.
  • Function Body- The function body comprises the list of statements defining the function’s task.

How to Call a Function

A function must be invoked or called before use, after which the control is transferred to it for performing the necessary task. The return statement gives back the control to the main program after this. The parameters and the function name must pass to call a function. Arguments can be passed to a function in multiple ways while calling it. The call types are as follows:-

  • Call by Value
  • Call by Reference
  • Call by Pointer

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Function Overriding in C++

The C++ override is an idea via which a function given the same name is defined. The base class function in overriding is redefined within the derived class, which overrides the base class function. Implementing the run-time polymorphism can also be defined as function overriding. Here, the run-time of the program overrides the function.  

Function overriding allows programmers to use a function in the child class found in its parent class. As a result, the child class inherits every data member and the member functions found in the parent class. For overriding any child class functionality, you must implement function overriding. Function overriding refers to making a new version of the parent class function within the child class. 

Syntax to Implement Function Overriding in C++

When the same function defined in both the based and the derived class is called using the object from the derived class, it executes the derived class function. This is function overriding in C++. 

Down below is the most used syntax for the implementation of the function overriding in C++:

// C++ program for demonstrating function overriding

#include <iostream>
using namespace std;
class Base {
   public:
void print() {
     cout << “Base Function” << endl;
}
};
class Derived : public Base {
   public:
void print() {
     cout << “Derived Function” << endl;
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}

Output

Derived Function

This redefines the base class function within the derived class. Hence, the return-type, function_parameters, and function_name must be the same for achieving function overriding.

How Function Overriding Works in C++

OOPs must allow derived classes to inherit the features of the parent class. Function overriding enables the programmers to override any functionality within a class in a specific derived class. This is especially useful when a child class needs its functionality variant. 

Down below is a simple example of function overriding in C++ for your better understanding:

#include <iostream>
using namespace std;
class parent_class
{
public:
virtual void print()
{
     cout << “\nThis is print() method”
             ” of BaseClass”;
}
};
class derived_class : public parent_class
{
public:
// Function Overriding – new definition of
// print method of base class
void print()
{
     cout << “\nThis is print() method”
             ” of the Derived Class”;
}
};
// Driver code
int main()
{
derived_class obj;
obj.print();
}

Access Overridden Function in C++

The scope resolution operator is used for accessing the overridden function of the base class in C++. The overridden function can also be accessed by using a base class pointer to point to an object of the derived class and then call the function from the pointer.

// C++ program to access overridden function

// in main() using the scope resolution operator ::

#include <iostream>
using namespace std;
class Base {
   public:
void print() {
     cout << “Base Function” << endl;
}
};
class Derived : public Base {
   public:
void print() {
     cout << “Derived Function” << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
// access print() function of the Base class
derived2.Base::print();
return 0;
}

Output

Derived Function

Base Function

The statement in the syntax; derived2.Base::print(); gives access to the the print() function of the Base class.

Call Overridden Function From Derived Class

You can use inheritance to change the behavior of a function. However, sometimes, you don’t need to change or replace the base/parent class functionality. Instead, more functionality must be added.

Below is an example of using the call overridden function from a derived class.

// C++ program to call the overridden function

// from a member function of the derived class

#include <iostream>
using namespace std;
class Base {
   public:
void print() {
     cout << “Base Function” << endl;
}
};
class Derived : public Base {
   public:
void print() {
     cout << “Derived Function” << endl;
     // call overridden function
     Base::print();
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}

Output

Derived Function

Base Function

In this program, the overridden function has been called inside the Derived class.

class Derived : public Base {
   public:
void print() {
     cout << “Derived Function” << endl;
     Base::print();
}
};

The code Base::print();, calls the overridden function within the Derived class.

Function Overloading vs. Function Overriding

You can achieve function overloading at a compile-time, usually done within the derived and base classes. It provides more than one function definition by altering the signature of each function, like the data type or the return type of parameters.

Function overriding, on the other hand, can be achieved at run-time. The base class in overriding is redefined in the derived class with the same parameters and the same return type. Let’s find out what other features make these two functions different. 

  Overriding Overloading
Inheritance Requires class inheritance Does not require class inheritance
Function Signature Differs in signature either in the type or the number of parameters. Function signatures stay the same
Function Scope Functions differ in scope Owns the same scope
Function Behavior Required when a derived class function performs differently or with added functionality than the base class function. Required when functions bearing the same name have different behaviors based on the given parameters.

Conclusion

Function overriding in C++ helps save memory space and maintain the readability and consistency of any code. It also helps in making code reusable easily. Needless to say, knowledge about function overriding is an important skill to possess if you see a future for yourself in programming. 

If you want to join a reliable and premium course to kickstart your career in this field, you can sign up for the upGrad’s Master of Science in Computer Science on upGrad. Some of the key highlights of this program are as follows:-

  • Learn MERN Stack, Microservices, JavaScript, etc.
  • Wholesome learning on Cloud Labs
  • Experiment with tools used on Netflix, LinkedIn, Facebook, etc.
  • Get skilled in 10+ programming tools and languages
  • 24/7 student support

Frequently Asked Questions (FAQs)

1. How function overriding happens?

The function C++ override happens via inheritance, i.e. when one class inherits another class. When the derived class and the base class possess member functions having the same return type, name, and arguments list, it is called function overriding.

2. What is the difference between function overloading and function overriding in C++?

Function Overriding happens when functions possess the same prototype in the base and derived classes. Function Overloading is performed when more than one function with a similar name exists in one class.

3. What are the features of function overloading in C++?

Function overloading in C++ is primarily used for improving code readability. Programmers use it so they don’t have to memorize numerous function names. Classes with multiple functions, different parameters, and the same name are called Overloaded.

RELATED PROGRAMS