class and objects general question

so i figured out how to make a class and object of that class, but when i try to make a version of my own...all i think is that it seems easier to just use regular functions (global functions) to get the same job done.

Why use classes if it just takes so much time to make it all when it seems so much faster to just make a function branch of main???

FOR EXAMPLE...this code i could make so much less code and easier to understand by getting rid of the class and using global functions
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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

class Tricycle
{
public:
    int getSpeed();
    void setSpeed(int speed);
    void pedal();
    void brake();
private:
    int speed;
};

// get the trike's speed
int Tricycle::getSpeed()
{
    return speed;
}

// set the trike's speed
void Tricycle::setSpeed(int newSpeed)
{
    if (newSpeed >= 0)
    {
        speed = newSpeed;
    }
}

// pedal the trike
void Tricycle::pedal()
{
    setSpeed(speed + 1);
    std::cout << "\nPedaling; tricycle speed " << speed << " mph\n";
}

// apply the brake on the trike
void Tricycle::brake()
{
    setSpeed(speed - 1);
    std::cout << "\nBraking; tricycle speed " << speed << " mph\n";
}

// create a trike and ride it
int main()
{
    Tricycle wichita;
    wichita.setSpeed(0);
    wichita.pedal();
    wichita.pedal();
    wichita.pedal();
    wichita.pedal();
    wichita.pedal();
    wichita.pedal();
    wichita.pedal();
    wichita.brake();
    wichita.brake();
    wichita.brake();
    return 0;
}
Your running into an issue of "I don't need this right now, so it can't be useful".

I happen to think wichita.setSpeed(0); can't get anymore clear. I also notice you are using std::cout, which is a class. It is handling everything in the background from buffers to error checking, and that is the strength of class. Once you define the class properly, you can elegantly use it anywhere, and it will do exactly what it needs to do. And, just as important, when your code becomes giant, if you need to make any changes to the way a class functions, you only need to change that single class, and everywhere it is used will also be updated.
Is Tricycle::setSpeed() good from an o-o point of view? I'm a but suspicious of that particular method...

If you're modelling a bike, I don't think you'd have a setSpeed(). You would have a getSpeed, but you only get to a speed by pedalling. Or maybe by coasting down hill??

Andy

P.S. Can a push bike pedal itself?
I suppose when the bike hits a wall, it sets its own speed to 0, or while at a stand still, get's hit by a car, would set it's speed to some high number.
I use to think the same but when I make a the class I get a feeling of clarity and my code seems so much easier to understand. But for me to get to that AH-HA moment I had to make a useful project. Try making a class that you will use. I made a class did a lot of annoying parts of code for me. Try making something like and you get the AH-HA moment. Hope this helps :)
Last edited on
TheMassiveChipmunk
what are some suggestions of projects thats is a necessity for a class. I'm trying to find someway to find that AH-HA moment???

Also, when you do small coding, should you use class as much as you can?
Last edited on
First of all I am also new so I might not be the right person to ask so if I make a mistake please tell me about it.
Now to the problem:
Try making a class that handles allocation and de-allocation.
Or make a class that generates a number from 1 to a User Picked Number For Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
class Generator 
{
	int Max; //Maximum value a random number can reach
public:
	Generator (int M)	//When class objects are initialized they have to have a limit value
	{
		Max = M;
	}
	int Generate ()	//Generates the number
	{
		int Random = 0;
		Random = rand () % Max + 1;
		return Random;
	}
};


Try something making something fun or useful.
I am sorry I could not explain any further I suck at explanations sorry.
But most importantly Have fun :)
Last edited on
Topic archived. No new replies allowed.