Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

C++ PROGRAM FOR SIMPLE UNARY OPERATOR

AIM:
To write a c++ program using simple unary operator.
ALGORITHM:
Step1: Start the program.
Step2: Declare the class name as unary.
Step3:Declare the three variable x,y,z.
step4: Inside the public access specifier declare the constructor.
Step5:Inside the constructor get the three integer value.
Step6: using the display() function display the three integer value.
Step7: using the --operator do the decrement operation.
Step8: using the ++operator do the increment operation.
Step9: Inside the main function create the object and call the function.
Step10: End of the program.

PRO GRAM CODE:
#include<iostream>
using namespace std;
class unary
{
int x,y,z;
public:
unary()
{
cout<<enter three integer;
cin>>x>>y>>z;
}
void display()
{
cout<<the three number are<<x<<y<<z<<endl;
}
void operator--()
{
x=--x;
y=--y;
z=--z;
}
void operator++()
{
x=++x;
y=++y;
z==++z;
}
int main()
{
unary s;
s.diplay()
--s;
cout<<the decremented value is <<endl;
s.display();
++s; cout<<the incremented value is <<endl;
s.display();
return 0;
}
OUTPUT:
enter the three integer 3 4 5
the decremented value
the three number are 2 3 4
the incremented value
the three number are 3 4 5














C++ PROGRAM FOR UNARY OPERATOR USING FRIEND FUNCTION
AIM:
To write a c++ program for unary operator using friend function.
ALGORITHM:
Step1: Start the program.
Step2: Declare the class name as xyz.
Step3: Declare the three integer value.
Step4: Inside the public access specifier declare the get() function.
Step5: Inside the get() function get the three integer value.
Step6: Inside the display() function display the integer value.
Step7: By using the friend function do the decrement operation outside
the class.
Step8: Inside the main function create the object.
Step9: using the object call the function.
Step10: End of the program.
PROGRAM CODE:
#include<iostream>
using namespace std;
class xyz
{
int x,y,z;
public:
void get()
{
cout<<enter the three value;
cin>>x>>y>>z;
}
void display()
{
cout<<x<<y<<z<<endl;
}
friend voidoperator-(xyz&s);
};
void operator-(xyz&s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
int main()
{
xyz s1;
s1.get();
cout<<before overloading<<endl;
s1.display();
cout<<after overloading<<endl;
-s1;
s1.display();
return 0;
}
OUTPUT
enter value 1 2 3
before overloading 1 2 3
after overloading -1 -2 -3
C++ PROGRAM FOR SIMPLE BINARY OPERATOR
AIM:
To write a c++ program for binary operator
ALGORITHM:
Step1: Start the program.
Step2: Declare the class name as complex.
Step3: Declare the two float variable x and y.
Step4: Inside the public access specifier declare the constructor.
Step5: Pass the argument real and imaginary.
Step6: By using the + operator add the two complex numbers.
Step7: Inside the main function create the object .
step8: using the object call function.
Step9: Stop the program.

PROGRAM CODE:
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex()
{
}
complex(float real, flat imaginary)
{
x=real;
y=imaginary;
}
complex operator+(complex);
void display();
};
complex complex::operator+(complex)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return (temp);
}
void complex::display()
{
cout<<x<<+j<<y;
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.0,2.7);
c3=c1+c2;
cout<<c1=<<endl;
c1.display();
cout<<c2=<<endl;
c2.display();
cout<<c3=<<endl;
c3.display();
return 0;
}
OUTPUT:
c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2












C++ PROGRAM FOR BINARY OPERATOR USING FRIEND FUNCTION
AIM:
To write a c++ program for binary operator using friend function.
ALGORITHM:
Step1: Start the program.
Step2: Declare the class name as time.
Step3: Declare the variables hours and minutes.
Step4: Inside the public access specifier declare the constructor.
Step5: Inside the constructor initialize hour=0, minutes=0.
Step6: Inside the display function display the time.
Step7: By using the friend function do the time increment operation.
Step8: Inside the main function pass the argument to the function.
Step9: using the object call the function.
Step10: End of the program.

PROGRAM CODE:
#include<iostream>
using namespace std;
class time
{
int hours, minutes;
public:
time()
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display()
{
cout<<hours<<hours<<minutes<<minutes<<endl;
}
friend time operator+(time,time);
};
time operator+(time y,time z)
{
int h=y-hours+z.hours;
int m=y-minutes+z,minutes;
while(m>60)
{
m=m-60;
n=n+1;
}
return time(h,m);
}
int main()
{
time t1(2,40);
time t2(3,30);
time t3;
t3=t1+t2;
t3.display();
return 0;
}
OUTPUT:
6 hours 10 minutes

You might also like