Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

C++ Function Overloading Example (Different Parameters)

C++ Function Overloading Example (Different Parameters)

✅ C++ Program: Function Overloading with Different Parameters

#include <iostream>
using namespace std;

class Math {
public:
    void add(int a, int b) {
        cout << "Sum of two ints: " << a + b << "\n";
    }

    void add(double a, double b) {
        cout << "Sum of two doubles: " << a + b << "\n";
    }

    void add(int a, int b, int c) {
        cout << "Sum of three ints: " << a + b + c << "\n";
    }
};

int main() {
    Math m;
    m.add(10, 20);        // calls add(int, int)
    m.add(5.5, 2.5);      // calls add(double, double)
    m.add(1, 2, 3);       // calls add(int, int, int)
}
  

๐Ÿ“˜ Explanation:

This program shows Function Overloading in C++. The same function name add() is used with:

  • add(int, int) → Adds two integers
  • add(double, double) → Adds two doubles
  • add(int, int, int) → Adds three integers
The compiler chooses the correct function based on argument type and count.

๐Ÿงพ Sample Output:

Sum of two ints: 30
Sum of two doubles: 8
Sum of three ints: 6
  

๐Ÿ”‘ Keywords:

C++ function overloading, add function in C++, compile-time polymorphism, OOP concepts, C++ examples

๐Ÿ“Œ Hashtags:

#CPlusPlus #FunctionOverloading #CppExamples #Polymorphism #OOP #Programming

๐Ÿ” Search Description:

This C++ program demonstrates function overloading with different parameter lists (integers and doubles). Example of compile-time polymorphism with sample output.

Comments

Popular Posts

๐ŸŒ™