Question: The sum of two points with the rectangular coordinates [a,b] and [c,d] is given by [a+c,b+d]. We therefore write: [a,b] + [c,d] = [a+c,b+d] For
The sum of two points with the rectangular coordinates [a,b] and [c,d] is given by [a+c,b+d]. We therefore write: [a,b] + [c,d] = [a+c,b+d] For example [2,5]+ [6,2] = [8,7]. Design a C++ class called Points. Use the two integer numbers as your class private data. Create three constructors: a default constructor, a constructor that takes two integers, and a constructor that takes two sets of points. Provide at least two functions within the class: addPoints(Points x) that will add the two points and print the result print() that will print the sets of points Use the following C++ main() program below to minimally test your class design to verify that your design works accordingly. #include using namespace std; int main() { Points p1, p2(3,2), p3(4,2), p4(2,1); p1.print(); p2.print(); // add p2 to p1 p1.addPoints(p2); p1.print(); // add p3 and p4 Points p5(p3, p4); p5.print(); system("pause"); return 0; }
Answer
#include
using namespace std;
// class Points that contains the rectangular coordinates of a point
class Points{
// data members representing x- and y- coordinates of a point
private:
int x,y;
public:
// default constructor initializing to (0,0)
Points()
{
x=0;
y=0;
}
// parameterized constructor that takes two integers as input
Points(int x1,int y1)
{
x=x1;
y=y1;
}
// parameterized constructor that takes two sets of Points as input
// initializes the Points object with the sum of the rectangular coordinates of the two Points passed as inputs
Points(Points p1,Points p2)
{
x = p1.x + p2.x;
y = p1.y + p2.y;
}
// function to add Points x to the calling object
void addPoints(Points x)
{
this->x = this->x + x.x;
y = y +x.y;
}
// function to print the coordinates
void print()
{
cout<<"("< } }; int main() { Points p1, p2(3,2), p3(4,2), p4(2,1); p1.print(); p2.print(); // add p2 to p1 p1.addPoints(p2); p1.print(); // add p3 and p4 Points p5(p3, p4); p5.print(); system("pause"); return 0; } Please help me to understand // parameterized constructor that takes two sets of Points as input // initializes the Points object with the sum of the rectangular coordinates of the two Points passed as inputs Points(Points p1,Points p2) { x = p1.x + p2.x; y = p1.y + p2.y; } // function to add Points x to the calling object void addPoints(Points x) { this->x = this->x + x.x; y = y +x.y; } What does Points(Points p1,Points p2) Points p1 do and what the use of .(dot) operator?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
