Displaying Negative of Inputted numbers using Operator Overloading cpp
In this program, we are going see how to Display negative of inputted numbers using Function Overloading  in C++ Programming Language.
 
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <math.h>
#include <conio.h>

class Point
{
    int x, y;

    public:
    Point(int xc = 0, int yc = 0)
    {
        x = xc;
        y = yc;
    }
    void print()
    {
        cout << "x: " << x << "\ty:" << y << endl;
    }
    Point operator-()
    {
        return Point(-x, -y);
    }
};

void main()
{
    clrscr();
    int num1, num2;
    cout << "Enter Two numbers : ";
    cin >> num1 >> num2;
    Point p1(num1, num2);
    p1.print();
    Point p2 = -p1;
    p2.print();
    getch();
}

//.......Coded by SAHIL SHAIKH

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <math.h>
using namespace std;

class Point
{
    int x, y;

    public:
    Point(int xc = 0, int yc = 0)
    {
        x = xc;
        y = yc;
    }
    void print()
    {
        cout << "x: " << x << "\ty:" << y << endl;
    }
    Point operator-()
    {
        return Point(-x, -y);
    }
};

int main()
{
    int num1, num2;
    cout << "Enter Two numbers : ";
    cin >> num1 >> num2;
    Point p1(num1, num2);
    p1.print();
    Point p2 = -p1;
    p2.print();
    return 0;
}

//.......Coded by SAHIL SHAIKH

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post