所以我写了三个类:character(抽象),zombie(具体)和goblin(具体)。我被指示改变我的打印方法,以利用多态性。问题是我对多态性和继承之间的区别仍然有点模糊。此外,我不确定如何实现这样的打印功能(可以在我的具体类中调用,但打印我的变量局部到混凝土并从抽象派生)。任何帮助是极大的赞赏。
abstract.h:
#pragma once
#include <iostream>
using namespace std;
#include <string>
//abstract
class character
{
protected:
string name;
int health;
public:
void setattributes(string charname, int charhealth)
{
name = charname;
health = charhealth;
}
virtual string returnname()
{
return (name);
}
virtual int returnhealth()
{
bool is_positive = health > 0;
bool is_negative = health < 0;
bool is_null = health == 0;
if (is_positive)
{
return (health);
}
else if (is_negative)
{
cout << "ERROR: Health cannot be a negative number" << endl;
return 0;
}
else if (is_null)
{
cout << "ERROR: Health cannot be a null value" << endl;
return 0;
}
}
void printcharinfo()
{
cout << "Abstract Attributes: (Character Name: " << returnname() << " | " << "Character Health: " << returnhealth() << ")" << endl;
}
};
concrete.h:
#pragma once
#include "abstracts.h"
//specified - zombie
class zombie : public character
{
protected:
string zombieGender;
int attack;
public:
void setzAttributes(string zgender, int zattack)
{
zombieGender = zgender;
attack = zattack;
}
string returngender()
{
return (zombieGender);
}
int returnattack()
{
return (attack);
}
void printzcharinfo()
{
cout << "Specified Attributes: (Zombie Gender: " << returngender() << " | " << "Zombie Attack: " << returnattack() << ")" << endl;
}
};
//specified - goblin
class goblin : public character
{
protected:
string goblinWeapon;
int goblinIntel;
public:
void setgAttributes(string gweapon, int gintel)
{
goblinWeapon = gweapon;
goblinIntel = gintel;
}
string returnweapon()
{
return (goblinWeapon);
}
int returnintel()
{
return (goblinIntel);
}
void printgcharinfo()
{
cout << "Specified Attributes: (Goblin Weapon: " << returnweapon() << " | " << "Goblin Intel: " << returnintel() << ")" << endl;
}
};
主要
#include <iostream>
#include <cctype>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <windows.h>
#include <iomanip>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <istream>
#include "abstracts.h"
#include "instances.h"
//main
int main() {
character jimmy;
zombie walker;
goblin creepy;
character * ptr_to_character = &jimmy;
zombie * ptr_to_zombie = &walker;
goblin * ptr_to_goblin = &creepy;
bool leave = false;
int option;
do
{
cout << "1 - Generic Character" << endl;
cout << "2 - Zombie" << endl;
cout << "3 - Goblin" << endl;
cout << "Would you like to enter a gerneic character, a zombie or a goblin? (4 - quit): ";
cin >> option;
switch (option)
{
case 1:
{
string charname;
int charhealth;
cout << "--- set the qualities for generic character ---" << endl << endl;
cout << "Enter your characters name: ";
cin >> charname;
cout << "Enter your characters health: ";
cin >> charhealth;
//---------------insert: cut
ptr_to_character->setattributes(charname, charhealth);
cout << "Character info:" << endl << endl;
ptr_to_character->printcharinfo();
break;
}
case 2:
{
//--------------insert: paste
string zomname;
int zomhealth;
string zomgender;
int zomattack;
cout << "--- set the qualities for zombie ---" << endl << endl;
cout << "Enter your zombies name: ";
cin >> zomname;
cout << "Enter your zombies health: ";
cin >> zomhealth;
cout << "Enter your zombies gender: ";
cin >> zomgender;
cout << "Enter your zombies attack: ";
cin >> zomattack;
//---------------insert: cut
ptr_to_zombie->setattributes(zomname, zomhealth);
ptr_to_zombie->setzAttributes(zomgender, zomattack);
cout << "Zombie info:" << endl << endl;
ptr_to_zombie->printcharinfo();
ptr_to_zombie->printzcharinfo();
break;
}
case 3:
{
string gobname;
int gobhealth;
string gobweapon;
int gobintel;
cout << "--- set the qualities for goblin ---" << endl << endl;
cout << "Enter your goblins name: ";
cin >> gobname;
cout << "Enter your goblins health: ";
cin >> gobhealth;
cout << "Enter your goblins weapon: ";
cin >> gobweapon;
cout << "Enter your goblins intel: ";
cin >> gobintel;
//---------------insert: cut
ptr_to_goblin->setattributes(gobname, gobhealth);
ptr_to_goblin->setgAttributes(gobweapon, gobintel);
cout << "Goblin info:" << endl << endl;
ptr_to_goblin->printcharinfo();
ptr_to_goblin->printgcharinfo();
break;
}
case 4:
{
int quitVar;
cout << "Are you sure you want to exit the program?: ";
cin >> quitVar;
cin.ignore();
if (quitVar == 1)
{
cout << "The program will now be terminated." << endl;
leave = true;
}
else if (quitVar == 0) cout << "Returning to the main menu." << endl;
}
break;
}
} while (leave == false);
return 0;
}
答案 0 :(得分:0)
使一个类抽象至少在其中提供一个抽象的虚拟成员函数:
class character
{
virtual void print() = 0; // pure virtual function makes the whole class abstract (uninstantiatable)
};
类字符现在是不可实例化的,所以我们不能写:
character ch; // character is abstract
每个继承自此类的类默认为抽象,因此无法实例化以便能够实例化派生类:
派生类必须覆盖基类的纯成员函数。 例如:
class varchar : public character
{
void print(){} // overriden
};
现在我们可以实例化类varchar。