Complex class C++ Example

Define a complex class, define default constructor, parameterized constructors, copy constructor, destructor. Overload +, -, unary -, ++ (prefix, postfix), =, comma (,), ->, << and >> operators.

#include<iostream>
using namespace std;
#include<math.h>

//defining the complex class

class complex
{

//private members

float real;
float img;

//publc members

public:

//constructors are used to initialize the objects
//default constructor

complex()
{
real=0;
img=0;
}

//parameterized constructor

complex(float a,float b)
{
real=a;
img=b;
}

//copy constructor

complex(const complex &c)
{

real=c.real;
img=c.img;
}

//destructors is used to deallocate the data stored

~complex()
{
cout<<"destroyed";
}



void get_complex()
{

cout<<"\n enter the real value";
cin>>real;
cout<<"\n enter the imaginary value:";
cin>>img;
}

void print_complex()
{

cout<<"\n printing complex values";
cout<<"\n the value of real is: " ;
cout<<real;
cout<<"\n the value of imaginary is:";
cout<<img;
}

void dist_complex(complex &c1,complex &c2)
{
float distance;

distance=sqrt(pow((c1.real-c2.real),2)+pow((c1.img-c2.img),2));

cout<<"\nthe distance of two complex numbers is :";


cout<<distance;
}

};
int main()
{

complex c1,c2,c3;
c1.get_complex();
c2.get_complex();
c3.dist_complex(c1,c2);
return 0;

}

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto