Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C++ Program for Function Overloading Example

C++ Program for Function Overloading Example

✅ C++ Program Demonstrating Function Overloading

#include <iostream>
using namespace std;

class Math {
public:
    // Function to add 2 integers
    int add(int a, int b) {
        return a + b;
    }

    // Function to add 3 integers
    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Function to add 2 floats
    float add(float x, float y) {
        return x + y;
    }
};

int main() {
    Math obj;
    int choice;

    cout << "Choose an option:\n";
    cout << "1. Add 2 integers\n";
    cout << "2. Add 3 integers\n";
    cout << "3. Add 2 float numbers\n";
    cin >> choice;

    if (choice == 1) {
        int a, b;
        cout << "Enter 2 integers: ";
        cin >> a >> b;
        cout << "Result = " << obj.add(a, b) << endl;
    }
    else if (choice == 2) {
        int a, b, c;
        cout << "Enter 3 integers: ";
        cin >> a >> b >> c;
        cout << "Result = " << obj.add(a, b, c) << endl;
    }
    else if (choice == 3) {
        float x, y;
        cout << "Enter 2 float numbers: ";
        cin >> x >> y;
        cout << "Result = " << obj.add(x, y) << endl;
    }
    else {
        cout << "Invalid choice!\n";
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program demonstrates Function Overloading in C++. Function overloading allows us to define multiple functions with the same name but different parameter lists.

  • int add(int, int) → Adds 2 integers
  • int add(int, int, int) → Adds 3 integers
  • float add(float, float) → Adds 2 floats
The compiler automatically decides which version of add() to call based on the arguments passed.

🧾 Sample Output:

Choose an option:
1. Add 2 integers
2. Add 3 integers
3. Add 2 float numbers
1
Enter 2 integers: 5 7
Result = 12
  
Choose an option:
1. Add 2 integers
2. Add 3 integers
3. Add 2 float numbers
2
Enter 3 integers: 2 4 6
Result = 12
  
Choose an option:
1. Add 2 integers
2. Add 3 integers
3. Add 2 float numbers
3
Enter 2 float numbers: 3.5 2.5
Result = 6
  

πŸ”‘ Keywords:

C++ function overloading, add function in C++, C++ polymorphism, OOP in C++, C++ programs for beginners

πŸ“Œ Hashtags:

#CPlusPlus #FunctionOverloading #CppExamples #Polymorphism #OOP #ProgrammingForBeginners

πŸ” Search Description:

This C++ program demonstrates function overloading with add() function to add two integers, three integers, or two float numbers. Includes explanation and sample output.

Comments

Popular Posts

πŸŒ™