Saturday, April 17, 2021

simple program using operator overloading (OOP) (68)

#include<iostream>

using namespace std;

class point //here i make a class name point

{

private:

int x,y; //here i declear two coordinate of a point 

public:

    point() //here i define the default constructor 

    {

    x=0;

    y=0;

}

point(int a,int b) //here i define a parameterized constructor

{

x=a;

y=b;

}

void display() //here i define a function that display the values

{

cout<<"the value of x for both points is:"<<x<<endl;

cout<<"the value of y for both points is:"<<y<<endl;

}

point operator +(point o1); //here i overload the + operator

point operator -(point o2); //here i overload the - operator

point operator *(point o3); //here i overload the * operator

point operator /(point o4); //here i overload the / operator

    friend bool operator==(point &point1, point &point2); //here i overload the == operator

    friend bool operator!=(point &point1, point &point2);//here i overload the != operator

};

point point::operator +(point o1) //here i overload the + operator

{

point result;

result.x=x+o1.x;

result.y=y+o1.y;

return result;

}

point point::operator -(point o2) //here i overload the - operator

{

point result;

result.x=x-o2.x;

result.y=y-o2.y;

return result;

}

point point::operator *(point o3) //here i overload the * operator

{

point result;

result.x=x*o3.x;

result.y=y*o3.y;

return result;

}

point point::operator /(point o4) //here i overload the / operator

{

point result;

result.x=x/o4.x;

result.y=y/o4.y;

return result;

}

bool operator==(point &point1, point &point2)//here i overload the == operator

  return (point1.x==point2.x||point1.y==point2.y);

}

bool operator!=(point &point1, point &point2)//here i overload the != operator

  return (point1.x!=point2.x||point1.y!=point2.y);

}

int main()

{

int a=1;

point point1,point2,point3; // here i make a three objects

point1=point(30,60); //here i pass the value to the constructor through object point1

point2=point(20,40); //here i pass the value to the constructor through object point2

point3=point1+point2; // here i add the two objects and store in object point3

cout<<"\tafter adding the two points:"<<endl;

cout<<endl;

point3.display(); //here i call the display function

cout<<endl;

point3=point1-point2; // here i subtract the two objects and store in object point3

cout<<"\tafter subsract the two points:"<<endl;

cout<<endl;

point3.display(); //here i call the display function

cout<<endl;

point3=point1*point2; // here i multiply the two objects and store in object point3

cout<<"\tafter multiply the two points:"<<endl;

cout<<endl;

point3.display(); //here i call the display function

point3=point1/point2; // here i divide the two objects and store in object point3

    cout<<endl;

cout<<"\tafter divide the two points:"<<endl;

cout<<endl;

point3.display(); //here i call the display function

    cout<<endl;

    cout<<"\tafter checking the equality the two points:"<<endl;

if(point1 == point2)

    {

        cout << "Both the points values are equal";

    }   

    cout<<endl;

    cout<<"\tafter checking the unequality the two points:"<<endl;

if(point1 != point2)

    {

        cout << "Both the points values are not equal";

    }   

} 

No comments:

Post a Comment