a question about classes

In all the practice exercises I do involving classes all I am doing is calling classes to call functions or variables...but it is a little more confusing than just calling functions outright. It is like I am calling a class just to call a function! Why is there a need for classes then? Are they used just for organization...or something else?
Last edited on
closed account (17pz3TCk)
You are right, classes are for organization.

They make for better programming practices and readability. Think of a class representing a student. The class could hold the information for the student's name, address, current grades, etc. Along with storing information, you could create functions to call and change the information as needed. Since all the information would pertains to a student, all the functions regarding the student's information are placed in a class to keep them away from other parts of the program.

Probably not a good explanation, but I hope it helps clarify the point of classes a little :)
Thanks Parable4. Any chance you can figure out what's wrong with this little example. I get an error at line 23 stating:
undefined reference to 'example::func(int)'


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>//messing around w/classes basics
#include <string>
using std::cout;
using std::string;
using std::endl;
using std::cin;

class example
{
        public:
            int func(int x);
        private:
            int i;
};
        example myExampleClass;

int main()
{
    int b;

    cout <<"Enter one for letters, two for numbers: ";
    cin>>b;
    myExampleClass.func(b);

    return 0;
}
//end main


void func(int x)
{
    switch(x)
    {
        case 1: cout<<"You got letters";
            break;
        case 2: cout<<"123456789";
            break;
    }
}
2 things. You need to use scope operator to tell c++ that the func belongs to the example class like this

example::func(int x)

Also you need to have the same form as you told c++ in the class.

You told that it would return a int by saying.
int func(int x)
But you later wrote.
void func(int x)

They must be the same

Wish you best luck
Thanks I FINALLY understand
Also i want to point out that classes are not ONLY for organization.

They are also useful in a moment like this.

lets say you have 4 "units" in your game. They all have a position and a velocity. So without classes you would do something like this.

int P1X, P1Y, P1Z, P1Xvel, P1Yvel
int P2X, P2Y, P1Z; etc.. etc..

Also you would need a function to update coordinates at each frame.

void movePlayer(int xPos, int Ypos, int Xvel, int Yvel)
{
//move the player
}

but with classes you could instead do something like this.

class unit
{
public:
void move();
private:
int X,Y,Z, Xvel, Yvel;
};

And then just declare some units classes

unit P1, P2, P3, P4;

And voila. Much easier right? Also this would give The the positions and velocity privacy so you wont acces them by mistake.

Hope this helped! wish you best luck!
Topic archived. No new replies allowed.