私はキャラクター(抽象)、ゾンビ(コンクリート)、ゴブリン(コンクリート)の3つのクラスを書いています。私は、多形性を利用するために、私の印刷方法を変更するように指示されています。問題は多形性と相続性の違いについて私はまだ少しばかげているということです。さらに、私はあなたがどのように(私の具体的なクラスで呼び出すことができるが、コンクリートに局所的な変数を出力し、抽象クラスから派生したもの)のような印刷機能をどのように実装するのかは分かりません。どんな助けでも大歓迎です。方法:印刷機能で多態性が実装されていますか?
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;
}
文字クラスは抽象型ではありません – amchacon
_ "多型性と相続性の違いについてはまだ少し曖昧です。" _それほど大きな違いはありません。 –
'printzcharinfo'と' printgcharinfo'を削除して、 'printcharinfo'を上書きする必要があるようです。 –