0% found this document useful (0 votes)
16 views3 pages

Unit Code: CS: Diploma in Technical Trainer Education (Computer Studies)

The document discusses the differences between overloading and overriding in object-oriented programming. Overloading allows methods with the same name but different parameters, while overriding allows subclasses to redefine inherited methods. An example program demonstrates overloading methods to make different sounds, and overriding a movement method in subclasses.

Uploaded by

krwsawe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Unit Code: CS: Diploma in Technical Trainer Education (Computer Studies)

The document discusses the differences between overloading and overriding in object-oriented programming. Overloading allows methods with the same name but different parameters, while overriding allows subclasses to redefine inherited methods. An example program demonstrates overloading methods to make different sounds, and overriding a movement method in subclasses.

Uploaded by

krwsawe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DIPLOMA IN TECHNICAL TRAINER EDUCATION (COMPUTER STUDIES)

DEPARTMENT: COMPUTER STUDIES

CLASS: CSCS (B) – APRIL 2023

UNIT CODE: CS

UNIT TITLE: Object Oriented Programming

Questions
i. What is the primary distinction between overloading and overriding?
ii. Write a program to illustrate the different between the two concepts

i. What is the primary distinction between overloading and overriding?


Overloading
Overloading in OOP is a term that implements a concept called polymorphism. Polymorphism is a Greek word that
means the ability to take more than one form i.e. literally means “many forms”. An operation may exhibit different
behaviours in different instances, and the behaviours depend on the types of data used in the operation. We have two
types of overloading in OOP namely operator overloading and function overloading.
In operator overloading, an operator is made to exhibit different behaviours in different instances. While function
overloading a single function name is used to perform different types of tasks.

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;
}

// Virtual method to be overridden by derived classes


virtual void move() {
cout << "Animal moves" << 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

You might also like