Monday, 22 December 2014

C++ PROGRAM: A point in the X-Y plane is represented by its x-coordinate and y-coordinate. Design a class point and perform following actions:

A point in the X-Y plane is represented by its x-coordinate and y-coordinate. Design a class point and perform following actions:
a.     Setting the coordinates of point.
b.     Printing the coordinates of the point.
c.     Returning the x and y coordinate.
d.     Translate the point by a value.

SOLUTION:
#include<iostream.h>
#include<conio.h>
class Point{
int x;
int y;
public:
void setPoint(int xx, int yy){
cout<<"Enter your desired x point:";
cin>>xx;
cout<<"enter your desired y point:";
cin>>yy;
x = xx;
y = yy;
}
void printPoint(){
cout<<"X - " <<x<<endl<<"Y - "<<y;
}
int getX(){
return x;
}
int getY(){
return y;
}
void translate(int txx, int tyy){
x = x+txx;
y = y+tyy;
}
};

void main(){
int xx, yy;
clrscr();
Point p1;
p1.setPoint(xx,yy);
p1.printPoint();
int x = p1.getX();
int y = p1.getY();
cout<<endl<<"x = "<<x<<endl<<"y = "<<y;
cout<<endl<<"Enter x to translate:";
int tx, ty;
cin>>tx;
cout<<"Enter y to translate:";
cin>>ty;
p1.translate(tx, ty);
p1.printPoint();
getch();
}
OUTPUT:



No comments:

Post a Comment