Help with virtual

Ok well this went from learning pointers to learning inline, virtual and some polymorphism lol. Thats fine with me though. Anyways From what i understand virtual basically makes a copy of a function and replaces data in it??

here is my code im trying to do that very thing, i have a character class and i want to use virtual to copy the basic damage function in it to use for enemy class

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "_Header.h"
 
using namespace std;
 
int main()
{
    int TMS_Player_Health_Init = 0; //Health Initializer
    int TMS_Player_Ammo_Init = 0; //Ammo Initializer
    int TMS_Enemy_Health_Init = 0;
    int TMS_Enemy_Ammo_Init = 0;
 
    Character C;
    Character *C2 = new TMS_PVars();
    C2->BasicDamage();
 
    C.TMS_PVars(&TMS_Player_Health_Init, &TMS_Player_Ammo_Init);
    C.TMS_EVars(&TMS_Enemy_Health_Init, &TMS_Enemy_Ammo_Init);
 
    cout << "Player Health equals " << TMS_Player_Health_Init << " " << " Player Ammo equals " << TMS_Player_Ammo_Init << endl;
    cout << "Enemy Health equals " << TMS_Enemy_Health_Init << " " << " Enemy Ammo equals " << TMS_Enemy_Ammo_Init << endl;
}
 



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
60
61
62
63
64
65
_Header.h

[code=cpp] 
#ifndef _HEADER_H_INCLUDED
#define _HEADER_H_INCLUDED
 
class Character
{
    public:
        Character();
        ~Character();
 
        inline void TMS_PVars(int *TMS_Player_Health, int *TMS_Player_Ammo) //Player Variables
        {
            *TMS_Player_Health = 100;
            *TMS_Player_Ammo = 17;
        }
        virtual int BasicDamage()
        {
            int BaseDamage = 10;
        }
 
 
    private:
        int BaseDamage;
};
 
Character::Character()
{
 
}
 
Character::~Character()
{
 
}
 
 
 
class Enemy
{
    public:
        Enemy();
        ~Enemy();
        inline void TMS_EVars(int *TMS_Enemy_Health, int *TMS_Enemy_Ammo) //Enemy Variables
        {
            *TMS_Enemy_Health = 400;
            *TMS_Enemy_Ammo = 80;
        }
 
    private:
};
 
Enemy::Enemy()
{
 
}
 
Enemy::~Enemy()
{
 
}
 
 
#endif // _HEADER_H_INCLUDED 


As you can probably see i dont know what im doing, well i sorta do im just confused a bit but if you can correct my code and i see how it works ill understand it. I understand pointers better and i now get inline and virtual(at least i hope i understand virtual)

errors

C:\Users\Chay\Desktop\Pointer Practice\_Header.h||In member function 'virtual int Character::BasicDamage()':|
C:\Users\Chay\Desktop\Pointer Practice\_Header.h|17|warning: unused variable 'BaseDamage'|
C:\Users\Chay\Desktop\Pointer Practice\_Header.h|18|warning: no return statement in function returning non-void|
C:\Users\Chay\Desktop\Pointer Practice\main.cpp||In function 'int main()':|
C:\Users\Chay\Desktop\Pointer Practice\main.cpp|14|error: expected type-specifier before 'TMS_EVars'|
C:\Users\Chay\Desktop\Pointer Practice\main.cpp|14|error: cannot convert 'int*' to 'Character*' in initialization|
C:\Users\Chay\Desktop\Pointer Practice\main.cpp|14|error: expected ',' or ';' before 'TMS_EVars'|
C:\Users\Chay\Desktop\Pointer Practice\main.cpp|18|error: 'class Character' has no member named 'TMS_EVars'|
||=== Build finished: 4 errors, 2 warnings ===|
Last edited on
Take a look at this.
http://www.parashift.com/c++-faq-lite/virtual-functions.html

You haven't posted enough code for me to comprehend what you are trying to do. What is the TMS_PVars type? You didn't provide the definition.

Virtual functions are used to define interfaces that can be inherited by derived classes. The objects will have a virtual function table so that the correct function is called at runtime when it is overriden by a derived class. So you'd probably want to have Enemy inherit from Character.

class Enemy : public Character
Oh and the following function will simply instantiate an integer with a value, and then destroy it. Calling the function will have no useful effect.

1
2
3
4
virtual int BasicDamage()
        {
            int BaseDamage = 10; // get rid of the int if you want to set the class attribute
        }


Oh, I see where you are going wrong. You need to learn some basic C++ before you get into inheritance.

1
2
3
4
5
6
// TMS_PVars() is a function of the Character class.  new is only to be used with the class type,
// in order to construct the object from a class type
Character *C2 = new TMS_PVars();

// if enemy properly inherits from character, then you can do this
Character *C2 = new Enemy;
Just so people know, I am hoping that the OP is aiming at implementing the suggestions I made in this post. There are also other threads on the same subject. I wish people would not do this - I would rather 10 pages in one thread than a new thread every 2 days.

http://www.cplusplus.com/forum/beginner/91131/#msg490263


@Ch1156

OK, here is the class layout I was envisaging (Note the leading C on the class names ):

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
class CMainApp {}; //Generic for loading / saving files, menus etc
class CGamePlay {}; //Generic controls the playing of the game
class CShop {}; //Buy CResources such as CAmmo, CMedipacks etc

class CBAckpack {}; //To keep CResources in - the Player has one of these as a member

class CResource {}; //Generic The backpack has Resorurces as members
class CMoney  : public CResource {};
class CAmmo : public CResource {};
class CMedipack : public CResource {};
class CWeapon : public CResource {};//Generic 

class CRifle : public CWeapon {};
class CShotgun : public CWeapon {};
class CPistol  : public CWeapon {};

class CActor {}; //Generic You have Character which is OK
class CPlayer : public CActor {};//Generic 
class CEnemy : public CActor {};//Generic 

class CHero : public CPlayer {}; //optional

class CDinosaur : public CEnemy {};//Generic 
class CTrex : public CDinosaur {};
class CRaptor :  public CDinosaur {}; 



The classes with Generic in the comment means that you shouldn't be to create objects from these. One way to achieve this is to make their constructors protected. An attempt to create an object of this type result in a compile error. The other way is if the class has pure virtual functions - an interface class say.

Now, can I get you to believe in the idea that each class has it's own .h & .cpp files? And not to put code in header files? Don't combine 2 headers together. As I said several times you should be able to get your IDE to do this automatically with the class wizard. Well mine does (KDevelop on Linux) anyway - I am hoping the others do too.

The reason header files don't have code in them is because you need to include them in whatever .cpp needs to use that type of object. Having code in the header just makes a huge mess if the file is included everywhere. The header file declares the class and it's functions once, but could be included many times into other files - that is why you have header guards and /or #pragma once . The code for the class functions is written once and once only in a .cpp file, and never ever included in anything. You can have forward declarations of classes, to get around circular dependencies.

The other thing is that naming of variables is important. For example, the CActor class can have a protected variable m_Health (member variables start with m_), which inherited by everything underneath it. This is much better than having EnemyHealth, PlayerHealth. One of the main ideas is to specify things once only, and write code once. If you see things being being repeated, then you need to alter the design - push things higher up the tree, create new classes if you need to. Keeping names general helps with this.

I am using some naming conventions - CMyClass, m_MemberVariable & CamelCaseForNames, these are not my particular conventions and IMO there are good reasons for using them. Some people have other conventions - I am just showing what I do.

I know this is all a big learning curve for you, but stick with it - I think it is great when one learns lots of new things. And I think there will a great sense of pride when you get it finished and know that it has been done very well compared to your original attempt. Ha - now you have lots to go on with. Once you have made all the classes, we can start at the highest level of class, to put in member variables and functions.

Above all - have fun !!!

Topic archived. No new replies allowed.