Skip to main content

Featured

Solve LeetCode 30 Substring with Concatenation of All Words in C++ using sliding window and frequency maps

Substring with Concatenation of All Words in C++ LeetCode 30 · sliding window · frequency maps · string_view Table of Contents Problem Statement Approach 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 Conclusion Problem Statement 30. Substring with Concatenation of All Words Given a string s and an array of strings words , where every word has the same length, find all starting indices of substrings that are a concatenation of every word exactly once, in any order. Approach This solution uses a sliding window and two frequency maps. Since every word has the same length, the string is checked in chunks of len characters. freq stores the required frequency of each word. temp stores the frequencies in the current window. count tracks the number of word-sized chunks in the window. If a word occurs too ...

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

Popular Posts

๐ŸŒ™