C++ and Object-Oriented Programming
Function Overloading; Default Arguments; Inline Functions
C-CAT
Function Overloading
What is Function Overloading?
Functions having the same name but differing in:
- Number of arguments
- Type of arguments
- Order of arguments
This process is called function overloading.
// Multiple 'Add' functions
int Add(int a, int b, int c, int d) {
return a + b + c + d;
}
float Add(float a, float b, float c, float d) {
return a + b + c + d;
}
float Add(int a, float b, float c, float d) {
return a + b + c + d;
}
float Add(int a, int b, float c, float d) {
return a + b + c + d;
}
float Add(int a, float b, float c) { // different count
return a + b + c;
}
// Practical example
void print(int n) {
cout << "Integer: " << n << endl;
}
void print(double d) {
cout << "Double: " << d << endl;
}
void print(string s) {
cout << "String: " << s << endl;
}
void print(int n, string label) {
cout << label << ": " << n << endl;
}
int main() {
print(42); // Integer: 42
print(3.14); // Double: 3.14
print("Hello"); // String: Hello
print(42, "Answer"); // Answer: 42
return 0;
}
Operator Overloading vs Function Overloading
| Feature | Function Overloading | Operator Overloading |
|---|---|---|
| What | Same function name, different params | Redefine operator behavior |
| Example | add(int,int) and add(float,float) | Complex + Complex |
| Syntax | Function name | operator+ |
Default Arguments
What are Default Arguments?
Default values assigned to function parameters that are used when no argument is provided.
Rules: Default arguments should be given in right-to-left order.
// Default arguments: right-to-left (mandatory rule!)
void display(int x, int y = 10, int z = 20) { // y and z have defaults
cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
}
int main() {
display(5); // x=5, y=10, z=20 (both defaults used)
display(5, 30); // x=5, y=30, z=20 (z default used)
display(5, 30, 40); // x=5, y=30, z=40 (no defaults)
return 0;
}
WRONG — Default args must be rightmost:
// ERROR: cannot have default args before non-default args
void wrong(int x = 5, int y) { } // Compile error!
Real-World Default Arguments Example
void create_account(string name, double balance = 0.0,
string type = "Savings", bool active = true) {
cout << "Account: " << name
<< ", Balance: " << balance
<< ", Type: " << type << endl;
}
int main() {
create_account("Alice"); // balance=0, type=Savings
create_account("Bob", 5000.0); // type=Savings
create_account("Charlie", 10000.0, "Current"); // all provided
return 0;
}
Inline Functions
What is an Inline Function?
An inline function is a function where the compiler replaces the function call with the function body — avoiding function call overhead.
- Small, frequently called functions benefit most
- Compiler may ignore
inlinehint
When compiler should NOT replace:
- Functions containing switch, loops
Recursive functions
inline float triangle_area(float base, float height) {
return 0.5 * base * height;
}
// What the compiler does:
// Instead of: float area = triangle_area(10, 5);
// It
generates: float area = 0.5 * 10 * 5;
int main() {
float base, height;
cout << "Enter base: "; cin >> base;
cout << "Enter height: "; cin >> height;
cout << "Area: " << triangle_area(base, height) << endl;
return 0;
}
Inline vs Macros
| Feature | Inline Function | Macro |
|---|---|---|
| Type checking | Yes | No |
| Debugging | Debuggable | No |
| Scope | Class aware | No scope |
| Side effects | Safe | Can have issues |
| Complexity allowed | Yes | Limited |
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.