How to use classes and virtual functions to make a player/enemy Class

Hello I am very new to programming and I am currently trying to torture myself to get better at coding and C++ classes for my project. My plan for this class is to make a class called Character. Character will have everything that any enemy or player has access to. It will have access to all attack moves and defensive moves, health, stats, ect. Then I will use a virtual function to to connect that to a playerCharacter class. The playerCharacter has most of the moves but there is level requirements. Though that part I dont want to worry about right now. then the is the enemyCharacter, I already made a very basic AI for it and I need to plug in my AI code to work with the enemyCharacter.

That is my plan. but the two questions I have is...
Is that a good plan?
And how would I go about it?
I am very bad with classes. And I will post most of my code once i get to a coding computer.
My plan for this class is to make a class called Character. Character will have everything ...
This is the right approach. In Object Oriented Programming, a Class is an Object, and should do everything you want that object to do.

It does this using Methods. Methods are messages that the Class accepts and will make it do stuff. That's the only way to communicate with and object, by sending it messages.

In C++, methods are implemented as virtual functions. C++ is a hybrid language, so has lots of concepts, it's difficult to disentangle them by starting with the language. It's kinda like trying to learn English by reading Shakespare.

Then I will use a virtual function to to connect that to a playerCharacter class.
So you should now realize that this isn't quite right.

Every communication with Character must be done thru a virtual function. In doing so, you can then specialize Character to say, have different abilities.

The archetypal example is handling shapes. You can do stuff with a shape, create one, draw one, colour it in, save it, ... and these operations can be done with different kinds of shapes. It's the same with you Character class.
For the sake of accuracy:

In Object Oriented Programming, a Class is an Object

No. A class is a type. An object is an instance of that type.

1
2
3
4
5
6
7
8
9
10
11
// MyClass is the class.  It is a type
class MyClass
{
  // Stuff...
};

// These are several objects. They all have the same type - MyClass
MyClass my_object_1;
MyClass my_object_2;
MyClass my_object_3;
MyClass my_object_4;


This is not pedantry; it is important to understand the difference.

In C++, methods are implemented as virtual functions.

Not necessarily. You only make them virtual if you want runtime polymorphism, which is often not the case.

Every communication with Character must be done thru a virtual function.

See above. It's perfectly possible to use non-virtual methods in a class's interface.
Topic archived. No new replies allowed.