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

Function Overloading vs Function Overriding in C

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function Overloading vs Function Overriding in C

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function Overloading vs Function Overriding in C++

Last Updated : 08 Feb, 2023

Function Overloading (achieved at compile time)

Function Overloading provides multiple definitions of the function by changing signature i.e.
changing number of parameters, change datatype of parameters, return type doesn’t play
any role.

 It can be done in base as well as derived class


.Example:

void area(int a);

void area(int a, int b);

 CPP

// CPP program to illustrate

// Function Overloading

#include <iostream>

using namespace std;

// overloaded functions

void test(int);

void test(float);

void test(int, float);

int main()

int a = 5;

float b = 5.5;
// Overloaded functions

// with different type and

// number of parameters

test(a);

test(b);

test(a, b);

return 0;

// Method 1

void test(int var)

cout << "Integer number: " << var << endl;

// Method 2

void test(float var)

cout << "Float number: "<< var << endl;

// Method 3

void test(int var1, float var2)

cout << "Integer number: " << var1;


cout << " and float number:" << var2;

Output

Integer number: 5

Float number: 5.5

Integer number: 5 and float number:5.5

Function Overriding (achieved at run time)

It is the redefinition of base class function in its derived class with same signature i.e. return
type and parameters.

 It can only be done in derived class.

 Example:

Class a

public:

virtual void display(){ cout << "hello"; }

};

Class b:public a

public:

void display(){ cout << "bye";}

};

 CPP

// CPP program to illustrate

// Function Overriding
#include<iostream>

using namespace std;

class BaseClass

public:

virtual void Display()

cout << "\nThis is Display() method"

" of BaseClass";

void Show()

cout << "\nThis is Show() method "

"of BaseClass";

};

class DerivedClass : public BaseClass

public:

// Overriding method - new working of

// base class display method

void Display()

cout << "\nThis is Display() method"

" of DerivedClass";
}

};

// Driver code

int main()

DerivedClass dr;

BaseClass &bs = dr;

bs.Display();

dr.Show();

Output

This is Display() method of DerivedClass

This is Show() method of BaseClass

Function Overloading Function Overriding

Function Overloading provides multiple Function Overriding is the redefinition of


definitions of the function by changing base class function in its derived class
signature. with same signature.

An example of compile time polymorphism. An example of run time polymorphism.

Function signatures should be different. Function signatures should be the same.

Overridden functions are in different


Overloaded functions are in same scope.
scopes.

Overloading is used when the same function Overriding is needed when derived class
has to behave differently depending upon function has to do some different job
parameters passed to them. than the base class function.

A function has the ability to load multiple A function can be overridden only a single
times. time.

In function overloading, we don’t need In function overriding, we need an


inheritance. inheritance concept.

Classes in C++ are a key feature of the language, used for creating objects with specific
attributes and behaviors. They encapsulate data and functions into a single entity, promoting
an object-oriented approach to programming. Here's a simple explanation and example for
better understanding:

### Basic Structure of a C++ Class

1. **Class Declaration:** This is where you define a class and its members.

2. **Class Members:** These include variables (called member variables or attributes) and
functions (called member functions or methods).

Here's an example of a basic C++ class:

```cpp

#include <iostream>

using namespace std;

// Class Declaration

class Car {

public: // Access specifier

// Member Variables

string brand;
string model;

int year;

// Member Function

void displayInfo() {

cout << "Brand: " << brand << endl;

cout << "Model: " << model << endl;

cout << "Year: " << year << endl;

};

int main() {

// Creating an object of Car

Car myCar;

// Assigning values to attributes

myCar.brand = "Toyota";

myCar.model = "Corolla";

myCar.year = 2021;

// Calling member function

myCar.displayInfo();

return 0;

```

### Key Concepts:


- **Class:** A blueprint for creating objects (a particular data structure).

- **Object:** An instance of a class.

- **Access Specifiers:** Define the access level/visibility of class members. The common
ones are:

- `public`: Members are accessible from outside the class.

- `private`: Members cannot be accessed or viewed from outside the class.

- `protected`: Members cannot be accessed from outside the class, but they can be
accessed in inherited classes.

By using classes, you can model real-world entities, make your code more modular, and
easier to manage and understand. Would you like to know more about advanced concepts
like inheritance or polymorphism in C++?

You might also like