-->

Overriding and Overloading in C++

 In C++, "overriding" and "overloading" are two different concepts that allow you to extend or modify the behavior of classes and functions.

Overriding refers to the ability to redefine a virtual function in a derived class with the same name and parameters as in the base class. This allows you to change the behavior of a function inherited from the base class in the derived class. To achieve this, the virtual function must be marked as such in the base class, and the overriding function in the derived class must use the "override" keyword.

Here is an example of overriding in C++:


#include <iostream> class Shape { public: virtual void draw() { std::cout << "Drawing a shape" 
<< std::endl; } }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle" 
<< std::endl; } }; int main() { Shape *shape = new Circle(); shape->draw(); delete shape; return 0; }

In this example, the base class "Shape" has a virtual function "draw()". The derived class "Circle" overrides the function "draw()" to provide its own implementation. The main function creates a pointer to a "Shape" object and assigns it to an object of the "Circle" class. When the "draw()" function is called through the pointer, the version from the "Circle" class is executed, and "Drawing a circle" is printed to the console.

Overloading, on the other hand, refers to the ability to have multiple functions with the same name but different parameters. This allows you to create multiple versions of a function that can operate on different data types or take different number of parameters. When a function is called, the compiler selects the appropriate version based on the parameters passed to the function.

Here is an example of overloading in C++:


#include <iostream> void print(int num) { std::cout << "Printing an integer: " 
 << num << std::endl; } void print(double num) { std::cout << "Printing a double: " 
 << num << std::endl; } void print(const char *str) { std::cout << "Printing a string: " 
 << str << std::endl; } int main() { print(5); print(5.5); print("Hello"); return 0; }

In this example, the function "print" is overloaded three times with different parameters. When the function is called in the main function, the compiler selects the appropriate version based on the type of the argument passed to the function, and the appropriate message is printed to the console.

In conclusion, both overriding and overloading are important concepts in C++ that allow you to extend and modify the behavior of classes and functions. Overriding is used to change the behavior of a virtual function inherited from a base class, while overloading is used to create multiple versions of a function with the same name but different parameters.

It is important to note that while overloading can be used to create functions with similar behavior, it can also lead to confusion and make the code less readable. Therefore, it is recommended to use overloading judiciously and choose descriptive names for the overloaded functions to make their purpose clear.

Another point to keep in mind is that overloading has some restrictions in C++. For example, you cannot overload functions based only on their return type. Also, when overloading a function, the parameters must be different in either their number, type or both.

Additionally, when using inheritance, it is important to understand the difference between "overriding" and "hiding". "Hiding" occurs when a derived class defines a function with the same name as a non-virtual function in the base class. Unlike overriding, hiding does not change the behavior of the base class function, and the base class function cannot be called through a pointer to the derived class.

In summary, understanding the concepts of overriding and overloading is essential for writing robust and maintainable C++ code. These concepts allow you to extend and modify the behavior of classes and functions, but it is important to use them carefully and follow best practices to ensure code quality and readability.

 

 

You may like these posts

Latest Posts

About Sure Mag

Join with us

Overriding and Overloading in C++

 In C++, "overriding" and "overloading" are two different concepts that allow you to extend or modify the behavior of classes and functions.

Overriding refers to the ability to redefine a virtual function in a derived class with the same name and parameters as in the base class. This allows you to change the behavior of a function inherited from the base class in the derived class. To achieve this, the virtual function must be marked as such in the base class, and the overriding function in the derived class must use the "override" keyword.

Here is an example of overriding in C++:


#include <iostream> class Shape { public: virtual void draw() { std::cout << "Drawing a shape" 
<< std::endl; } }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle" 
<< std::endl; } }; int main() { Shape *shape = new Circle(); shape->draw(); delete shape; return 0; }

In this example, the base class "Shape" has a virtual function "draw()". The derived class "Circle" overrides the function "draw()" to provide its own implementation. The main function creates a pointer to a "Shape" object and assigns it to an object of the "Circle" class. When the "draw()" function is called through the pointer, the version from the "Circle" class is executed, and "Drawing a circle" is printed to the console.

Overloading, on the other hand, refers to the ability to have multiple functions with the same name but different parameters. This allows you to create multiple versions of a function that can operate on different data types or take different number of parameters. When a function is called, the compiler selects the appropriate version based on the parameters passed to the function.

Here is an example of overloading in C++:


#include <iostream> void print(int num) { std::cout << "Printing an integer: " 
 << num << std::endl; } void print(double num) { std::cout << "Printing a double: " 
 << num << std::endl; } void print(const char *str) { std::cout << "Printing a string: " 
 << str << std::endl; } int main() { print(5); print(5.5); print("Hello"); return 0; }

In this example, the function "print" is overloaded three times with different parameters. When the function is called in the main function, the compiler selects the appropriate version based on the type of the argument passed to the function, and the appropriate message is printed to the console.

In conclusion, both overriding and overloading are important concepts in C++ that allow you to extend and modify the behavior of classes and functions. Overriding is used to change the behavior of a virtual function inherited from a base class, while overloading is used to create multiple versions of a function with the same name but different parameters.

It is important to note that while overloading can be used to create functions with similar behavior, it can also lead to confusion and make the code less readable. Therefore, it is recommended to use overloading judiciously and choose descriptive names for the overloaded functions to make their purpose clear.

Another point to keep in mind is that overloading has some restrictions in C++. For example, you cannot overload functions based only on their return type. Also, when overloading a function, the parameters must be different in either their number, type or both.

Additionally, when using inheritance, it is important to understand the difference between "overriding" and "hiding". "Hiding" occurs when a derived class defines a function with the same name as a non-virtual function in the base class. Unlike overriding, hiding does not change the behavior of the base class function, and the base class function cannot be called through a pointer to the derived class.

In summary, understanding the concepts of overriding and overloading is essential for writing robust and maintainable C++ code. These concepts allow you to extend and modify the behavior of classes and functions, but it is important to use them carefully and follow best practices to ensure code quality and readability.

 

 


SHARE THIS

Author:

Etiam at libero iaculis, mollis justo non, blandit augue. Vestibulum sit amet sodales est, a lacinia ex. Suspendisse vel enim sagittis, volutpat sem eget, condimentum sem.

Most Popular