LAB MANUAL
Program
3(a): Class to hold complex number and perform complex arithmetic
//
Filename : complex.hpp
//
Purpose : To implement complex class
// Class
declaration & definition
#include <iostream>
using namespace std;
namespace secs {
class Complex {
protected:
double real;
double imag;
public:
//Default
constructor
Complex() {
real=0;
imag=0;
}
//Parameterized
constructor with default argument
Complex(double r, double i=0) {
real=r;
imag=i;
}
//Destructor
~Complex() {
}
//Function
to print complex number
void print(); //only function prototype is declared here
//Function
to return conjugate of complex number
Complex
conjugate();
//Inline
functions – defined inside class
void set(double r, double i) {
real=r;
imag=i;
}
void setReal(double r) {
real=r;
}
void setImag(double i) {
imag=i;
}
double getReal() {
return real;
}
double getImag() {
return imag;
}
//Overloading
operators as member operators
//Only prototype
is declared here, will be defined later
Complex
operator+(Complex &obj);
Complex
operator-(Complex &obj);
Complex
operator*(Complex &obj);
Complex
operator/(Complex &obj);
Complex
operator=(Complex obj);
//Declaring
operators as friend operators of the class
//these are
not member operators and defined somewhere outside
friend ostream
&operator<<(ostream &out,Complex obj);
friend istream
&operator>>(istream &in,Complex &obj);
}; //Class declaration & definition ends here
//Implementing
functions and operators outside class but inside namespace
//print() is a member of the Complex class and returns void
void Complex::print() {
cout<<real;
if(imag>=0)
cout<<"+";
cout<<imag<<"i";
}
//conjugate() is a member of the Complex class and returns an
object of the Complex type
Complex
Complex::conjugate() {
Complex
tmp;
tmp.real=real;
tmp.imag=-imag;
return tmp;
}
//operator+
and operator- are the members of the Complex class,
//These
returns an object of the Complex class and takes a reference
//to
object of Complex class as argument
Complex
Complex::operator+(Complex &obj)
{
Complex
tmp;
tmp.real=real+obj.real;
tmp.imag=imag+obj.imag;
return tmp;
}
Complex
Complex::operator-(Complex &obj)
{
Complex
tmp;
tmp.real=real-obj.real;
tmp.imag=imag-obj.imag;
return tmp;
}
Complex
Complex::operator*(Complex &obj) {
Complex
tmp;
tmp.real=real*obj.real - imag*obj.imag;
tmp.imag=real*obj.imag + imag*obj.real;
return tmp;
}
Complex
Complex::operator/(Complex &obj)
{
Complex
tmp;
double denom = obj.real
* obj.real + obj.imag * obj.imag;
tmp.real=(real*obj.real
+ imag*obj.imag)/denom;
tmp.imag=(imag*obj.real - real*obj.imag)/denom;
return tmp;
}
} //namespace secs closed here
//To
define members of secs namespace, we need to go inside it
namespace secs { //Opening namespace to define member operators
//Defining
member operators outside class & inside namespace
//operator=
is a member of the Complex class, which is a member of
//the
secs namespace, it returns an object of Complex class and
//takes
an object of the class Complex as argument
Complex
Complex::operator=(Complex obj) {
Complex
tmp;
tmp.real=real=obj.real;
tmp.imag=imag=obj.imag;
return tmp;
}
//Defining
friend operators of the class which are not members of the class
//operator<<
returns a reference to an object of class ostream of
//the std namespace and takes two arguments, first, a reference to
//an
object of class ostream of namespace std and second, an object of
//class
Complex of namespace secs
ostream &operator<<(ostream &out,Complex obj) {
out<<obj.real;
if(obj.imag>=0)
out<<"+";
out<<obj.imag<<"i";
return out;
}
//operator>>
returns a reference to an object of class istream of
//the std namespace and takes two arguments, first, a reference to
//an
object of class istream of namespace std and second, a reference to
//an
object of class Complex of namespace secs
istream &operator>>(istream &in,Complex &obj) {
char ch;
in>>obj.real>>obj.imag>>ch;
return in;
}
} //Closing namespace again
Program 3(b): Program to perform
complex arithmetic using Complex class
// Filename : complex1.cpp
// Purpose : To use Complex
class to perform complex arithmetic
// main()
function definition to use Complex class
//including the file created above,
complex.hpp having class definition
//filename “complex.hpp” is
enclosed inside double quotes as the file exist in current directory
//and not in the standard header
file directory
#include "complex.hpp"
using namespace secs;
int main() {
Complex a,b(3,4);
a.set(6,-2);
cout<<"a =
"<<a<<endl;
cout<<"b =
"<<b<<endl;
Complex c;
c=a+b;
cout<<"c =
"<<c<<endl;
system("pause");
return 0;
}
Program
3(c): Program to perform complex arithmetic using Complex class
//
Filename : complex2.cpp
//
Purpose : To use Complex class to get complex conjugate, and perform
arithmetic
// main() function definition to use Complex class
//including
the file created above, complex.hpp having class definition
#include "complex.hpp"
using namespace secs;
int main() {
Complex a(3,-4),b(8,-2);
cout<<" a =
"<<a<<endl
<<"
b = "<<b<<endl
<<"
Conjugate of a = "<<a.conjugate()<<endl
<<"
a + b = "<<a+b<<endl
<<"
a - b = "<<a-b<<endl
<<"
a * b = "<<a*b<<endl
<<"
a / b = "<<a/b<<endl;
system("pause");
return 0;
}
Exercise: Revise operator overloading, function
overloading, friend function and inline functions from your text book and also
try to implement other complex arithmetic by overloading * and / operator to
perform complex multiplication and complex division
Home of Learning Center Index of Lab Manual
Previous Program
Next Program