2:18 PM
c++ Program for concept of unary and binary operator overloading

Develop a program for concept for unary and binary operator overloading in c++  (in dev c++)
Program for Unary operator overloading:
#include<iostream>
#include<conio.h>
using namespace std;
class avi
{
int a;
public:
avi()
{
a=11;
}
avi operator-()
{
a=-a;
}
void show()
{
cout<<a;
}
};
main()
{
avi a1;
-a1;
a1.show();
getch();
}

output:
-11
Program for Unary operator overloading :
#include<iostream>
#include<conio.h>
using namespace std;
class avi
{
int a;
public:
avi(int x)
{
a=x;
}
avi()
{};   //always use default constructor with paramatrizes constructor
avi operator +(avi c)
{
avi s;
s.a=a+c.a;//value of a1 in a and value of a2 in c.a
return(s);
}
void out()
{
cout<<a;
}
};
main()
{
avi a1(5);
avi a2(6);
avi a3;
a3=a1+a2;
a3.out();
}


output:
11

Views: 128 | Added by: vijoriya | Tags: Binary, operator overloading, c++, Concept, unary, program | Rating: 0.0/0
Total comments: 0
avatar