0% found this document useful (0 votes)
60 views

Operator Overloading and Friend Function

The document describes a C++ program that implements operator overloading and friend functions. It creates a complex number class with real and imaginary variables as members. It overloads the +, *, and / operators as friend functions to perform addition, multiplication, and division of complex numbers. The program defines the complex number class, overloads the relevant operators as friend functions, creates complex number objects, performs operations on them using the overloaded operators, and displays the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Operator Overloading and Friend Function

The document describes a C++ program that implements operator overloading and friend functions. It creates a complex number class with real and imaginary variables as members. It overloads the +, *, and / operators as friend functions to perform addition, multiplication, and division of complex numbers. The program defines the complex number class, overloads the relevant operators as friend functions, creates complex number objects, performs operations on them using the overloaded operators, and displays the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Expt no:4 OPERATOR OVERLOADING AND

27-7-2010 FRIEND FUNCTION

AIM: To write a C++ program to implement operator overloading and


friend function

ALGORITHM:

STEP 1: Start the program


STEP 2: Create Class with real and imaginary variables as members
STEP 3: Create a default Constructor and a parameterized constructor
STEP 4: Create a display function to print the output
STEP 5: Overload the operator * , / as friend functions
STEP 6: Display the output
STEP 7: Stop the program

PROGRAM:

#include<iostream.h>
#include<conio.h>
class complex
{float x,y;
public:
complex()
{
x=y=0;
}
complex(float real, float img)
{
x=real;
y=img;
}
friend complex operator + (complex a, complex b);
friend complex operator * (int a, complex b);
friend complex operator *(complex a, int b);
void display();
};
complex operator + (complex a, complex b)
{
complex c;
c.x=a.x+b.x;
c.y=a.y+b.y ;
return c;
}
complex operator * (int a, complex b)
{
complex c;
c.x=a*(b.x);
c.y=a*(b.y) ;
return c;
}
complex operator * (complex a, int b)
{
complex c;
c.x=b*(a.x);
c.y=b*(a.y) ;
return c;
}
void complex::display()
{
cout<<”/n”<<”The Number is:”<<x<<”+i”<<y;
}
void main()
{
clrscr();
complex c1,c2,c3,c4,c5;
c1=complex(2.5,3.5);
cout<<”/n”<<c1;
c1.display;
c2=complex(1.6,2.7) ;
cout<<”/n”<<c2;
c2.display;
c3=c1+c2 ;
cout<<”/n”<<c1+c2;
c3.display;
c4=2*c1 ;
cout<<”/n”<<c1*2;
c4.display;

c5=c1*3 ;
cout<<”/n”<<c1*3;
c5.display;
getch() ;
}

OUTPUT:

c1= 2.5 + i3.5


c2=1.6 + i3.7
c1+c2 = 4.1 + i6.2
c2*c1= 5 + i7
c1*3= 7.5 + i10.5

RESULT: The C++ program is written and implemented.

You might also like