Very Lost on Classes

From this point on in our course over the next several weeks we are going to be concentrating on classes--
I am finding it INCREDIBLY difficult to make sense of it :(

Our first assignment is as follows

We are to make a program that implements class files so that we have the output

Havingone experienced runner, and one light weight runner as two separate methods


Sonia O’Sullivan walked 0 miles, ran 500 miles and jogged 0 miles

Sonia O’Sullivan walked 0 miles, ran 1000 miles and jogged 0 miles

Sonia O’Sullivan walked 0 miles, ran 500 miles and jogged 0 miles

You walked 10 miles, ran 0 miles and jogged 10 miles




So far my Jogger.h file is as follows

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
#ifndef JOGGER_H
#define JOGGER_H
#include <iostream>
#include <cstdlib>

using namespace std;

class Jogger {
public:
	Jogger(string name);

	void walk(int amount);
	void run(int amount);
	void jog(int amoount);

	void   displayStats()
	void   clearStats();

	string name;
	int walkedSoFar;
	int ranSoFar;
	int joggedSoFar;
};

#endif 



And my .cpp file is as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <string>

#include "Jogger.h"

using namespace std;

int main( )
{
Jogger marathoner( "Sonia O’Sullivan" );
marathoner.run( 500 );
marathoner.displayStats( );
marathoner.run( 500 );
marathoner.displayStats( );
marathoner.clearStats( );
marathoner.run( 500 );
marathoner.displayStats( );

Jogger liteWeight( "You" );
liteWeight.walk( 10 );
liteWeight.jog( 10 );
liteWeight.displayStats( );
}



I was able to get the SKELETON put together but now I've come to a sudden brick wall and am starting to pull out my hair trying to wrap my head around it.
I haven't been able to find a decent example to help me guide my way into understanding it

Any push in the right direction would be a TREMENDOUS relief


Thank you.
If this is your first time using classes, your work doesn't seem all that bad. I'm not sure exactly what kind of trouble you're having so I'll just give a bit of generic advice about classes.

Use them to represent "things" you want to work with; in this case, a "jogger" is one of those things, so you created a class to represent it. The members (variables inside the class) represent information or otherwise internal data about the class. In this case, the amount of miles ran/walked/c. were that kind of information. Methods (functions in the class) should be things you'll want to ask the class to do for you with its data, such as printing it.

It seems like you have the idea right, so what exactly aren't you sure about?
Well, My main problem that I am having trouble with is the other .cpp file that goes with a class file that sort of "directs" the classes in the right way,

I am looking at other examples of this and can't quite seem to figure out how to even get started


For Example, How do I tell the program to recognize a jogger as "marathoner" and then print the walkedSoFar, or ranSoFar, or joggedSoFar as values.


That second .cpp file is what is really stumping me. For some reason, it just makes no sense.
Again,
I was able to put together what I have already by sort of 'cherry picking' similar examples from similar programs-- which is how I learn C++ best-- through example..

Our instructor likes a more independent study approach and on this particular topic, there isn't any example I can find that makes completing this program any easier...
Last edited on
To put it another way, I have 'X' and I have 'Z'

But I just can't figure out how to get 'Y'
Basically the .cpp file for the class is just a definition of what the methods of the class do. I assume you already know how to define functions, so it's really just a similar process to that.

Try giving the tutorial here a quick read and see if it helps at all.
http://www.cplusplus.com/doc/tutorial/classes/
Okay...
I think that helped a little bit

I think the thing that is stalling me a little bit that I keep looking at are specifically how to implement "displayStats()" and how to assign the string name as marathoner or liteWeight

This is what I came up with for the definition .cpp

Am I getting Close?

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
#include <iostream>
#include <cstdlib>
#include <string>

#include "Jogger.h"

using namespace std;


Jogger::Jogger() {
	walkedSoFar  = 0;
	ranSoFar = 0;
	joggedSoFar = 0;
}

void Jogger::walk(int amount) {
}

void Jogger::run(int amount) {
}

void Jogger::jog(int amount) {
}

void Jogger::displayStats(int amount) {
}

void Jogger::clearStats(int amount) {
}

int Jogger::name() {
	string marathoner;
	string liteWeight;
}

And somehow run (int amount) [as well as walk and jog]
somehow need to speak with walkedsofar/ransofar/joggedsofar

confused on this as well.
I've since updated this as such

I am still not quite sure how to implement displayStatus()

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 <cstdlib>
#include <string>

#include "Jogger.h"

using namespace std;


Jogger::Jogger() {
	walkedSoFar  = 0;
	ranSoFar = 0;
	joggedSoFar = 0;
}

Jogger::Jogger (string name)
{
	name = marathoner;
	name = liteWeight;
}

void Jogger::walk(int amount) {
	walkedSoFar = amount;
}

void Jogger::run(int amount) {
	ranSoFar = amount;
}

void Jogger::jog(int amount) {
	joggedSoFar = amount;
}

void Jogger::displayStats(int amount) {
}

void Jogger::clearStats(int amount) {
}
Last edited on
¿do you know what this is?
¿why is `displayStats()' taking a parameter?

> how to assign the string name as marathoner or liteWeight
I have no idea what you meant
Topic archived. No new replies allowed.