Create a simple Point class Members: int x, y These should be private Instance M
ID: 3548260 • Letter: C
Question
- Create a simple Point class
- Members:
- int x, y
- These should be private
- Instance Methods:
- Constructor 1
-
- Takes no parameters
- Sets x and y to 0.
- Constructor 2
-
- Takes two int parameters, x and y
- Sets x and y to the given values
- Add
-
- Takes another Point and adds its x and y values to this Point
- Does not return anything.
- Subtract
-
- Takes another Point and subtracts its x and y values from this Point
- Does not return anything.
-
- Takes no parameters.
- Prints the point in the following format: (x, y)
- Does not return anything.
- Driver
- Make several Points and add / subtract them. Print out the values of the Points before and after the additions / subtractions.
Explanation / Answer
#include <iostream>
using namespace std;
class Point
{
public:
Point(void)
{
x = 0;
y = 0;
}
Point(int X, int Y)
{
x = X;
y = Y;
}
~Point(void)
{
}
void add(Point p)
{
x += p.x;
y += p.y;
}
void Subtract(Point p)
{
x -= p.x;
y -= p.y;
}
void Print(void)
{
cout << "(" << x << "," << y << ")" << endl;
}
private:
int x;
int y;
};
int main()
{
Point p(1,2), q(3,4);
/**** Add point p to q ****/
cout << "P = ";
p.Print();
cout << "Q = ";
q.Print();
p.add(q);
cout << "P + Q = ";
p.Print();
/**** Subtract point p to q ****/
cout << "P = ";
p.Print();
cout << "Q = ";
q.Print();
p.Subtract(q);
cout << "P - Q = ";
p.Print();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.