Wednesday, 12 October 2011

Complex Numbers with Operator Overloading and Type Conversion


Complex Numbers with Operator Overloading and Type Conversion

Aim:
To write a C++ program to implement complex number class with necessary operator overloading and type conversion

Algorithm:
Step 1: Start the program.
Step 2: Create a class Complex.
Step 3: Define the default and two parameterized constructors. One constructor is having two integer arguments and another one will have the two double data type arguments
Step 4: Declare the operator function which are going to be overloaded.
Step 5: Define the overloaded functions such as +,-,*,/,>>,<<.
Step 6: Create the objects and pass the complex values.
Step 7: Invoke the overloaded functions.
Step 8: Display the converted result.
Step 9: Stop the process.

Program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0.0;
}
complex(int r,int i) //conversion constructor
{
real = r;
imag = i;
}
complex(double r, double i)//conversion constructor
{
real = r;
imag = i;
}
friend istream& operator>>(istream &, complex &);
friend ostream& operator<<(ostream &, complex &);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
friend double condou(complex t); //complex–>double
};
double condou(complex t)
{
return t.real+t.imag;
}
istream& operator >>(istream &in, complex &c)
{
cout<<”\nReal Part:”;
in>>c.real;
cout<<”Imag Part:”;
in>>c.imag;
return in;
}
ostream& operator<<(ostream &out, complex &c)
{
if (c.imag<0)
out<<c.real<<c.imag<<”i”;
else
out<<c.real<<”+”<<c.imag<<”i”;
return out;
}
complex complex::operator+(complex c)
{
complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.real = real-c.real;
temp.imag = imag-c.imag;
return temp;
}
complex complex::operator*(complex c)
{
complex temp;
float t=c.real;
temp.real = real*c.real-imag*c.imag;
temp.imag = real*c.imag+imag*t;
return temp;
}
complex complex::operator/(complex c)
{
complex temp;
float qt;
float res=(imag*c.real-real*c.imag);
qt = c.real*c.real+c.imag*c.imag;
temp.real = (real*c.real+imag*c.imag)/qt;
temp.imag = res/qt;
return temp;
}
void main()
{
complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444);
double t;
clrscr();
t=condou(c5);
cout<<”\nEnter complex number 1: “;
cin>>c1;
cout<<”\nEnter complex number 2: “;
cin>>c2;
cout<<”\nEnter complex numbers are:”;
cout<<”\nComplex 1: “<<c1;
cout<<”\nComplex 2: “<<c2;
c3=c1+c2;
cout<<”\nResult of addition is:”<<c3;
c3=c1-c2;
cout<<”\nResult of subtraction is:”<<c3;
c3=c1*c2;
cout<<”\nResult of multiplication is:”<<c3;
c3=c1/c2;
cout<<”\nResult of division is:”<<c3;
cout<<”\nInteger–>complex:”<<c4;
cout<<”\nDouble–>complex:”<<c5;
cout<<”\nConverted to double”<<t;
getch();
}

OUTPUT:
 Enter complex number 1:
Real Part:2
Imag Part:4
Enter complex number 2:
Real Part:3
Imag Part:5
Enter complex numbers are:
Complex 1: 2+4i
Complex 2: 3+5i
Result of addition is:5+9i
Result of subtraction is:-1-1i
Result of multiplication is:-14+22i
Result of division is:0.764706+0.058824i
Integer->complex:4+9i
Double->complex:3.23004+4.666305i
Converted to double7.896345

Result:
Thus the program for operator overloading for complex numbers and their type conversions was executed.

MPLEMENTATION OF COMPLEX NUMBER OPERATIONS USING OPERATOR OVERLOADINGoo


IMPLEMENTATION OF COMPLEX NUMBER OPERATIONS USING OPERATOR OVERLOADING

AIM :
            To write a C++ program to implement the complex number operations using operator overloading.


ALGORITHM:

Step 1:      Start the process
Step 2:      Create a class complex with real and imaginary data members.
Step 3:      A parameterized constructor is used here for initializing real and
                  imaginary data members during object creation.
Step 4:      The operators such as +, -,* and / are overloaded.
Step 5:      In the main function the complex number operations are done with the
                  help of overloaded operators.
Step 6:      The result is displayed.
Step 7:      Stop the process.
PROGRAM:

#include <math.h>
#include <iostream.h>
#include <iomanip.h>
#include<conio.h>
class complex
{
     private:
                float real;               // Real Part
                float imag;      //  Imaginary Part
   public:
              complex(float,float);
              complex operator +(complex);
              complex operator -(complex);
              complex operator *(complex);
              complex operator /(complex);
              void printdata();

};
//                                        CONSTRUCTOR
                complex::complex(float r=0.0f,float im=0.0f)
                {
                   real=r;
                   imag=im;
                }
                complex complex::operator +(complex c)
                {
                        complex tmp=c;
                        tmp.real=this->real+tmp.real;
                        tmp.imag=this->imag+tmp.imag;
                        return tmp;
                }
                complex complex::operator -(complex c)
                {
                        complex tmp=c;
                        tmp.real=this->real - tmp.real;
                        tmp.imag=this->imag - tmp.imag;
                        return tmp;
                }
                complex complex::operator *(complex c)
                {
                        complex tmp=c;
                        tmp.real=(real*tmp.real)-(imag*tmp.imag);
                        tmp.imag=(real*tmp.imag)+(imag*tmp.real);
                        return tmp;
                }
                complex complex::operator /(complex c)
                {
                        complex tmp=c;
                        float res= (imag*tmp.real)-(real*tmp.imag);
                        float div=(tmp.real*tmp.real)+(tmp.imag*tmp.imag);
                        tmp.real=(real*tmp.real)+(imag*tmp.imag);
                        tmp.real/=div;
                        tmp.imag= res;
                        tmp.imag/=div;
                   return tmp;
                }
                void complex::printdata()
                {
                        cout<<real<<" + ("<<imag<<")j\n";
                }
void main()
{
   complex c1(5,3),c2(3,2),c3;             // Calls Constructor
   clrscr();
   c3=c1+c2;                                        // Calls overloaded +
   cout<<"\nComplex addition:\n";
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex subtraction:\n";
   c3=c1-c2;                                         // Calls overloaded -
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex multiplication:\n";
    c3=c1*c2;                                        // Calls overloaded *
   c1.printdata();
   c2.printdata();
   c3.printdata();
   cout<<"\nComplex division:\n";
    c3=c1/c2;                 // Calls overloaded /
   c1.printdata();
   c2.printdata();
   c3.printdata();
   getch();
}



OUTPUT:
Complex addition:
5 + (3)j
3 + (2)j
8 + (5)j

Complex subtraction:
5 + (3)j
3 + (2)j
2 + (1)j

Complex multiplication:
5 + (3)j
3 + (2)j
9 + (37)j

Complex division:
5 + (3)j
3 + (2)j
1.615385 + (-0.076923)j


RESULT:
Thus the program to implement the complex number operations using operator overloading.

MATRIX MULTIPLICATION USING FRIEND FUNCTIONS


MATRIX MULTIPLICATION USING FRIEND FUNCTIONS

AIM:
To write a program for matrix multiplication using friend functions using C++.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class Matrix with two matrix array as data member and a function as their member function.
Step 3: Declare a function multiply as a friend function for the class matrix.get the value of the given two matrix using a member function.
Step 4: Calculate the matrix multiplication using friend function. The friend function is called in the main function without the help of the object and object as its arguments.
Step 5: Display the resultant matrix.
Step 6: Stop the program.

PROGRAM

#include<iostream.h>
#include<conio.h>
class matrix
{
int a[5][5],b[5][5],d[5][5];
int i,j,k,l,m,r,c;
public:
void get();
friend void multiply(matrix m1);
};
void matrix::get()
{
cout<<"\n Enter the dimension (n*m) of the first matrix:";
cin>>r>>c;
cout<<"\n Enter "<< r*c <<" element:";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cin>>a[i][j];
 }
}
cout<<"\n The value of the first matrix:";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
}
}
void multiply(matrix m1)
{
int i,j,k;
char ch='y';
do
{
cout<<"\n Enter the dimension (n*m) for the second matrix:";
cin>>m1.l>>m1.m;
cout<<"\n Enter "<< m1.l*m1.m <<" element:";
for(i=0;i<m1.l;i++)
{
 for(j=0;j<m1.m;j++)
 {
  cin>>m1.b[i][j];
 }
}
cout<<"\n The value of the second matrix:";
for(i=0;i<m1.l;i++)
{
cout<<"\n";
for(j=0;j<m1.m;j++)
{
cout<<m1.b[i][j]<<"\t";
}
}
if(m1.r==m1.m)
{
for(i=0;i<m1.l;i++)
{
 for(j=0;j<m1.m;j++)
 {
  m1.d[i][j]=0;
  for(k=0;k<m1.m;k++)
  m1.d[i][j]=m1.a[i][k]*m1.b[k][j]+m1.d[i][j];
 }
}
cout<<"\n Resultant Matrix:";
for(i=0;i<m1.l;i++)
{
 cout<<"\n";
 for(j=0;j<m1.m;j++)
 cout<<m1.d[i][j]<<"\t";
 }
}
else
cout<<"\n Rows and Columns are inequal; Muliplication is not possible";
cout<<"\n Do you want to multiply one more matrix:";
cin>>ch;
}
while(ch=='y');
}
void main()
{
clrscr();
matrix m1;
m1.get();
multiply(m1);
getch();
}


OUTPUT:

 Enter the dimension (n*m) of the first matrix: 2 2

 Enter 4 element:1 2 3 4

 The value of the first matrix:
1       2
3       4
 Enter the dimension (n*m) for the second matrix: 2 2

 Enter 4 element:4 5 6 7

 The value of the second matrix:
4       5
6       7
 Resultant Matrix:
16      19
36      43
 Do you want to multiply one more matrix: n



RESULT:
Thus the program for matrix multiplication using friend functions was written and executed.

CALCULATION OF BANK INTEREST USING DEFAULT ARGUMENT

AIM:
            To calculate bank interest using the concept of default arguments


ALGORITHM:
1.      Start the process.
2.      Create a class with sufficient data members and two member functions.
3.      The default values are specified in the class
4.      The two member functions are defined outside the class.
5.      The object is created for the class and the member function is called from the main function.
6.      The function assigns a default values to the parameter which does not have a matching argument and function call.
7.      If the values for the parameter are specified then the default value is not taken.
8.      The bank interest is calculated.
9.      Display the result.
10.  Stop the process.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class arg
{
            float sum,amount,p,n,r;
            public:
                        float value(float p,int n,float r=0.15);
                        void print(char ch='*', int len=13);
};
float arg::value(float p,int n, float r)
{
            int year=1;
            float sum=p;
            while(year<=n)
{
                        sum=sum*(1+r);
                        year=year+1;
            }
            return(sum);
}
void arg:: print(char ch, int len)
{
            for(int i=1;i<=len;i++)
            cout<<ch;
            cout<<"\n";

void main()
{
            arg a1;
            float tot, res;
            clrscr();
            a1.print();
            tot=a1.value(5000,5);
            cout<<tot<<endl;
            a1.print();
            cout<<endl;
            res=a1.value(5000,1,0.20);
            cout<<res;
            getch();
}


OUTPUT:
            ****************************
            10056.786133
            ****************************
            6000

RESULT:
Thus the program for calculation of simple interest using default arguments was executed.