Search code examples
c++inheritancevectorvirtualabstract

Abstract class with inheritance classes and functions writing into separate vectors c++


Card game; I'm using abstract class for the "hands" which hold cards. The "hands" need to be slightly different but they share majority of the functions. So there is a Hand and a MasterHand which will inherit from the Hand and have an additional function. Everything goes through the deck which deals cards. My aim is to formulate the function within the Hand class in a way which allows writing into handV of the instance of the Abstract class that is called- regardless of it being Hand of MasterHand.. I've tried different methods but couldn't make any of them work.

class AbstractClass
{
    virtual ~AbstractClass(){}        
    virtual void getCard(Card::card)=0 ;

};

class Hand:public AbstractClass
 {
 public: 
    vector<Card::card> handV;
    virtual void getCard(Card::card k) override
    {
        writeIntoVector(k);
    }
    void writeIntoArray(Card::card g)
    {
        handV.push_back(g);
    }
};
class HandMaster:public Hand
{
    public: 
    vector<Card::card> handV;
// now here I would like to use the functions from Hand, but make them write into HandMaster.handV rather than Hand.handV
};

class Deck
{
    void passCard(AbstractBase &x)
    {
        x.getCard(deck.back());
    }
};

int main
{
    AbstractBase* k=&h;
    Hand* p = static_cast<Hand*>(k);  // so I was trying to do it via casting in different convoluted ways but failed
    MasterHand h2;
    AbstractBase* m=&h2;
    d.passCard(*k); // here I'd like the card to be passed to Hand.handV
    d.passCard(*m); // here I'd like the card to be passed to MasterHand.handV
}

Solution

  • I added and simplified some of the code. But I will point you to some resources on polymorphism to get you started. I also remove the AbstractClass entirely since from an OOP perspective, you have objects that are Hands and another Master hand object.

    https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

    #include <vector>
    #include <iostream>
    //dummy class
    struct Card{
        int x; 
    };
    
    class Hand{
     public: 
        Hand(){}
        std::vector<Card> handV;
        virtual ~Hand(){}        
        virtual void getCard(Card k) 
        {
            handV.push_back(k);
        }
        virtual void showHand() 
        {
            for(std::vector<Card>::const_iterator it = handV.begin(); 
                it != handV.end(); 
                it++) 
            std::cout << it->x << " ";
        }
    };
    class HandMaster: public Hand
    {
        public:
        HandMaster(){} 
        //add additional methods
    };
    
    class HandOther: public Hand
    {
        public:
        HandOther(){} 
        //add additional methods
    };
    
    class Deck
    {
    public:
        std::vector<Card> deck;
        Deck(){
            Card c;
            for(int i = 1; i <= 52; ++i){
                c.x = i; 
                deck.push_back(c);
            }
        }
        void passCard(Hand *x)
        {
            x->getCard(deck.back());
            deck.pop_back();
        }
    };
    
    int main()
    {
        Deck d;
        Hand* p = new Hand();
        HandMaster *m = new HandMaster();
        HandOther * o = new HandOther();
        for(int i =0; i < 5; ++i){
            d.passCard(p); 
            d.passCard(m); 
            d.passCard(o); 
        }
        std::cout << "\nHand:";
        p->showHand();
        std::cout << "\nHandMaster:";
        m->showHand();
        std::cout << "\nHandOther:";
        o->showHand();
    
        std::cout << "\n";
        delete o;
        delete p;
        delete m;
    }