OOPC Practical 14


Practical : 14
Subject : Object Oriented Programming with C++

Aim : Write a C++ program to perform Binary operator overloading


Source Code :
#include<iostream.h>
#include<conio.h>
class complex
{
inta,b;
public:
voidgetvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}

Output :


Pratik Boghani

Author & Editor

Life is all about the next step.

0 comments:

Post a Comment