Thread: Am I using virtual correctly?

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    49

    Am I using virtual correctly?

    I am making a text based dinosaur fighting game and I have different dinosaurs and guns and I used virtual to create these things but I am unsure if that's necessary or if it is, am I using it correctly.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <map>
    
    
    class Player
    {
        public:
            Player() = default;
            ~Player() = default;
    
    
            void SavePlayerData();
    
    
    
    
        private:
            std::string playerName = "Default_Player";
            float playerHealth = 100;
    
    
            void RestorePlayerData();
    };
    
    
    void Player::SavePlayerData()
    {
        std::ofstream Save("PlayerData.txt");
    
    
    
    
    }
    
    
    class Weapon
    {
        public:
            Weapon() = default;
            ~Weapon() = default;
    
    
            virtual void SetWeaponName(std::string weapName){ weaponName = weapName; }
            std::string ReturnWeaponName() const { return weaponName; }
    
    
            virtual void SetWeaponDamage(float weapDamage){ weaponDamage = weapDamage; }
            float ReturnWeaponDamage() const { return weaponDamage; }
    
    
        private:
            std::string weaponName = "Default_Weapon";
            float weaponDamage = 0;
    };
    
    
    class Handgun : public Weapon
    {
    
    
    };
    
    
    
    
    class Dinosaur
    {
        public:
            Dinosaur() = default;
            ~Dinosaur() = default;
    
    
            void SaveDinosaurData();
    
    
            virtual void SetDinosaurName(std::string name){ dinosaurName = name; };
            std::string ReturnDinosaurName() const { return dinosaurName; };
    
    
            virtual void SetDinosaurAttackPower(){};
            float ReturnDinosaurAttackPower() const;
    
    
            virtual void SetDinosaurAttackName(){};
            std::string ReturnDinosaurAttackName() const;
    
    
            virtual void SetDinosaurHealth(){};
            float ReturnDinosaurHealth() const;
    
    
        private:
            float dinosaurHealth = 100;
            float dinosaurAttackPower = 0;
            std::string dinosaurAttackName = "Default";
            std::string dinosaurName = "Default_Dinosaur";
    
    
            void RestoreDinosaurData();
    };
    
    
    
    
    class Utahraptor : public Dinosaur
    {
        public:
            Utahraptor();
            ~Utahraptor();
    };
    
    
    class Allosaurus : public Dinosaur
    {
        public:
            Allosaurus() = default;
            ~Allosaurus();
    };
    
    
    
    
    void Fight();
    void ReturningPlayer();
    void NewPlayer();
    
    
    int main()
    {
        int choice = 0;
    
    
        Dinosaur *dino = new Allosaurus;
        dino->SetDinosaurName("Allosaurus");
    
    
        Weapon *weap = new Handgun;
        weap->SetWeaponName("Handgun");
    
    
        std::cout << dino->ReturnDinosaurName() << std::endl;
        std::cout << weap->ReturnWeaponName() << std::endl;
    
    
    
    
    /*
        while(choice != -1)
        {
            std::cout << "Welcome, are you a new player or are you a returning one?\n" << std::endl;
    
    
            std::cout << "1) I'm New" << std::endl;
            std::cout << "2) I'm Returning" << std::endl;
            std::cin >> choice;
    
    
            switch(choice)
            {
                case 1:
                    NewPlayer();
                    break;
                case 2:
                    ReturningPlayer();
                    break;
                default:
                    std::cout << "Incorrect Choice!" << std::endl;
            }
        }
    */
        return 0;
    }
    
    
    
    
    void ReturningPlayer()
    {
    
    
    }
    
    
    
    
    void NewPlayer()
    {
    
    
    }
    
    
    
    
    void Fight()
    {
    
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    49
    Updated code, I dont know how to edit my post:


    Code:
    #include <iostream>#include <string>
    #include <fstream>
    #include <map>
    
    
    class Player
    {
        public:
            Player() = default;
            ~Player() = default;
    
    
            void SavePlayerData();
    
    
    
    
        private:
            std::string playerName = "Default_Player";
            float playerHealth = 100;
    
    
            void RestorePlayerData();
    };
    
    
    void Player::SavePlayerData()
    {
        std::ofstream Save("PlayerData.txt");
    
    
    
    
    }
    
    
    class Weapon
    {
        public:
            Weapon() = default;
            virtual ~Weapon() = default;
    
    
            virtual void SetWeaponName(std::string weapName){ weaponName = weapName; }
            std::string ReturnWeaponName() const { return weaponName; }
    
    
            virtual void SetWeaponDamage(float weapDamage){ weaponDamage = weapDamage; }
            float ReturnWeaponDamage() const { return weaponDamage; }
    
    
        private:
            std::string weaponName = "Default_Weapon";
            float weaponDamage = 0;
    };
    
    
    class Handgun : public Weapon
    {
    
    
    };
    
    
    
    
    class Dinosaur
    {
        public:
            Dinosaur() = default;
            virtual ~Dinosaur() = default;
    
    
            void SaveDinosaurData();
    
    
            virtual void SetDinosaurName(std::string name){ dinosaurName = name; };
            std::string ReturnDinosaurName() const { return dinosaurName; };
    
    
            virtual void SetDinosaurAttackPower(){};
            float ReturnDinosaurAttackPower() const;
    
    
            virtual void SetDinosaurAttackName(){};
            std::string ReturnDinosaurAttackName() const;
    
    
            virtual void SetDinosaurHealth(){};
            float ReturnDinosaurHealth() const;
    
    
        private:
            float dinosaurHealth = 100;
            float dinosaurAttackPower = 0;
            std::string dinosaurAttackName = "Default";
            std::string dinosaurName = "Default_Dinosaur";
    
    
            void RestoreDinosaurData();
    };
    
    
    
    
    class Utahraptor : public Dinosaur
    {
    
    
    };
    
    
    class Allosaurus : public Dinosaur
    {
    
    
    };
    
    
    
    
    void Fight();
    void ReturningPlayer();
    void NewPlayer();
    
    
    int main()
    {
        //int choice = 0;
    
    
        Dinosaur *dino = new Allosaurus;
        dino->SetDinosaurName("Allosaurus");
    
    
        Weapon *weap = new Handgun;
        weap->SetWeaponName("Handgun");
        weap->SetWeaponDamage(30.00);
    
    
        std::cout << dino->ReturnDinosaurName() << std::endl;
        std::cout << weap->ReturnWeaponName() << std::endl;
    
    
        delete dino;
        delete weap;
    
    
    
    
    /*
        while(choice != -1)
        {
            std::cout << "Welcome, are you a new player or are you a returning one?\n" << std::endl;
    
    
            std::cout << "1) I'm New" << std::endl;
            std::cout << "2) I'm Returning" << std::endl;
            std::cin >> choice;
    
    
            switch(choice)
            {
                case 1:
                    NewPlayer();
                    break;
                case 2:
                    ReturningPlayer();
                    break;
                default:
                    std::cout << "Incorrect Choice!" << std::endl;
            }
        }
    */
        return 0;
    }
    
    
    
    
    void ReturningPlayer()
    {
    
    
    }
    
    
    
    
    void NewPlayer()
    {
    
    
    }
    
    
    
    
    void Fight()
    {
    
    
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,664
    Have you drawn one of these?
    Entity–relationship model - Wikipedia

    Get some 5x3 index cards or post-it notes, and start making entries like this.
    Code:
    +====================+
    |       Name         |
    +====================+
    | Method  | Attribute|
    |         |          |
    |         |          |
    |         |          |
    |         |          |
    +====================+
    A whiteboard becomes a good idea if you have more than half a dozen cards to arrange.

    Later on, you could start to use tools.
    I like the look of this one as a fairly simple intro - Open-source tool that uses simple textual descriptions to draw UML diagrams.

    You don't need to document every single method or attribute, but you should try to capture the important ideas.

    If you can't fit a single object on a card (too many methods or attributes), it's a sign your object may need splitting.

    A couple of days spent on this will save you weeks of pointless coding and recoding.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queries regarding virtual pointer and virtual function
    By gaurav# in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2016, 04:06 AM
  2. Help with virtual function and virtual inheritance
    By shaxquan in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2010, 10:39 AM
  3. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  4. Confusing between Virtual and Non-virtual function
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2006, 06:30 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM

Tags for this Thread