Help with classes!

Pages: 123
So is the class that isn't in main not needed? I'm honestly confused.
In lines 5 - 21, you are defining a class. Basically, a class is a type. Like int, or float, or bool.

However, you still need to create objects of that type for your code to actually work with, just like you create an int variable if you need to use an int. This seems to be what you're trying to do in line 25.
Can you explain how to create the object, or give me an example. I don't know where it should go. Like I said before, the textbook I have doesn't show you how to use or create one, it just says there is one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

struct myClass //struct functions similarly to class. The only difference is that class has private, public and protected.
{
    void saySomething() //Member function of class myClass.
    {
        std::cout << "Something said" << std::endl;
    }
};

int main()
{

    myClass objectOf_myClass; //Creating object of myClass named objectOf_myClass. It is similar to declaring int x;
                              //int is an integer type, myClass is a class type (named myClass). x is a variable(aka object) 
                              //of type int, objectOf_myClass is an object of type class.

    objectOf_myClass.saySomething(); //objectOf_myClass is calling member function saySomething.

}
Last edited on
Can you explain how to create the object, or give me an example.

As I said:
just like you create an int variable if you need to use an int

And I find it hard to believe that in a C++ book, there's not one example of an object being created anywhere.
Last edited on
It doesn't say when it's created or what to do to create it. It just shows that it's there. It doesn't even describe it as an object. It just shows:
1
2
3
4
5
class P_example {
int num;

int main () {
p_example ob[2], *P;


that's why I tried to put an 'a' in front of distance but I was told that it doesn't need to be anywhere in the code. It never says that the "ob[2], P* is an object, it is just put there.
Is my code on the previous page completely wrong in any way? I feel like I may have to just start from scratch again. I fear I may have just boggled it all up and it's too wrong to fix at this point.
Last edited on
That example isn't very good, as it shows the creation of an array of objects rather than a single object. That line 5 is creating an array of objects of type p_example, and a pointer to an object of type p_example. Actually, if you've copied it correctly, then it's utter nonsense, as it won't compile. Get a new book.

Consider the following:

1
2
3
int myInt;
float myFloat;
p_example ob;


This creates a variable of type int, called myInt, a variable of type float, called myFloat, and an object of type p_example called ob.

that's why I tried to put an 'a' in front of distance but I was told that it doesn't need to be anywhere in the code.

No you weren't. You were told it shouldn't be in the place you were putting it - i.e. inserted into the class definition.
Last edited on
Okay. I'm having trouble getting all this straight in my mind. So this would be correct?

1
2
3
4
5
6
int main()
{	
	Distance a; {
		double x1, x2, y1, y2;
	}
	double length;
Last edited on
Well, it's legal C++, but I'm not sure what it's supposed to be doing.

What are x1, x2, y1 and y2 supposed to be? They weren't in any previous version of your code. Why are you enclosing their declarations in their own block, and not actually doing anything with them?
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;
}


Where I declared the class above main. x1, y1, x2, y2 are the variables I need to find the distance between them as well as have the program display their respective values: x1=12.0 - y1=20.0 and x2=40.0 - y2=50.0.

I just can't figure out if it's just mixed up. Or lines are specifically wrong. I'm having trouble applying what you're saying to my code. It seems like every time I try, it's wrong.
Last edited on
In your constructor for the Distance class, x1, x2, y1, y2 are the names of parameters that the constructor takes. However, in the definition of your constructor (lines 9 - 14), you're not actually doing anything with those parameters.

In line 26, you're declaring some local variables called x1, x2, y1, y2, which exist only within the scope of the code block that starts at the end of line 25 and ends on line 27.

I've just noticed that in all your definitions for your class methods, on lines 17 - 20, you have numbers in parentheses, instead of parameter definitions. That's not legal. Those lines aren't method calls, they're method definitions. You need to put the parameter definitions in there.
I thought that the lines 9-14 were setting the x and y variables to their proper value?

So lose the {} in line 25 and 27?

Remove the numbers from the parentheses.

Right? I just want to get this correct and figure out how it works. I hate not understanding what went wrong and having to turn in something unfinished.

I thought that the lines 9-14 were setting the x and y variables to their proper value?

Lines 9 - 14 are defining a constructor for your class. The way you've defined your constructor, it's calling 4 other functions/methods. But now that I look at it, I've noticed that I don't see the definitions for those functions. Where have you defined set_x1, set_x2, set_y1 and set_y2? Are they supposed to be methods belonging to Distance? Global functions? Methods on another class?

So lose the {} in line 25 and 27?

Well, that would mean that those four local variables will be in scope for the entire main function. Is that what you want?
Remove the numbers from the parentheses.

Declare the parameters that you want those methods to take in the parentheses. What are those parameters? Do you want them to take any parameters at all?
Right? I just want to get this correct and figure out how it works. I hate not understanding what went wrong and having to turn in something unfinished.

Well, to be honest, it seems to me that a book - a good one - is going to help you more than me answering questions. I don't mean to be rude, but right now, it looks as though you're just typing random things that look a bit like C++ without knowing what they mean, in the hope that you somehow magically stumble upon a combination of numbers and letters that works. And, believe me, it's very unlikely that's going to happen.

You need to go back to some tutorial sources, and learn about classes. Learn how to define them. Learn what a method is, and how to define one. Learn what a data member is, and how to define one. Learn how to instantiate an object of a given class. Learn how to call methods, how to pass parameters into them, and how to return values from them.

Once you actually understand what you're writing, you'll be able to turn in some finished work.
I'm just going off the examples my instructor gave me. I'm trying to take into consideration what everyone here says and what he's given me. I'm trying to learn it, but it just doesn't make sense to me.
If you don't understand those examples, then trying to copy them isn't going to do you any good.

Seriously, you're never going to get anywhere with this if you don't go back to whatever tutorial sources you're using and learn what this stuff means.

Oh, and you seem to have ignored all of the questions I've asked. I'm not typing them for my own pleasure - I'm trying to find out what you're trying to do so I can help you. If you're not going to bother paying attention to what I'm writing, then what's the point of this thread?
Last edited on
I've told you what I'm trying to do. I've read the tutorials on it but I've having trouble applying it all to my code. And some of it contradicts the examples from my instructor. I understand that you don't type this for your own pleasure and I thank you for your input so far! C++ is EXTREMELY confusing to me and I've only been doing it for 2 or 3 months off and on. I can't immerse myself in it as much as I'd like to due to other issues. Partly because it is so difficult for me to comprehend. It's just that everything people say when I ask for help requires me to be 100% knowledgeable in what I'm doing. If I was, I wouldn't need to ask for help.
Fine. But you're still ignoring the questions I've been asking you. Why should I bother trying to help, if you're not prepared to play your part?
Kariss wrote:
And some of it contradicts the examples from my instructor.
Could you post your instructors examples and then the code you think contradicts that? A possibility is that your instructor has not stayed up to date with the latest in C++ - there are a lot of professors out there teaching ancient coding practices that are now considered deprecated and non-standard.
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.

I just need the program to have a class that stores Distances (x1, x2, y1, y2) in the private section with public member functions to set the values with a member function (the GetDistance in my code) that will give the distance between the points. <This is what I'm trying to do.

To LB: He writes this.

Distance a;
Double length;

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

Is that correct?
Pages: 123