The sum of two points with the rectangular coordinates [a,b] and [c,d] is given
ID: 666044 • Letter: T
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 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: xcoord
and y-coord, and a constructor that takes two sets of points. Provide seven
functions within the class (see below). You may add other functions where you think
necessary.
addPoints(Points coord) // add the two points
addPoints(int x, int y) // an overloaded function that will add the two points
print() // print the sets of points
setPoints(int x, int y) // will reset the coordinate to new values
getA() // get ‘x-coord’ value
getB() // get ‘y-coord’ value
reset( ) // reset points to zeros
Use the main() program given below to minimally test your class design to verify that
your design works accordingly. You can add other necessary line of codes to thoroughly
test your class design.
#include <iostream>
using namespace std;
int main()
{
Points p1, p2(6,2), p3(5,5), p4(2,1);
p1.print();
p2.addPoints(4, 3);
p2.print();
// add p2 to p1
p1.addPoints(p2);
p1.print();
// add p3 and p4
Points p5(p3, p4);
p5.print();
p2.reset();
p2.setPoints(7, 11);
cout << “Point p2 = ” << p2.getA() << “,“ << p2.getB() << endl;
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
typedef struct{
int a;
int b;
}Points;
Points function(Points p1, Points p2){
Points p;
p.a=p1.a+p2.a;
p.b=p1.b+p2.b;
return p;
}
int main() {
Points p[2], pr;
cout<<"Enter two coordinate:"<< endl;
for(int i=0;i<2;i++){
cin>>p[i].a>>p[i].b; //enter ith coordinate
}
pr=function(p[0],p[1]);
cout<<"Result is: ";
cout<<"["<<pr.a<<", "<<pr.b<<"]"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.