繁体   English   中英

如何:在打印功能中实现多态?

[英]How to: Polymorphism implemented in print function?

所以我写了三类: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;
}

使类抽象至少在其中提供抽象的虚拟成员函数:

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类。

  • 如果希望某些派生对象链接以实现功能,则还可以提供抽象成员函数打印定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM