When to use classes?

Hey everyone, I have been learning c++ for around 1 year, but i am still not very good. I programming in an object oriented manner, but would just like some advice.

If i was to make a console RPG where there were different character you could be(wizard, warrior etc.) , when and where would i use a class?

And if I am making a Contact manager to add, delete and save contacts and their name, what kind of OOP related coding should i have. And by 'should' i mean what is the most neccarsary and effeciant way to code and make the programm.

THankyou verymuch :)
OP wrote:
And if I am making a Contact manager to add, delete and save contacts and their name, what kind of OOP related coding should i have.


Maybe you could make a class with functions(of cause virtual for OOP) and the "database" then a derived class would make its implementation of these functions.....of that sought.
Oh i see, thanks man.
One thing, I am just wondering what implementation means in that context ("a derived class would make its implementation of these functions")?

Well, basically i am wondering what implementation means in OOP in general. thanks:)
Resource: http://uk.answers.yahoo.com/question/index?qid=20081019073402AAdZHTc

As a general term, implementation is the actual code that has been created based on a hypothetical design, so to implement a program is to actually program it.

In object-oriented programming, two terms are "interface" and "implementation." The interface is akin to the blueprint of an object or a system. It shows the methods, variables, documentation about a class. The implementation is how it actually works (e.g., the workflow, code, etc.)

Here's a very short, elementary example:

1
2
3
4
5
6
7
8
9
10
11
//Car interface in file car.h
class Car
{
public:
Car()
~Car()
Accelerate()
Decelerate()
private:
int speed;
};


This is the interface. It's what programmers will see when they use the car class.

Now, the IMPLEMENTATION (which actually fleshes out the methods)

1
2
3
4
5
6
7
8
9
10
11
//in car.cpp

void Car::Accelerate()
{
speed++;
}

void Car::Decelerate()
{
speed--;
}


Oh great, thankyou, Aceix. THanks for replying, no one usually replies to my threads anymore, i dont know why. I made one juts before, which got no replies.

Anyway just to clarify, does the implementation simply mean the member functions of a class and how they operate and use the class?

So when you said: "then a derived class would make its implementation", you meant a derived class that would use the functions of the other class?

And what is a dervied class? thanks man
Last edited on
What I know is, implementation/definition" of a virtual function of a base class, is to give a version of that function.

a base class, is a class that other classes inherit from.
and a derived class is a class, which inherits from a base class.

Note: private methods(functions) or variables cannot be inherited, but can be accessed, by friend classes and functions.

Sounds confusing, look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Polygon  //which is a base class.
{
     public:
      virtual int area(int,int){};    //a virtual function the reason for the {} is to give some implementation or else you'll get some 'vtable' error.
// Note this applies to virtual methods and not pure virtual ones.
};

class square:public Polygon{
     int area(int a, int b){return a*b;}   //its version of the virtual function.
//Note. parameters and return type, and also the name of the function, must be the same!
};

class triangle:public Polygon{
     int area(int a,int b){return (a*b/2);} //its version too
}


For more info: http://cplusplus.com/doc/tutorial/inheritance/
and http://cplusplus.com/doc/tutorial/polymorphism/
Last edited on
Oh thanks alot man. So, if it were an rpg, would i make a base class for 'Player' and have all of the different type of players(wizard, warrior etc.) as child classes coming off the base class ?

Is it common to do that all of the time in these scenarios? Like If i had a car, then the different types of car would be coming off a base class?

THanks again man:)
OP wrote:
Is it common to do that all of the time in these scenarios? Like If i had a car, then the different types of car would be coming off a base class?


Yeah like this. All cars have mass(since it is matter) and a type(SUV,convertible,...). So you can have a base class with weight(); function and type(); then a derived class say Lamborghini Murcielago would implement it...that sort since now you understand........

Thanks for the replies. They make me active in forums!
Last edited on
Thanks alot man, you've been a great help. Thanks for replying. I'll just go learn and practice everything you said :) bye
Topic archived. No new replies allowed.