Functions are essential building blocks in C++ that help organize programs into reusable and maintainable modules. C++ extends the capabilities of functions beyond traditional procedural programming by introducing advanced features such as function overloading, default arguments, inline functions, static members, friend functions, and virtual functions.
Learning Objectives
Understand function overloading
Use default arguments
Create inline functions
Work with static class members
Use friend functions
Understand virtual functions
Apply these concepts in object-oriented programs
Function Overloading
Function overloading allows multiple functions to have the same name, provided they differ in the number or type of parameters.
Example:
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
};
int main()
{
Calculator calc;
cout << calc.add(10,20) << endl;
cout << calc.add(10.5,20.5);
return 0;
}
Output
30
31
Default Arguments
Default arguments allow a function parameter to have a predefined value.
Example:
#include <iostream>
using namespace std;
void greet(string name = "Student")
{
cout << "Welcome " << name;
}
int main()
{
greet();
return 0;
}
Output
Welcome Student
Inline Functions
An inline function requests the compiler to replace the function call with the function body, reducing function call overhead for small functions.
Example:
#include <iostream>
using namespace std;
inline int square(int x)
{
return x * x;
}
int main()
{
cout << square(5);
return 0;
}
Output : 25
Static Class Members
A static data member belongs to the class rather than individual objects.
Example:
#include <iostream>
using namespace std;
class Counter
{
public:
static int count;
};
int Counter::count = 0;
int main()
{
Counter::count++;
cout << Counter::count;
return 0;
}
Output
1
Friend Functions
A friend function can access the private members of a class.
Example:
#include <iostream>
using namespace std;
class Student
{
private:
int marks = 90;
friend void display(Student);
};
void display(Student s)
{
cout << s.marks;
}
int main()
{
Student s;
display(s);
return 0;
}
Output
90
Virtual Functions
A virtual function enables runtime polymorphism by allowing derived classes to override a base class function.
Example:
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void sound()
{
cout << "Animal Sound";
}
};
class Dog : public Animal
{
public:
void sound()
{
cout << "Dog Barks";
}
};
int main()
{
Animal *a;
Dog d;
a = &d;
a->sound();
return 0;
}
Output
Dog Barks
Advantages
Improves code reusability
Supports polymorphism
Reduces duplicate code
Makes programs more flexible
Improves maintainability
Common Mistakes
Overloading functions with identical parameter lists.
Forgetting the virtual keyword in the base class.
Misusing friend functions.
Using inline functions for large code blocks.
Confusing static members with object members.
Still need help?
Contact us