Overloading and Overriding Presentation
Overloading and Overriding Presentation
Speaker Notes:
Image:
Speaker Notes:
Image:
Additional Tips:
Remember, this is a starting point. You can customize and add more details to
these slides based on your specific needs and the level of complexity required
by your teacher.
1. Function Overloading:
C++
#include <iostream>
using namespace std;
int main() {
int circle_radius = 5;
int rectangle_length = 10;
int rectangle_breadth = 4;
return 0;
}
Use code with caution.
content_copy
Explanation:
2. Method Overriding:
C++
#include <iostream>
class Animal {
public:
virtual void makeSound() {
cout << "Generic animal sound" << endl;
}
};
int main() {
Animal animal;
Dog dog;
return 0;
}
Use code with caution.
content_copy
Explanation:
We have a base class Animal with a virtual method makeSound that provides a
generic animal sound.
The Dog class inherits from Animal.
In the Dog class, we override the makeSound method to provide a specific sound
for a dog ("Woof!").
The override keyword is optional but helps clarify that we are intentionally
overriding a parent class method.
In the main function, we create objects of both Animal and Dog class.
When we call makeSound on the animal object, it calls the parent class method.
When we call makeSound on the dog object, it calls the overridden method in
the Dog class, resulting in "Woof!" being printed.
C++
#include <iostream>
int main() {
int num1 = 5, num2 = 3;
double decimal1 = 2.5, decimal2 = 1.7;
cout << num1 << " + " << num2 << " = " << add(num1, num2) << endl;
cout << decimal1 << " + " << decimal2 << " = " << add(decimal1, decimal2)
<< endl;
return 0;
}
Use code with caution.
content_copy
Explanation:
We have two add functions, one for integers and one for doubles.
Based on the arguments provided (integers or doubles), the correct add function is
called.
C++
#include <iostream>
class Person {
public:
void greet() {
cout << "Hello!" << endl;
}
};
int main() {
Person person;
Teacher teacher;
return 0;
}
Use code with caution.
content_copy
Explanation:
These examples keep the functionalities very basic, focusing solely on understanding
how overloading and overriding work with different argument types and method
implementations. You can expand on these by adding more functions or methods with
slightly more complex calculations or greetings based on your needs.
Imagine all animals can make a sound, but each has a unique call.
In C++, method overriding is like animals inheriting the ability to make a sound but then
personalizing it.
A base class Animal might have a makeSound() method that prints "Generic animal
sound."
A subclass Dog inherits from Animal and overrides the makeSound() method to print
"Woof!"
When you tell an Animal or a Dog object to make a sound, it uses the appropriate
method based on its type, just like different animals have different calls.
Key Points:
Function overloading allows functions with the same name to handle different data
types.
Method overriding allows subclasses to customize inherited methods for specific
functionalities.
Both concepts make code more flexible and reusable.
Remember: These are simplified analogies to grasp the core ideas. As you learn more
about C++ programming, you'll encounter the technical details behind these concepts.
Function Overloading
With function overloading, multiple functions can have the same name with
different parameters:
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
Consider the following example, which have two functions that add numbers of
different type:
Example
int plusFuncInt(int x, int y) {
return x + y;
}
double plusFuncDouble(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Try it Yourself »
Instead of defining two functions that should do the same thing, it is better to
overload one.
Example
int plusFunc(int x, int y) {
return x + y;
}
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Try it Yourself »
Note: Multiple functions can have the same name as long as the number and/or
type of parameters are different.