Hi, I have been learning a lot of different c++ lately and trying to make a small program to test out different stuff. But I am facing an issue with a virtual string function that I can not figure out. I have a class called MrPring that inherits from entity, this is working fine until i try and print out stateAbility(). Rather than it returning the string that I passed in when the object is created, it is returning an empty string. The rest of the functions are working correctly, but they are inherited from the base class, but not a virtual function like stateAbility(), so I am assuming I am going wrong somewhere involving the virtual function, but do not understand how. I have pasted my code below if anyone could please help me out.
You aren't setting MrPring's Ability but that's the one you're printing.
You are only setting Entity's ability, which you aren't printing.
A virtual function doesn't make much sense in this case.
// main.cpp
#include <iostream>
#include <vector>
#include "MrPring.h"
#include "MrBang.h"
using std::cout, std::cin; // can do this with C++17
int main()
{
MrPring Pring1(100, 10, 3, "Power Punch");
cout << Pring1.getHealth() << '\n';
cout << Pring1.getPower() << '\n';
cout << Pring1.getLives() << '\n';
cout << Pring1.getAbility() << '\n';
Pring1.action();
std::cout << '\n';
// Because action is virtual, it can be called through
// a pointer or reference to the base class.
Entity& e = Pring1;
e.action();
std::cout << '\n';
// This means we can have a container of different derived classes
// and call the virtual functions.
std::vector<Entity*> v;
v.push_back(new MrPring(1, 2, 3, "Power Punch"));
v.push_back(new MrBang(4, 5, 6, 100));
for (constauto e: v) e->action();
for (constauto e: v) delete e;
}
// Entity.h
#pragma once
#include <string>
class Entity
{
int health;
int power;
int lives;
public:
Entity(int h, int p, int l)
: health(h), power(p), lives(l) { }
// Should usually have a virtual dtor if you have virtual functions.
virtual ~Entity() = default;
int getHealth() const { return health; }
int getPower() const { return power; }
int getLives() const { return lives; }
// "Pure" virtual function (must be overriden in derived class)
virtualvoid action() = 0;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// MrPring.h
#pragma once
#include <string>
#include "Entity.h"
class MrPring : public Entity
{
// Derived class can have extra data.
std::string ability;
public:
MrPring(int h, int p, int l, std::string a)
: Entity(h, p, l), ability(a) { };
// Derived class can have extra functions.
std::string getAbility() { return ability; }
// Derived class can override functions.
void action() override { std::cout << "Pow!!!\n"; }
};
1 2 3 4 5 6 7 8 9 10 11 12 13
// MrBang.h
#pragma once
#include "Entity.h"
class MrBang : public Entity
{
int range;
public:
MrBang(int h, int p, int l, int r)
: Entity(h, p, l), range(r) { };
int getRange() { return range; }
void action() override { std::cout << "BANG!!!\n"; }
};
Hi, thank you for the help, I now have the program working. I havent touched on vectors yet, so I did not go down the exact route you showed, but it will be helpful for when i go over vectors next, so thank you for that.
I am confused about one part of your code, that I have never seen before, it is with the constructors (Like MrPring shown below) Why is ability(a) out on the end like that, I have never seen a variable be added on like that? I have only ever seen it in the constructor either in the () or in the curly braces as something like ability = a.
1 2
MrPring(int h, int p, int l, std::string a)
: Entity(h, p, l), ability(a)