Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

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

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

✅ C++ Program to Subtract 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();
    cout<<"Result after subtraction:\n";
    c3=c1-c2;
    c3.display();
}
  

πŸ“˜ Explanation:

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

  • Constructors: Default and parameterized constructors are used.
  • operator-() subtracts the real and imaginary parts of two complex numbers.
  • display() prints the result in the standard complex number format.

🧾 Sample Output:

Enter first part:
Enter real part:
5
Enter imag part:
3
Enter second part:
Enter real part:
2
Enter imag part:
1
Result after subtraction:
3-2i
  

πŸ”‘ Keywords:

C++ program complex number subtraction, 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 subtract two complex numbers using operator overloading. Demonstrates constructors, operator overloading, and object-oriented programming concepts.

Comments

πŸŒ™