+1 vote
60 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.

| 60 views

+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.3k 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.3k points)
Thanks Dear!

+1 vote
+1 vote
+1 vote