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

function overloading and overriding

Uploaded by

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

function overloading and overriding

Uploaded by

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

Function Overloading

and Overriding
Introduction to Functions

 Definition: A function is a block of code that performs a specific task,


often taking inputs (called parameters) and giving an output.
 Purpose of Functions:
 Make code more organized and reusable
 Allow programmers to break down complex problems into simpler tasks
int add(int a, int b) {
return a + b;
}
Understanding Function
Overloading
 Definition: Function overloading allows you to have multiple
functions with the same name but different parameters in the same
scope.
 Purpose: Allows us to perform similar operations for different types
of data without creating multiple function names.
 Analogy: Think of it like using the word "run" in different situations
(e.g., "run a program," "run a marathon"). The action depends on the
context.
How Function Overloading Works

 Parameter Requirements: To overload a function, change one of the


following:
 Number of Parameters: e.g., add(int a) vs. add(int a, int b)
 Type of Parameters: e.g., add(int a, int b) vs. add(double a, double b)
 Return Type: Does not affect overloading; only the parameter list
matters.
Example- Different Parameters

int add(int a, int b) {


return a + b;
}

double add(double a, double b) {


return a + b;
}

int main() {
cout << add(3, 4); // Calls int version: Output 7
cout << add(3.5, 4.5); // Calls double version: Output 8.0
return 0;
}
Example

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

int main() {
cout << add(3, 4); // Calls two-parameter version: Output 7
cout << add(3, 4, 5); // Calls three-parameter version: Output 12
return 0;
}
Advantages of Function
Overloading
 Simplifies Code: Only one function name needed, making code
more readable.
 Flexibility: Easily use similar operations for different data types or
parameter counts.
 Practicality: Commonly used in libraries to handle various data
types with the same function name.
What is Function Overriding?

 Definition: Function overriding allows a derived class to provide a


specific implementation for a function that is already defined in its
base class.
 Purpose: Enables polymorphism, where the derived class modifies or
extends the behavior of a base class function.
How Function Overriding Works.

 Inheritance Required: The function must exist in a base class and


then be redefined in the derived class.
 Same Function Signature: The overridden function in the derived
class must have the same parameter types and return type as in the
base class.
 Virtual Keyword:
 Use virtual in the base class function to allow overriding.
 In the derived class, use override for clarity.
class Animal {
public:
virtual void makeSound() {
cout << "Some animal sound" << endl;
}
};
 Animal has a makeSound
class Dog : public Animal { function.
public:  Dog overrides makeSound
void makeSound() override { with its own version.
cout << "Woof!" << endl;
 When using an Animal
}
pointer, makeSound
};
dynamically calls the correct
Dog version.
int main() {
Animal *animal = new Dog();
animal->makeSound(); // Calls Dog's version:
Output "Woof!"
return 0;
}
Why Use Function Overriding?

 Polymorphism: Enables derived classes to customize behavior without


modifying the base class.
 Extensibility: Makes code more flexible and modular.
 Example: Imagine a graphics program where all shapes (Circle,
Rectangle, etc.) override a draw() method. The program can then call
draw() on any shape without knowing its specific type.
Difference between function
overloading and function Overriding
 Function Overloading in the Base Class (Shape):

 Shape has two versions of the area function:


 area(): No parameters, returns 0.0.
 area(double side): Takes one parameter and returns side * side, which can represent the
area of a square.
 Function Overriding in Derived Classes:

 Rectangle and Circle classes both override area() with specific implementations.
 Rectangle calculates area as length * width.
 Circle calculates area as π * radius^2.
 Main Function:

 Demonstrates overloading by calling baseShape.area(4.0).


 Demonstrates overriding by calling rectangle.area() and circle.area(), which use the
overridden versions in each derived class.

You might also like