Thread: Is this initialization of class variables and object setup correct usage?

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

    Is this initialization of class variables and object setup correct usage?

    I am working on a console game and I am wondering if this is correct usage and setup of class objects, mainly in main, I think it's ok but idk, it just doesnt feel right, it feels like a kludge to me. It's not complete but this is what I have so far. These are not all of the classes either but this is the one i'm working on.


    main.cpp


    Code:
    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include "Item.h"
    #include "Player.h"
    #include "Arena.h"
    #include "Shop.h"
    #include "Dinosaur.h"
    
    
    
    
    int main()
    {
        Shop shop;
        Arena arena;
    
    
        shop.AddItems("Golden Necklace of Speak Good", 450);
        shop.AddItems("Helmet of Protect Head", 800);
        shop.AddItems("Staff of Magic Stuff", 8000);
        shop.AddItems("Mask of Suffocation", 420);
        shop.AddItems("Dagger of Major Stabness", 230);
    
    
        Player player("Chay", 60000);
    
    
        std::vector<std::pair<std::string, int>> trexAttacks, velociraptorAttacks;
    
    
    
    
        //Create T-Rex
    
    
        trexAttacks.push_back(std::make_pair("Stomp", 80));
        trexAttacks.push_back(std::make_pair("Crunch", 30));
    
    
        Dinosaur Trex("T-Rex", 800, trexAttacks);
    
    
        std::cout << Trex.GetDinosaurName() << '\n';
    
    
        Trex.ShowAllDinosaurAttacks();
    
    
        std::cout << "\n";
    
    
    
    
        //Create Velociraptor
    
    
        velociraptorAttacks.push_back(std::make_pair("JumpAttack", 20));
        velociraptorAttacks.push_back(std::make_pair("Bite", 15));
    
    
        Dinosaur Velociraptor("Velociraptor", 400, velociraptorAttacks);
    
    
        std::cout << Velociraptor.GetDinosaurName() << '\n';
    
    
        Velociraptor.ShowAllDinosaurAttacks();
    
    
        std::cout << "\n";
    
    
    
    
        //Show list of Arena opponents (All possible enemies the player could face)
    
    
        arena.CreateListOfOpponents(Trex, trexAttacks);
        arena.CreateListOfOpponents(Velociraptor, velociraptorAttacks);
    
    
        //Choose and enemy randomly
    
    
        std::cout << "\n";
    
    
        arena.RandomlyChooseEnemy();
    
    
        //shop.Buy(player);
    
    
        return 0;
    }



    Dinosaur.h


    Code:
    #pragma once
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <time.h>
    #include <stdlib.h>
    
    
    class Dinosaur
    {
        public:
            Dinosaur(std::string dinosaurName, int dinosaurHealth, std::vector<std::pair<std::string, int>> dinosaurAttacks)
                : mDinosaurName(dinosaurName), mDinosaurHealth(dinosaurHealth), mDinosaurAttacks(dinosaurAttacks){}
    
    
            void ChooseRandomAttack()
            {
                srand(time(NULL));
    
    
                unsigned int chooseRandomAttack = rand() % mDinosaurAttacks.size();
                
                            //TO DO
                //If number is within certain range, perform attack.
            }
    
    
            void ShowAllDinosaurAttacks()
            {
                for(unsigned int i = 0; i < mDinosaurAttacks.size(); i++)
                { 
                    std::cout << mDinosaurAttacks[i].first << " " << mDinosaurAttacks[i].second << "\n";
                }
            }
    
    
            std::string GetDinosaurName() const { return mDinosaurName; }
            std::string GetDinosaurAttackName() const { return mDinosaurAttackName; }
    
    
            int GetDinosaurAttackPower() const { return mDinosaurAttackPower; }
    
    
            int GetDinosaurHealth() const { return mDinosaurHealth; }
    
    
    
    
        private:
            int mDinosaurHealth;
            int mDinosaurAttackPower;
    
    
            std::string mDinosaurAttackName;
            std::string mDinosaurName;
            std::vector<std::pair<std::string, int>> mDinosaurAttacks;
    };

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    56
    bump I've been told this is bad design? not really sure why though, they didnt specify, to me this was perfectly reasonable solution.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have one of these? If not, try creating one.
    Entity–relationship model - Wikipedia

    A few minutes of sketching and reworking can save weeks of pointless code rewrites.

    > Dinosaur Trex("T-Rex", 800, trexAttacks);
    ...
    > arena.CreateListOfOpponents(Trex, trexAttacks);
    The second use of trexAttacks just seems wrong.
    If something wants to know about attacks, it should go through Dinosaur, not through Arena.
    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. correct usage of strcpy_s
    By diego31416 in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2018, 06:06 PM
  2. Correct usage of malloc and free for 2D array
    By disruptivetech in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 05:20 AM
  3. Correct usage of malloc()
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2005, 06:28 AM
  4. Class object memory usage?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2003, 02:45 AM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 PM

Tags for this Thread