Unit Code: CS: Diploma in Technical Trainer Education (Computer Studies)
Unit Code: CS: Diploma in Technical Trainer Education (Computer Studies)
UNIT CODE: CS
Questions
i. What is the primary distinction between overloading and overriding?
ii. Write a program to illustrate the different between the two concepts
Overriding
In OOP, overriding is a mechanism that allows a subclass (or child class) to redefine the behaviour of a method
inherited from its superclass (or parent class).
The difference
Feature Overloading Overriding
Definition Multiple methods with the same name Redefining a method in a subclass
Concept Redefining operator or method behavior based Redefining inherited method behavior in
on arguments subclass
Method Same method name, but different parameter Same method name, parameters, and return
signature list (number, order, or type) type as superclass method
Relationship Occurs within the same class Involves inheritance, where a subclass
redefines a method from its superclass
Parameters Parameters may differ in number or types Parameters must be exactly the same
Return type May or may not have the same return type Must have the same return type
Inheritance Yes, subclass inherits from superclass
involved NO
Usage Provides different implementations for Used to provide specialized behavior for
different use cases subclasses
Resolution Resolved at runtime based on the object's
Resolved at compile time (static binding) actual type (dynamic binding)
iii. Write a program to illustrate the different between the two concepts
#include <iostream>
using namespace std;
// Base class
class Animal
{
public:
// Overloaded method with the same name but different parameters
void makeSound() {
cout << "Animal makes a generic sound" << endl;
}
// Derived class 1
class Dog : public Animal
{
public:
// Overridden method with the same name and signature as the base
class method
void makeSound() {
cout << "Dog barks" << endl;
}
// Overridden method
void move() override {
cout << "Dog walks" << endl;
Page 2 of 3
}
};
// Derived class 2
class Cat : public Animal
{
public:
// Overridden method with the same name and signature as the base
//class method
void makeSound()
{
cout << "Cat meows" << endl;
}
// Overridden method
void move() override
{
cout << "Cat jumps" << endl;
}
};
int main()
{
// Create objects of both derived classes
Dog dog;
Cat cat;
// Demonstrate overloading
dog.makeSound(); // Calls Dog's makeSound() method
cat.makeSound(); // Calls Cat's makeSound() method
// Demonstrate overriding
dog.move(); // Calls Dog's overridden move() method
cat.move(); // Calls Cat's overridden move() method
return 0;
}
In this program:
The Animal class serves as the base class with a method makeSound() that is overloaded and a virtual
method move() to be overridden.
The Dog class and the Cat class are derived classes from Animal.
Both Dog and Cat classes override the move() method from the Animal class.
The main() function demonstrates overloading and overriding by creating objects of Dog and Cat classes and
calling respective methods.
When you run this program, you'll see the distinction between overloading and overriding in action. The
makeSound() method demonstrates overloading, as each subclass has its own implementation of the method with
the same name. The move() method demonstrates overriding, as each subclass redefines the method inherited from
the base class.
Page 3 of 3