Overview
Function overloading and function overriding are two fundamental concepts in C++ that allow programmers to use the same function name in different contexts, enhancing polymorphism. Understanding these concepts is crucial for writing flexible and maintainable code in C++.
Key Concepts
- Function Overloading - Defining multiple functions with the same name but different parameters or signature.
- Function Overriding - Redefining a base class function in a derived class with the same signature.
- Polymorphism - The ability of a function or an object to take on many forms, facilitated by overloading and overriding.
Common Interview Questions
Basic Level
- What is function overloading, and why is it used?
- Provide an example of function overloading in C++.
Intermediate Level
- Explain function overriding with an example in C++.
Advanced Level
- How does virtual function play a role in function overriding, and why is it important?
Detailed Answers
1. What is function overloading, and why is it used?
Answer: Function overloading allows multiple functions to have the same name but different parameters (type, number, or order of parameters). It enables functions to perform similar tasks with different inputs, enhancing code readability and usability.
Key Points:
- Increases the readability of the code.
- Allows the same function name to be reused for different types or numbers of parameters.
- Provides compile-time polymorphism.
Example:
#include <iostream>
using namespace std;
class Print {
public:
void display(int i) {
cout << "Displaying int: " << i << endl;
}
void display(double d) {
cout << "Displaying double: " << d << endl;
}
void display(string s) {
cout << "Displaying string: " << s << endl;
}
};
int main() {
Print obj;
obj.display(5);
obj.display(3.14);
obj.display("Hello, World!");
return 0;
}
2. Provide an example of function overloading in C++.
Answer: Function overloading is demonstrated by declaring multiple functions with the same name but different parameters.
Key Points:
- Function overloading can be based on the number of parameters, types of parameters, or both.
- The compiler uses the number and type of arguments to distinguish between the overloaded functions.
- Overloading is resolved at compile time.
Example:
#include <iostream>
using namespace std;
// Function with one parameter
void printNumber(int i) {
cout << "The integer is " << i << endl;
}
// Overloaded function with two parameters
void printNumber(int i, double d) {
cout << "The integer is " << i << ", and the double is " << d << endl;
}
int main() {
printNumber(5);
printNumber(10, 3.14);
return 0;
}
3. Explain function overriding with an example in C++.
Answer: Function overriding occurs when a derived class has a function with the same name, return type, and parameters as a function in its base class. It allows the derived class to provide a specific implementation of the function.
Key Points:
- Used to achieve runtime polymorphism.
- The function in the base class must be declared as virtual
to be overridden.
- Allows a derived class to provide a specific implementation of a function that is already provided by its base class.
Example:
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Print base class" << endl;
}
};
class Derived : public Base {
public:
void print() override {
cout << "Print derived class" << endl;
}
};
int main() {
Base* bptr;
Derived d;
bptr = &d;
// Virtual function, binded at runtime
bptr->print();
return 0;
}
4. How does virtual function play a role in function overriding, and why is it important?
Answer: Virtual functions are crucial for function overriding as they enable runtime polymorphism. When a function is declared as virtual
in a base class, it can be overridden in any derived class. This allows a program to call overridden functions through pointers/references of base class type at runtime.
Key Points:
- Enables runtime polymorphism.
- Virtual functions allow the program to decide which function to execute at runtime.
- Important for achieving dynamic binding of functions.
Example:
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function called" << endl;
}
};
class Derived : public Base {
public:
void show() override {
cout << "Derived class show function called" << endl;
}
};
int main() {
Base *bptr;
Derived d;
bptr = &d;
// Function call is resolved at runtime
bptr->show();
return 0;
}
This demonstrates how virtual functions facilitate function overriding to achieve runtime polymorphism, allowing the correct function version to be called based on the object type, not the pointer/reference type.