213k views
4 votes
Define a data type for representing complex numbers. (A complex number consists of 'real' and 'imaginary' parts. The real and imaginary parts can represent the x and y coordinates of a point on a plane.) How can a complex data type be created? (pick correct choice in Illustration 2 )

User Afflatus
by
7.9k points

1 Answer

3 votes

Answer:

A complex data type can be created by the use of class or structures.

#include <iostream>

using namespace std;

class complex{//creating a class complex to store a complex value.

public://defining all the values public.

int real;

int img;

complex(int real,int img)//parameterized constructor.

{

this->real=real;

this->img=img;

}

};

int main() {

complex n1(1,2);//n1 is a complex number with real part 1 and imaginary part 2.

cout<<n1.real<<" i"<<n1.img<<endl;

return 0;

}

Output

1 i2

Step-by-step explanation:

I have created a class complex to store complex number with two integer parts real and imaginary.You can also declare them float if you wan to store decimal values and after that i have created a parameterized constructor with real and imaginary values.So we can assign the values at the time of declaration of the object of class complex.

User Rohan Grover
by
8.5k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.