简体   繁体   中英

How to: Polymorphism implemented in print function?

So I have written three classes: character(abstract), zombie(concrete) and goblin(concrete). I am being instructed to change my print method in character to utilize polymorphism. The problem is I am still a bit fuzzy on what the difference between polymorphism and inheritance. Furthermore, I am unsure how you would implement a print function like that (one that could be called in my concrete class but print my variables local to the concrete and derived from the abstract). Any help is greatly appreciated.

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;
    }
};

main:

#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;
}

to make a class abstract at least provide an abstract virtual member function in it:

class character
{
    virtual void print() = 0; // pure virtual function makes the whole class abstract (uninstantiatable)
};

the class character now is uninstantiateable so we cannot write:

character ch; // character is abstract

each class inherits from this class is by default abstract thus uninstantiateable to be able to instantiate the derived class:

the derived class must override the base class's pure member function. eg:

class varchar : public character
{
    void print(){} // overriden
};

now we can instantiate class varchar.

  • you can also provide the abstract member function print a definition if you want some derived objects to chain up for functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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