+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