Class storage Errors

I need to
Store the data points x1, y1, x2, y2 in the private section of the class as doubles

Provide public member functions to set the values into the class

Provide a member function called GetDistance that returns the distance between the points

These are the instructions for my lab and This is the code I came up with but I do not understand class storage nor how to set the class storage as doubles.

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
 #include<stdio.h>

#include<cmath>

#include <iostream>

using namespace std;


void main() 
{ 
 Distance a; 
 double length; 
 
 
a.setx1(12.0); 
a.sety1(20.0);
a.setx2(40.0);
a.sety2(50.0); 
 length = GetDistance(a.x1, a.x2, a.y1, a.y2); 
 cout << "distance is" << length << "/n"; 
}

double GetDistance (double x1, double x2, double y1, double y2)
{
	double distance=0;

       distance=sqrt(pow((x2-x1),2)+pow((y2-y1),2));

       return GetDistance;
}
Last edited on
You mean something like this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Distance
{
public :
    void setx1( double x1 )
    {
        this->x1 = x1;
    }

    // other setters

    double getDistance()
    {
        // code
    }

private :
    double x1;
    double y1;
    double x2;
    double y2;
};



Btw, main() should be int & u don't need to #include <stdio.h>
Last edited on
I am just trying to figure out the class part first then I will try to apply it to main()

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
#include <iostream>
#include <cmath>
using namespace std;

// Declare Class
class Distance {
	double x1, x2, y1, y2;

	double setx1, sety1, setx2, sety2;

public:	

	Distance(double x1, double x2, double y1, double y2){
		setx1 = x1;
		setx2 = x2;
		sety1 = y1;
		sety2 = y2;
	}
	double GetDistance(){ return sqrt( pow(x2 - x1, 2) + pow(y2 - y1, 2) );}

//functions
double get_x1() { return x1; }

double get_x2() { return x2; }

double get_y1() { return y1; }

double get_y2() { return y2; }

};

int main ()
{
}

This is my new updated code for the Class part of it How does it look?
Last edited on
This is how my Prof. has the example for the code set up ...
(He is using void main() instead of int main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void main()
{
 Distance a;                  // create a Distance object

 Double length; 
 
 // set the values into the object; 
a.setx1(12.0) 
a.sety1(20.0) 
a.setx2(40.0) 
a.sety2(50.0) 
 
 length = a.GetDistance(); 
 cout << “distance is “ << length << “\n”; 
}
You won't need the additional variables setx1, sety1, setx2 and sety2
You can directly assign the arguments in the constructor to it :
1
2
3
4
5
6
7
8
9
10
11
12
13
class Distance
{
    double x1, x2, y1, y2;

public :
    Distance( double x1, double x2, double y1, double y2 )
    : x1(x1), x2(x2), y1(y1), y2(y2) // http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
    {
        // .. empty body
    } 

    // ... setters & getters
};


other than that it looks good. although you might want to fix the indentation a little bit
Last edited on
so you are saying I can replace
1
2
3
4
5
	Distance(double x1, double x2, double y1, double y2){
		setx1 = x1;
		setx2 = x2;
		sety1 = y1;
		sety2 = y2;


With
1
2
Distance( double x1, double x2, double y1, double y2 )
    : x1(x1), x2(x2), y1(y1), y2(y2)

but will i not have to redefine x1-y2 in this section?
1
2
3
4
5
6
7
8
9
10
11
12
double GetDistance(){ return sqrt( pow(x2 - x1, 2) + pow(y2 - y1, 2) );}

//functions
double get_x1() { return x1; }

double get_x2() { return x2; }

double get_y1() { return y1; }

double get_y2() { return y2; }

};
Yes.

What do you mean by redefine ?
With
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Distance {
	double x1, x2, y1, y2;

	double setx1, sety1, setx2, sety2;

public:	

	Distance( double x1, double x2, double y1, double y2 )
    : x1(x1), x2(x2), y1(y1), y2(y2);
	}
	double GetDistance(){ return sqrt( pow(x2 - x1, 2) + pow(y2 - y1, 2) );}

//functions
double get_x1() { return x1; }

double get_x2() { return x2; }

double get_y1() { return y1; }

double get_y2() { return y2; }

};

the identifiers x1,x2,y1,y2 all show up as undefined and I do not know why.

But if I use this code same but with out the ";" afterx1(x1), x2(x2), y1(y1), y2(y2);
I get an error saying expected a "{" on the following line
1
2
: x1(x1), x2(x2), y1(y1), y2(y2)
	}
you don't need to put a semicolon & u are missing the opening brace

1
2
3
4
5
Distance( double x1, double x2, double y1, double y2 )
: x1(x1), x2(x2), y1(y1), y2(y2)
{
    // .. empty body
} 


And also like i said before, you won't need variables setx1 - sety2
Last edited on
Oh
I thought I tried that Thanks a lot now just to figure out how to set it to the point values described
1
2
3
4
a.setx1(12.0) 
a.sety1(20.0) 
a.setx2(40.0) 
a.sety2(50.0) 


Any tips??
For the main function do I need to redeclare X1-y2 like this?
1
2
3
4
5
6
7
void main ()
{
	double length;
	double x1, x2, y1, y2;
	length = GetDistance(x1,x2,y1,y2);
	cout << "distance is" << length << "\n";
}


Or what is the best way to call them?
1
2
3
void setx1( double x1 ) { this->x1 = x1; } // x1 parameter will shadow the x1 member,
                                                                  // we need to refer to it explicitly using this
// etc ... 


http://www.learncpp.com/cpp-tutorial/84-access-functions-and-encapsulation/
Last edited on
Do void setx1( double x1 ) { this->x1 = x1; }
for x2 y1 y2 need to be seperate functions or do they all exist under Main()?

By the way you are great!
they must be member functions of class Distance
Okay thanks Ill post the finished source code when I am done thanks for the loads of help!
Updated Code
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
#include <iostream>
#include <cmath>
using namespace std;

// Declare Class
class Distance {
	double x1, x2, y1, y2;

	void setx1 ( double x1 ) { this-> x1 = 10.0;}
	void setx2 ( double x2 ) { this-> x2 = 20.0;}
	void sety1 ( double y1 ) { this-> y1 = 40.0;}
	void sety2 ( double y2 ) { this-> y2 = 50.0;}

public:	

	Distance( double x1, double x2, double y1, double y2 )
    : x1(x1), x2(x2), y1(y1), y2(y2)
	{
}
double GetDistance() {return sqrt( pow(x2 - x1, 2) + pow(y2 - y1, 2) );}

//functions
double get_x1() { return x1; }

double get_x2() { return x2; }

double get_y1() { return y1; }

double get_y2() { return y2; }

};
void main ()
{
	double length;
	double x1, x2, y1, y2;
	length = GetDistance(x1,x2,y1,y2);

	cout << "distance is" << length << "\n";

The only Error Showing up is in void main () With length = GetDistance(x1,x2,y1,y2);
double GetDistance() {return sqrt( pow(x2 - x1, 2) + pow(y2 - y1, 2) );}

GetDistance() doesn't accept any arguments

Just remove the arguments in the call to GetDistance at line 36
Last edited on
where did you declared the function GetDistance(), also the GetDistance() you defined does not take any arguments.
Topic archived. No new replies allowed.