Tuesday, 21 April 2015

Write a program that will demonstrate overloading unary operator

Write a program that will demonstrate overloading unary operator. The program that uses the following properties – class names- test; three integer type private member variables- e,f,g; three public member function getdata(int x, int y, int z), display(), operator-(). Function getdata(-50, 88, -19) assigns the values for e, f, g; operator-() changes the sign of the value of the object. display() member function displays the result as output before change sign and after change sign which is as follows-
S: -50 88 -19

S: 50 -88 19

#include<iostream>
using namespace std;
class test
{
    int e,f,g;
public:
    void getdata(int x,int y,int z);
    void display();
    void operator-();
};
void test::getdata(int x,int y,int z)
{
    e=x;
    f=y;
    g=z;
}
void test::display()
{
    cout<<e<<" "<<f<<" "<<g<<endl;
}
void test::operator-()
{
    e=-e;
    f=-f;
    g=-g;
}
int main()
{
    test  p;
    p.getdata(-50,88,-19);
    cout<<"Before change sign : ";
    p.display();
    -p;
    cout<<"After change sign : ";
    p.display();
    return 0;
}

No comments:

Post a Comment