Help with classes!

Pages: 123
To MikeyBoy: I'm not ignoring them, I just don't know how to properly answer them. My best answer would be to post the entire code. You've done this far longer than I have and it confuses me even more when you're confused. I don't know how to answer something when the person who is more knowledgeable can't answer either and can't see what I'm trying to get at.

But most of my questions are asking you about the code you've written. You must know the answers, because you're the one who wrote the code. For example:

Where have you defined set_x1, set_x2, set_y1 and set_y2?

Well? I can't know know about the rest of the code you've written, but you do.
Well, that would mean that those four local variables will be in scope for the entire main function. Is that what you want?

Well, do you? I don't know what you want to do with those local variables, but you must do, because you made the effort to declare them. You must have had a reason for declaring them.
What are those parameters? Do you want them to take any parameters at all?

Well? They're your methods. You wrote them. When you declared them, what did you intend them to do?
He writes this.

1
2
3
4
5
6
7
Distance a;
Double length;

a.setx1(12.0)
a.sety1(20.0)
a.setx2(40.0)
a.sety2(50.0)


Is that correct?

(Code tags added by me).
That looks correct to me. He's declared an object called a, of type Distance. He's then called 4 methods on that class, each one taking a single parameter.

I assume the upper-case "D" in "Double" is a typo on your part, and it's supposed to be a double called length. Or is there another class called
Double
? It doesn't seem to be used anywhere, but possibly that's in some code of his that you haven't shown us.

Last edited on
Alright, I'll break it down how I wrote it and why. And the double is his typo, not mine.

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

class Distance
{
	double set_x1, set_y1, set_x2, set_y2;
public:	
	Distance(double x1, double x2, double y1, double y2) {
		set_x1(12.0);
		set_y1(20.0);
		set_x2(40.0);
		set_y2(50.0);
	}
	double GetDistance() {return sqrt ((pow (double) (x1-x2),2.0) + (pow( (double) (y1-y2),2.0));}

	double get_x1(12.0) { return x1; }
	double get_x2(40.0) { return x2; }
	double get_y1(20.0) { return y1; }
	double get_y2(50.0) { return y2; }
};
	
int main()
{	
	Distance a; {
		double x1, x2, y1, y2;
	}
	double length;

	length = GetDistance();

	cout << "Point 1: x =" << get_x1() << "\t" "y = " << get_y1() << "\n";
	cout << "Point 2: x = " << get_x2() << "\t" "y = " << get_y2() << "\n";
	cout << "Distance is " << length << "\n";

	system("pause");

	return 0;
}


In lines 5-14 is the class: Distance with the variables of x1,x2,y1,y2 as doubles and setting them to their correct values.

Lines 15-21 is the GetDistance formula and the get variables set to return the proper variable when asked

Line 23 the instructor showed using void main (){ but I was corrected here and told to make it int main (){

Line 25-27, I wasn't sure it would even work without having class in front of it but I left it out. It states, according to you that those variables only exist there. It isn't what I expect it to do and thought it would bring those back from above.

Line 28 length is a variable needed for the distance between the points

Line 30 states that length means to get the GetDistance value using the formula in line 15

Line 32-34 are outputs to the user stating the coordinate and the distance between them.

Does this help at all?
In lines 5-14 is the class: Distance with the variables of x1,x2,y1,y2 as doubles and setting them to their correct values.

This needs more explanation. When you say "Distance with the variables of x1,x2,y1,y2", do you mean that the class Distance is supposed to have four data members called x1,x2,y1,y2? Or that the constructor Distance() is supposed to take four parameters called x1,x2,y1,y2?

And since you haven't answered this question the previous two times I've asked it, I'm going to go with the principle of 3rd time lucky: In lines 10 - 13, you're calling some things called set_x1, set_x2, set_x3 and set_x4. What are they? Where are they defined?

Lines 17 - 20, and another ask-3-times question: Are those four get_* methods supposed to be taking a parameter? Is that what the number in parentheses are supposed to be?

Line 25-27, I wasn't sure it would even work without having class in front of it but I left it out. It states, according to you that those variables only exist there. It isn't what I expect it to do and thought it would bring those back from above.
Distance a; is a self-contained statement. It ends with a semicolon, which is how we end statements in C++. As we've already established from your instructor's code that you've posted, it creates an object called a, of type Distance. I don't know why you thought it needed "class" in front of it, because the code you've shown us from your instructor doesn't.

Line 26 doesn't "bring back" anything from above, whatever that means. It just declares four local variables, in the same way as it would anywhere else - for example, in the same way that line 28 declares a local variable. Local variables exist only within the scope that immediately encloses them. By wrapping the declarations within a code block, you've restricted their scope to that block.

At line 30, you need to follow your instructor's example of how to call methods on an object. Ditto for lines 32 - 33.

I don't know how else to answer your question, sir. I put those in the thought that they SET x1 to what is in the parentheses Exactly what I meant when I said
and setting them to their correct values.
. It's painfully obvious that I wont be able to understand any help given due to my inability to get my point across (of my own fault) and the lack of understanding of the help from the knowledgeable community. Forgive me for wasting your time but I do appreciate your assistance on the matter. I just can't get my point across and I can't understand what you ask for. I suppose C++ isn't for me.
I don't know how else to answer your question, sir. I put those in the thought that they SET x1 to what is in the parentheses Exactly what I meant when I said
and setting them to their correct values.

I assume that you're referring to the question I asked in my second paragraph? The thing is - where did you think those things would come from? Did you think that all you had to do was invent the name of a function called "set_x1" and that C++ would magically read your mind and write a function for you that did what you were thinking of when you thought of the name? Because, no, C++ won't do that for you. No programming language will. If you want a function called set_x1, then you have to write it, or else find one that someone else has written and use that. Computers can only follow the instructions that you give them. If you don't give them the instructions for what "set_x1" means, then they can't know what to do with it.

All I can really do is reiterate my point about going back to your books and trying to learn and understand what they're telling you. You seem to lack some very fundamental understanding of the basics of programming and of C++. Us answering specific questions or solving specific problems doesn't seem like it's really going to help you.

Edit: Huh... apparently you can't nest quotes on this forum. Well, I guess I learned something today...
Last edited on
If you were to write a code, to get to the goal I mentioned above. Would those Set_x1 etc. be there?
It depends.

I'm going to assume that x1, x2, y1 and y2 are supposed to be private data members. That's how I'd design a class with this functionality, anyway.

I'd have to think properly about my interface design before making the decision. It looks as though the class simply takes the coordinates of two points and calculates the distance between them, so it might simply be enough for the constructor to take those values when the object is created, rather than supplying access methods to allow the calling code to change the values after creation.

Even if I did decide I wanted them, I wouldn't need to call them from within the class's constructor, because a class is allowed to access its own private data members directly.

But if I did decide I wanted those methods, I'd have to write them myself. I couldn't just call some names I'd invented and expect the compiler to magically create them for me.

(To be honest, I wouldn't write this as a class at all. I'd just write a CalculateDistance function that took the coordinates of the two points and returned the distance between them. But I assume that this is an arbitrary tutorial problem, and you've been instructed to write a class with this behaviour.)
Last edited on
+1 MikeyBoy

This all goes back to what I said at the start:

setx1 is a good name for a function not a variable


@Kariss
Just to reiterate what everyone is saying, again

Variables x1, x2, y1 and y2 are supposed to be private data members.

There should be functions to set the value of these variables:

setx1, setx2, sety1, sety2.

Did you read any of the tutorial on this site? I think you are missing the concept of what a class is, here is a real basic version:

A class stores information (aka properties) and has functions (aka methods) which do things with that info.

There are 3 levels of access - private, public & protected (don't worry about protected for now).

- private means that only class functions have direct access to the variable or function
- public means the variable or function can be accessed through an object

The properties should be stored as private member variables - which means that only class functions (methods) have direct access to them.

The value of private member variables can be set initially with a constructor, which means you might not need a public set function for each member variable.

There is a default constructor which takes no arguments and is typically used to set (initialise) the private member variables to some default values like 0.0

You can also have other constructors which take arguments & use them to set the value of the private member variables

public set functions are used to set the value of the private member variables. These have a return type of void.

Set functions are only needed if you want to change the value of a private member variable after the object has been created.

public get functions are used to retrieve (return ) the value of the private member variables. Make sure the return is correct - in your case it will be double.

Get functions are only needed if you need to obtain the value of a private member variable from outside the class (because class functions (methods) have direct access to them. )

So a bare bones version of your program wouldn't need any get or set functions, because initialisation happens in the constructor & the answer is given with the length function. However this is an assignment to learn how things work - so you should provide them. Just don't get in the habit of thinking you always need to provide both for each member variable.

To call a class public member function, we need to create an object of that class type inside the main() function. This example presumes that you have declared the Distance class & defined it's functions before 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
36
37
38
39
40
41
42
43
44
45
46

int main() {

Distance MyDistance1(); //Object MyDistance1 of type Distance class - calls default constructor function         
                                     //Distance()

//need to call the set functions for this object because they should all be 0.0 as defined in the Distance()
// function

MyDistance1.setx1(10.0);  //notice that setx1 is a function not a variable
MyDistance1.sety1(20.0);
MyDistance1.setx2(30.0);
MyDistance1.sety2(40.0);

double Dist1 = MyDistance1.length; //calculate the length as per the definition of the length() function for the 
                               //MyDistance1 object 


Distance MyDistance2(100.0, 200.0, 300.0, 400.0); // calls constructor which initialises the 
                                                                           //x1, y1, x2, y2  private member variables 
                                                                           // this is the easiest way 
                                                                                             
double Dist2 = MyDistance2.length; //calculate the length as per the definition of the length() function for the
                               //MyDistance2 object 

//can also do this:

std::cout << "Distance 1 is " << MyDistance1.length << std::endl;
std::cout << "Distance 2 is " << MyDistance2.length << std::endl;

//or this:

std::cout << "Distance 1 is " << Dist1 << std::endl;
std::cout << "Distance 2 is " << Dist2 << std::endl;

//now change something

MyDistance2.setx2(350.0);
MyDistance2.sety2(450.0);

//recalc & show anwer
std::cout << "Distance 2 is " << MyDistance2.length << std::endl;


return 0;
}


There are 2 ways to write the length function:

-1 Have it return a double, so that you can assign it to a variable in main() - this is preferable. Note that you can call the length function in a cout statement.
-2 Make the return type void, which means you should cout the answer, otherwise no one will see it.

I hope this has cleared up a lot of issues for you:)

The important thing is to read the tutorial section & Google & wiki are your best friends. Also there is a comprehensive reference section on this site where you can look up all the different functions, and an Articles section - all at the top left of this page.

Cheers
Topic archived. No new replies allowed.
Pages: 123