Saturday, January 25, 2014

C++ Program to Implement the Concept Binary Operator Overloading

with 0 Comment
OVERLOADING BINARY OPERATOR

AIM:

  • To write a C++ program to Implement the concept Binary Operator Overloading.

ALGORITHM:
1. Start the program
2. Include suitable header file
3. Create a class with necessary variables and function
4. Get the value as two variables
5. Overload an binary operator
6. Execute the program
7. Stop the program

CODING:
#include <iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float image)
{
x=real;
y=image;
}
complex operator + (complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y;
}
int main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 =";
c1.display();
cout<<"c2 =";
c2.display();
cout<<"c3 =";
c3.display();
getch();
return 0;
}

OUTPUT:
c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2

RESULT:

  • Thus to write a C++ program to concatenate two string using the concept Binary operator overloading was successfully completed.


0 comments:

Post a Comment

Powered by Blogger.

Blog Archive