questions on "classes"

Hello guys,
I've been reading through a few chapters on 'classes'

The prolem is that what I understood is very little, plus, I am reading many threads in here talking about classes from different prospectives, and I must say I am very confused.

Aren't they just meant to be types 'created' by the user? (not provided in lybraries)
just like INT?

Sometimes I see classes without constructors!!?? what does that mean??

Somebody says "they are like blue print for a house" "something you try to stick with. etc"


Are they so useful?

I am larning programming by building (very) simple games
Is there a typical little console game usually chosen for learning classes??


In the end,
What situation litteraly 'force' you to use clases?
What could you not do withou classes?

I am sorry for the question-storm but I am very confused.
int is a primitive data type, it's a built in type define by the language, not in libraries.

A class is a user-define type. The aim of the class is separating details of implementation from the user interface, so you don't have to deal with those unnecessary details.

Sometimes I see classes without constructors!!?? what does that mean??

There are no classes without constructors, if you don't manually define a constructor, the compiler will generate an empty one.

What situation litteraly 'force' you to use clases?
What could you not do withou classes?

I don't think there are situations where you are forced to use classes. OOP(object oriented programming) is just a technique. It can ease the work and make it easier to maintain and change in the future.

Last edited on
Aren't they just meant to be types 'created' by the user? (not provided in lybraries)
just like INT?

You are correct here in one way. But i would not compare a class with an int.

Sometimes I see classes without constructors!!?? what does that mean??

Constructors main purpose is to initialize the class, but sometimes the class does not need an initialisation. For example here is a case where giving the monkey a name is required.

monkey.h
1
2
3
4
5
6
7
8
9
class monkey
{
public:
    monkey(string nameChosed);
    void dance();
    void talk();
private:
    string name;
}


monkey.cpp
1
2
3
4
monkey::monkey(string nameChosed)
{
    name = nameChosed
}


Here is another case where it is not needed to have a constructor

1
2
3
4
5
6
7
class hamsterCage
{
public
    void addHamster();
    void killHamster();
    void destroyCage();
}


as you can see, the class have no variables that needs to be initialized. But sometimes a constructor is required for other things but i should not confuse you with that for now.

What situation litteraly 'force' you to use clases?
What could you not do withou classes?

You can do anything without classes (as far as i know) but it's tend to become spaghetti code, their main purpose is orgainisation. I can give you an example.


Now im gonna give you an example where a class is useful.

Here is a example of code that tends to be spaghetti like without class, it describes racing cars.
1
2
3
float car1Velocity, car2Velocity;
int car1Number, car2Number;
int car1FrictionRate, car2FrictionRate;

I can go on like this forever and maybe i want like 15 cars, then this will already get very very messy.

But now with some class magic
in car.h
1
2
3
4
5
6
class car{
public:
    float velocity;
    int number;
    int frictionRate;
}


in main.cpp
1
2
3
4
5
6
7
8
9
10
#include "car.h"

car car1
car car2 //Now i have already created two cars with the same amount of properties as the ones without classes.

void main()
{
   car1.velocity = 4.5 //now im easily accesing members of the car class
   car2.velocity = 5.5
}



I also want to point out that this is not a perfect example of classes but it was what came to my head. It is very simplified.

Hope this helped, just reply if you want more help

Last edited on
closed account (o3hC5Di1)
Hi there,

The main benefit of Object Oriented Design (that is, designing a program using classes) is that you can model your program towards real life creating separate components and that these components.

In practice, let's consider a game, like pacman.
If you would describe pacman to someone, you could say "pacman is a game where you move around a figure, which eats up dots, fruits and keeps away from ghosts".

In object oriented design, we would analyse that as the following:

- Figure pacman which moves and eats things
- Eaten things: dots, fruits, ghosts
(note that for clarity I'm leaving out the actual game grid)

We can then transfer this into classes:


Class PacMan:
   move();
   eat(Thing);

Class Thing:
    score;
    lives;

Class Dot is a Thing:
    score +1;
    lives remain the same;

Class Fruit is a Thing:
    score +10;
    lives +1;

Class Ghost is a Thing:
    score -10;
    lives -1;


Keep in mind that this is just a rough example.
The benefit is that when you would like to add a "thing", it's easy to do, when you want to change the score added by eating a dot, it's easy to do. That is because every class forms a seperate entity, with a limited interface to the outside. Think of it as going to the cash register of a store. You pay the person at the register, but don't care what happens after that, things like increasing the daily income of the store, updating the stock, etc., you simply use the interface the cash register provides you. Similarly, classes cooperate with one and other through the interface they present to one and other, which means that the details of implementation are hidden and known only to a class itself.

It's also worth mentioning that C++ actually uses a lot of classes in it's standard library. Take std::string for instance. That is actually a class, and every std::string you create is an object of that class. So you can't really go about using C++ not using classes (unless you confine yourself to the C-subset, in which case, just learn C).

If you don't see a constructor defined for a class, the default constructor will just be called.
The default constructor is code generated by the compiler, but which doesn't really alter anything to the object you're creating.

So to conclude, the use of classes may benefit the clearness of your design, the maintainability of your code and make your code more reusable (you can reuse classes you created before easily).
I say may, because it depends on how you use it, it could also clutter your code into a mess if you don't know what you're doing.

If you want a good read on this stuff, have a look at Robert Lafore's Object Oriented programming using C++.

Hope that helps.

All the best,
NwN

Edit: I'm so slow... sorry!

Last edited on
Guys, thnkyou so much for your replies.
I feel now like I learnt more reading your piece of writing than I did reading a C++ book for a whole week.

I really understand what you said and I am now going to put that into practise now.

I'll write again if I will get stuck or if I will struggle working on classes
Thank you again for your posts.
really appreciated
If you want to put it into a larger professional point of view as to why you might want to use classes imagine a game or something such as minecraft...

I've seen the java source code for minecraft and i can tell you it's made rather well with the manipulation of classes... If you've ever played minecraft you'll know that every time you create a new game the whole world is randomly generated and the terain is made up of blocks of materials...

These blocks are all instances of a class, for example there's only 1 piece of code for how dirt or stone might act when blown up, walked on, mined, punched, placed, etc.... However there are thousands of blocks of dirt and stone in the game, no-one could possibly ever code all this in so we make classes...

Once you've set up properties for one material all you need to do is tell the world how to load it in generation and you have thousands of copies of the same block running in game, making the coding more efficient, more user/coder friendly and more importantly able to be used well by world generation to make loads of wonderful different types of maps rather than just a select few fixed maps.

P.S. Good luck in coding
OMG,

Now it's soooooooooo clear..
niceeeeeee

wait..

just to make sure!!!!

so, CLASS is a generalisation of TYPE??

SO,
in a videogame (I love games, you hit it on the nose with a hammer)
when you "create" classes, you are "God" and define how things will look and behave like in your world, afterwards there will be an "architect" that will place these things in the world (creation of OBJECTs, elements of the class) like in a factory

so basically I wouldn't pay 5pence for a BIC if each BIC was handmade right?
Do you think I got t?
SO,
in a videogame (I love games, you hit it on the nose with a hammer)
when you "create" classes, you are "God" and define how things will look and behave like in your world, afterwards there will be an "architect" that will place these things in the world (creation of OBJECTs, elements of the class) like in a factory

Basicly, yes.

The "God" creates the classes and the architect places objects (instances of classes)

You need to think of classes like real life objects, and think of what they can do. Then implement the code. For example this is what comes to my mind when i think of a dog.

1
2
3
4
5
6
7
8
9
10
11
12
13
class dog{
public:
    void giveName(string dogName);
    void feed();
    void playWith();
    void kill();
    void move();
private:
    string name;
    int energy;
    bool alive;
    int hunger;
}


it is very important to think of whats public and whats private. for example if i would put the "string name" in the public section then i't could be changed in bad ways which the giveName() method can prevent, like this.

1
2
3
4
5
6
7
void dog::giveName(string dogName){
    if(name contains bad characters that we dont want in the dogs name){
         return;
    }else{
         name = dogName
    }
}


one more thing. You should learn inheritance before you try to make a game or something with classes. It makes stuff easier.

Best luck.
Last edited on
Thank you so much.
i ll make sure i find out what inheritance is before i get myself to make an actual game.
thank you for your clarification.
I actually feel like understanding this much better now. Still, I need a lot of practise but that comes with time I guess

Again, thank you

Cla
Hey i know the questions answered but in your first post you asked if there's any console games that could use classes... Although i cant think of any games, my C++ book uses a game lobby program to demonstrate classes.

It creates a Player class (which only contained a name but you could add health and/or hunger, and other life traits) and a Lobby class to store pointers the the players and to execute commands on players such as:

AddPlayer(string name),
RemovePlayer(Player* pPlayer),
ClearLobby()


^^^(you can create pointers to objects you've made, and also store an array or vector of these object pointers)



It shouldn't be too complicated, Good luck!
Last edited on
Topic archived. No new replies allowed.