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++ Program to Add Two Complex Numbers using Operator Overloading

C++ Program to Add Two Complex Numbers using Operator Overloading

✅ C++ Program to Add Two Complex Numbers using Operator Overloading

#include<iostream>
using namespace std;
class complex
{
    public:
    int real,imag;
    void input()
    {
        cout<<"Enter real part:\n";
        cin>>real;
        cout<<"Enter imag part:\n";
        cin>>imag;
    }
    complex()
    {
        real=0;
        imag=0;
    }
    complex(int r,int i )
    {
        real=r;
        imag=i;
    }
    complex operator+(complex&obj)
    {
        complex result;
        result.real=real+obj.real;
        result.imag=imag+obj.imag;
        return result;
    }
    void display()
    {
        cout<<real<<"+"<<imag<<"i"<<"\n";
    }
};
int main( )
{
    complex c1,c2,c3;
    cout<<"Enter first part:\n";
    c1.input();
    cout<<"Enter second part:\n";
    c2.input();
    c3=c1+c2;
    cout<<"Result after addtion:\n";
    c3.display();
}
  

๐Ÿ“˜ Explanation:

This program demonstrates operator overloading in C++. The + operator is overloaded to add two complex numbers.

  • Constructors: Default and parameterized constructors are used.
  • operator+() adds the real and imaginary parts of two complex numbers.
  • display() prints the result in proper format.

๐Ÿงพ Sample Output:

Enter first part:
Enter real part:
2
Enter imag part:
3
Enter second part:
Enter real part:
4
Enter imag part:
5
Result after addtion:
6+8i
  

๐Ÿ”‘ Keywords:

C++ program complex number addition, operator overloading in C++, OOP concepts in C++, C++ interview program, complex class in C++

๐Ÿ“Œ Hashtags:

#CPlusPlus #OperatorOverloading #ComplexNumbers #OOP #CodingInterview

๐Ÿ” Search Description:

C++ program to add two complex numbers using operator overloading. Demonstrates constructors, operator overloading, and object-oriented programming concepts.

Comments

Popular Posts

๐ŸŒ™