Write the definition for a class called ‘point’ that has x & y as integer data members. Overload the assignment operator (=) to copy one object to another. (Use Default and parameterized constructor to initialize the appropriate objects) Write a C++ program to illustrate the use of above class.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<conio.h>
#include<iostream.h>
class point
{
   float x,y;
   public:
   point()
   {
   }
   point(float a,float b=10) //constructor
   {
    x=a;
        y=b;
   }
   point operator =(point p1) //function overloading
   {
       x=p1.x;
       y=p1.y;
       return(p1);
   }
   void disp()
   {
    cout<<"\n  X = "<<x;
      cout<<"\n  Y = "<<y;
   }
};
int main()
{
 int n;
 cout<<"\nEnter X coordinate: ";
 cin>>n;
 cout<<"\nY coordinate is fixed.";
 point p(n),p1; //object created
 cout<<"\n========Original Points=======";
 p.disp();
 p1=p;
 cout<<"\n========Copy Of points========";
 p1.disp();
 getch();
 return(0);
}

0 Comments