+1 vote
21 views

I have to solve Complex Numbers in C++ OOP But I have no idea how to solve them. The Problem statments are given bellow:

  • Print the sum, difference and product of two complex numbers by creating a class named 'Complex' with separate functions for each operation whose real and imaginary parts are entered by the user.

  • Print the average of three numbers entered by the user by creating a class named 'Average' having a function to calculate and print the average without creating any object of the Average class.

by (155 points)   | 21 views

2 Answers

+1 vote

Hello Naeem, here is the program to complex Add two complex numbers using C++

#include<iostream>
using namespace std;
class Complex{
public:
    int real;
    int imag;
     void setvalue()
    {
        cin>>real;
        cin>>imag;
    }


    void display()
    {
        cout<<real<<"+"<<imag<<"i"<<endl;
    }

    void sum(Complex c1, Complex c2)
    {
        real=c1.real+c2.real;
        imag=c1.imag+c2.imag;
    }
    };

main()
    {
        Complex c1,c2,c3;
        cout<<"Enter real and imaginary part of first complex number"<<endl;
        c1.setvalue();
        cout<<"Enter real and imaginary part of second complex number"<<endl;
        c2.setvalue();
        cout<<"Sum of two complex numbers is"<<endl;
        c3.sum(c1,c2);
        c3.display();

    }
by (3.1k points)  
+1 vote

The solution of Program No.1 is given bellow. Please support our website if you are happy by our study materials.

#include <iostream>

using namespace std;

class Complex{
    private:
        int Real;
        int Imaginery;

    public:
        Complex(){
            this->Real = 0;
            this->Imaginery = 0;
        }

        void setReal(int real){
            this->Real = real;
        }
        void setImaginery(int imagnery){
            this->Imaginery = imagnery;
        }

        int getReal(){
            return this->Real;
        }
        int getImaginery(){
            return this->Imaginery;
        }

        int Sum(Complex Obj1, Complex Obj2){

            //int sum_real, sum_imaginery;

            this->Real = Obj1.Real + Obj2.Real;
            this->Imaginery = Obj1.Imaginery + Obj2.Imaginery;

            cout<<"Sum of Complex number is "<<this->Real<<" + "<<this->Imaginery<<"i \n";          

        }

        int Difference(Complex Obj1, Complex Obj2){


            this->Real = Obj1.Real - Obj2.Real;
            this->Imaginery = Obj1.Imaginery - Obj2.Imaginery;

            cout<<"Difference of Complex number is "<<this->Real<<" + "<<this->Imaginery<<"i \n";           

        }

        int Product(Complex Obj1, Complex Obj2){


            this->Real = Obj1.Real * Obj2.Real;
            this->Imaginery = Obj1.Imaginery * Obj2.Imaginery;

            cout<<"Product of Complex number is "<<this->Real<<" + "<<this->Imaginery<<"i \n";          


        }

};

int main(){
    int real1,imag1,real2,imag2;

    cout<<" Please Enter Real Number of Complex Number 1 = ";
    cin>>real1;

    cout<<" Please Enter Imaginery Number of Complex Number 1 = ";
    cin>>imag1;

    Complex Number1;
    Number1.setReal(real1);
    Number1.setImaginery(imag1);

    cout<<" Please Enter Real Number of Complex Number 2 = ";
    cin>>real2;

    cout<<" Please Enter Imaginery Number of Complex Number 2 = ";
    cin>>imag2;

    Complex Number2;
    Number2.setReal(real2);
    Number2.setImaginery(imag2);

    Complex Number3;
    Number3.Sum(Number1,Number2);
    Number3.Difference(Number1, Number2);
    Number3.Product(Number1, Number2);




}

The solution of Program No.2:

#include <iostream>

using namespace std;

class Average{
    private:
        int Number1;
        int Number2;
        int Number3;
        int Average;

    public:

    void setNumber1(int num1){
        this->Number1 = num1;   
    }


    void setNumber2(int num2){
        this->Number2 = num2;   
    }


    void setNumber3(int num3){
        this->Number3 = num3;   
    }

    void Calculate(){
        this->Average = (this->Number1 + this->Number2 + this->Number3)/3;  
    }

    void Display(){
        cout<<" Average of Numbers is = "<<this->Average;
    }

};

class HelperClass: public Average{

};

int main(){

    int n1,n2,n3;
    cout<<" Enter Number 1 = ";
    cin>>n1;
    cout<<" Enter Number 2 = ";
    cin>>n2;
    cout<<" Enter Number 3 = ";
    cin>>n3;

    HelperClass h;

    h.setNumber1(n1);
    h.setNumber2(n2);
    h.setNumber3(n3);
    h.Calculate();
    h.Display();

}
by (3.1k points)  
Thanks Dear!

Related questions

+1 vote
1 answer
asked Jun 27 by fatyma Nauman (485 points) | 24 views
+2 votes
0 answers
asked Jun 12 by toheed (1.2k points) | 9 views
+1 vote
0 answers
asked Jun 5 by toheed (1.2k points) | 9 views
+2 votes
1 answer
asked Jun 5 by toheed (1.2k points) | 13 views
+2 votes
3 answers
+2 votes
2 answers
asked Jun 4 by toheed (1.2k points) | 73 views
+2 votes
1 answer
+3 votes
0 answers




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
113 questions
85 answers
38 comments
103 users