How to add objects to a program with class?

My homework problem is "please write a C++ program which asks the user to enter at the keyboard the numeric values of two pairs of ordered-pair coordinates on a graph. First, the program allows the user to enter the numeric values and tests them for validity. Second, it instantiates two objects of the class, using those two pairs of values. Third, it calculates the distance between the two points and displays it on the user’s monitor. Fourth, it calculates the slope of the straight line which joins the two points and displays the slope. Also, please test for a 0 denominator in the slope expression, because a slope with a 0 denominator is undefined."
I have made a program that works, but it does not contain 2 objects. How should I add them in?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

// Global area for defining functions and C++ classes
class OrderedPair{
	public:
		double distance(double a, double b, double c, double d)
			{x1=a; x2=b; y1=c; y2=d;
			return sqrt (pow(x2-x1, 2)+pow(y2-y1, 2));}
		double slope(double a, double b, double c, double d)
			{x1=a; x2=b; y1=c; y2=d;
			return ((y2-y1)/(x2-x1));}
	private:
		double x1, y1, x2, y2;
};

int main () {
	double a, b, c, d;
	OrderedPair A;
	cout << "Enter the first set of ordered pair." << endl;
	cin >> a >> b;
	cout << "Enter the second set of ordered pair." << endl;
	cin >> c >> d;
	if (!cin.eof()&&cin.good()){
		if (c-a==0){
			cout << "The distance between the two points is " << A.distance(a, b, c, d) << "." << endl;
			cout << "The slope cannot be calculated because of division by 0." << endl;
		}
		else{
			cout << "The distance between the two points is " << A.distance(a, b, c, d) << "." << endl;
			cout << "The slope of the line that joins the two points is " << A.slope(a, b, c, d) << "." << endl;
		}
	}
	else
		cout << "The data entered is invalid" << endl;
		
	return EXIT_SUCCESS;
}
Last edited on
Your OrderedPair class should contain (as the name might hint) a pair of values. This class should represent a point.
So you want to read pair of values, create OrderedPair from them, read second pair of value and create second OrderedPair from them. Then you will operate them to find whatever you need.

So you will need following functions:
1
2
double distance(const OrderedPair& a, const OrderedPair & b);
double slope(const OrderedPair& a, const OrderedPair & b);
and your OrderedPair should hold 2 values and let you access them.
Did you mean that I need to have one class for each ordered pair?
You have one class, but several objects (instances of class).
Ok, so I have another version of the program that looks better but is not working at all. My teacher told me to add set and get methods and constructors, but I have no idea what to do or what's happening to the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

// Global area for defining functions and C++ classes
class OrderedPair{
	public:
		OrderedPair(OrderedPair&, OrderedPair&);
		OrderedPair(double,double);
		void setA(double, double);
		void setB(double, double);
		double getA();
		double getB();
		double distance(const OrderedPair&, const OrderedPair&);
		double slope(const OrderedPair&, const OrderedPair&);

		
	private:
		double x, y, x1, y1;
};
OrderedPair::OrderedPair(double x, double y)
	{}
OrderedPair::OrderedPair(OrderedPair&A, OrderedPair&B)
	{x==A.x; y==A.y; x1==B.x; y1=B.y;}
	
void OrderedPair::setA(double a, double b){x==a; y=b;}
void OrderedPair::setB(double c, double d){x1==c; y1==d;}
double OrderedPair::getA(){return x; return y;}
double OrderedPair::getB(){return x1; return y1;}

double OrderedPair::distance(const OrderedPair&A, const OrderedPair&B)
	{return sqrt (pow(B.x-A.x, 2)+pow(B.y-A.y, 2));}
double OrderedPair::slope(const OrderedPair&A, const OrderedPair&B)
	{return ((B.y-A.y)/(B.x-A.x));}
				
int main () {
	double a, b, c, d;
	OrderedPair A(a, b);
	OrderedPair B(c, d);
	cout << "Enter the first set of ordered pair." << endl;
	cin >> a >> b;
	cout << "Enter the second set of ordered pair." << endl;
	cin >> c >> d;
	if (!cin.eof()&&cin.good()){
		if (c-a==0){
			A.setA(a, b);
			B.setB(c, d);
			cout << A.getA() << endl;
			cout << "The distance between the two points is " << A.distance(A, B) << "." << endl;
			cout << "The slope cannot be calculated because of division by 0." << endl;
		}
		else{
			A.setA(a, b);
			B.setB(c, d);
			cout << "The distance between the two points is " << A.distance(A, B) << "." << endl;
			cout << "The slope of the line that joins the two points is " << A.slope(A, B) << "." << endl;
		}
	}
	else
		cout << "The data entered is invalid" << endl;
		
	return EXIT_SUCCESS;
}


I know this looks really messed up...
Last edited on
Why your OrderedPair class has four member variables?
Why your setter takes two values per parameter?

Also distance and slope should be either non-member, or take one argument. (or be static member, but i believe this is out of scope of your cource right now)

You need:
1) create two double variables.
2) read coordinates of one point and store them in these doubles.
3) create OrderedPair from these coordinates.
4) read coordinates of second point reusing variables created in (1).
5) create second OrderedPair using new coordinates.
*) now you should not change created OrderedPairs. That means no setter calls (getters are fine).
*) Neither should you touch variables created in (1).
6) Do work on these ordered pairs.
Last edited on
So I've made made a few changes to the things I understand, but I'm still really confused...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

// Global area for defining functions and C++ classes
class OrderedPair{
	public:
		OrderedPair(OrderedPair&, OrderedPair&);
		OrderedPair(double,double);
		void setX(double);
		void setY(double);
		double getX();
		double getY();
		double distance(const OrderedPair&, const OrderedPair&);
		double slope(const OrderedPair&, const OrderedPair&);

		
	private:
		double x, y;
};
OrderedPair::OrderedPair(double x, double y) //something is probably missing in here...?
	{}
OrderedPair::OrderedPair(OrderedPair&A, OrderedPair&B)
	{}
	
void OrderedPair::setX(double x){}
void OrderedPair::setY(double y){}
double OrderedPair::getX(){return x;}
double OrderedPair::getY(){return y;}

double OrderedPair::distance(const OrderedPair&A, const OrderedPair&B) //what should I do with this if you said distance and slope should either be non member, or take one argument??
	{return sqrt (pow(B.x-A.x, 2)+pow(B.y-A.y, 2));}
double OrderedPair::slope(const OrderedPair&A, const OrderedPair&B)
	{return ((B.y-A.y)/(B.x-A.x));}
				
int main () {
	double a, b, c, d;
	cout << "Enter the first set of ordered pair." << endl;
	cin >> a >> b;
	OrderedPair A(a, b);
	A.setX(a); //<--is this what I am supposed to do???
	A.setY(b);
	cout << "Enter the second set of ordered pair." << endl;
	cin >> c >> d;
	OrderedPair B(c, d);
	B.setX(c);
	B.setY(d);
	if (!cin.eof()&&cin.good()){
		if (c-a==0){
			cout << "The distance between the two points is " <<  << "." << endl; //how should I get the answers for slope and distance after they are calculated? 
			cout << "The slope cannot be calculated because of division by 0." << endl;
		}
		else{
			cout << "The distance between the two points is " <<  << "." << endl;
			cout << "The slope of the line that joins the two points is " <<  << "." << endl;
		}
	}
	else
		cout << "The data entered is invalid" << endl;
		
	return EXIT_SUCCESS;
}
1) Remove setX/setY functions. They are not needed in this code and confuse you.
2)
is this what I am supposed to do???
No. A constructor should handle setting values. Which it is not doing right now. You need to assign constructor parameters to class members. It is better if you call parameters and members differently so you would non confuse two:
1
2
3
4
OrderedPair::OrderedPair(double xxx, double yyy) //Different names
{
    //What you need to write to make x and y members equal xxx and yyy?
}


what should I do with this if you said distance and slope should either be non member
First, remove them from your class.
1
2
3
4
    double getY();
    double distance(const OrderedPair&, const OrderedPair&);
    double slope(const OrderedPair&, const OrderedPair&);
  private:
Second, make them freestanding functions:
double OrderedPair::distance(const OrderedPair&A, const OrderedPair&B)
Third, use getters to access private members ( A.x → A.getX() )
Regarding the constructor, see http://www.cplusplus.com/articles/1vbCpfjN/
I made something like this, and when I compiled it, it says "passing 'const OrderedPair' a 'this' argument of 'double OrderedPair::getX() discards qualifiers", what does it mean??
1
2
3
4
5
6
7
8
double distance(const OrderedPair&A, const OrderedPair&B)
	{double p, q, r, s;
	p=A.getX(); q=A.getY(); r=B.getX(); s=B.getY(); // <--error in this line, similar case for the other lines too
	return sqrt (pow(r-p, 2)+pow(s-q, 2));}
double slope(const OrderedPair&A, const OrderedPair&B)
	{double p, q, r, s;
	p=A.getX(); q=A.getY(); r=B.getX(); s=B.getY();
	return ((s-q)/(r-p));}
This is because you did not made your functions const-correct:
1
2
3
double getY() const;
//...
double OrderedPair::getY() const {return y;}
The A is const. Only const member functions of such object may called.
1
2
3
4
class OrderedPair {
  public:
  double getX() const;
...


PS. Bad style: p=A.getX(); q=A.getY(); r=B.getX(); s=B.getY();
Better style:
1
2
3
4
p=A.getX();
q=A.getY();
r=B.getX();
s=B.getY();

Why? It is easier to see which statement contains errors, when they are not all on the same line.
Topic archived. No new replies allowed.