I'm learning about polymorphism and this is a small game. I have a representative class Character here, I want to program so that from a Character people can choose Warrior or Archer to continue the game.
#pragma once
#include <iostream>
using namespace std;
#include <string>
class Warrior;
class Archer;
class Character {
public:
Character(void);
~Character(void);
Character* creatCharacter(int choice, string CharacterName) {
if (choice == 1)
return (Character*)new Warrior(CharacterName);
if (choice == 2)
return (Character*)new Archer(CharacterName);
return NULL;
}
virtual void Skill_Cast() {};
};
class Warrior :public Character {
private:
string name;
public:
Warrior(void);
~Warrior(void);
Warrior(string CharacterName) {
name = CharacterName;
}
void Skill_Cast() {
cout << "Punch!" << endl;
}
};
class Archer : public Character
{
private:
string name;
public:
Archer(void);
~Archer(void);
Archer(string CharacterName) {
name = CharacterName;
}
void Skill_Cast() {
cout << "Shoot!" << endl;
}
};
In the main function:
int main() {
cout <<"Enter character's name: ";
string name;
getline(cin, ten, '\n');
cout <<"Enter your character class by number (1),(2),(3): ";
int choice;
cin >> choice;
Character* YourChar;
YourChar = YourChar->creatCharacter(choice, name);
YourChar->Skill_Cast();
}
And this is ERRORS:
Error 1 error C2512: 'Warrior' : class has no constructors
Error 2 error C2514: 'Archer' : class has no constructors
Can you explain me the errors and help me fix that, by the way, Is this a kind of "Abstract Factory Design Pattern" ? Thanks so much. (sorry for my bad English)
Aucun commentaire:
Enregistrer un commentaire