Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™