user3569912
user3569912

Reputation: 3

C++ Inheritance: How do you make it so that the kid's data member can be seen in its parent's functions and used?

How do you make it so that the kid's data member can be seen in its parent's functions and used? (Without using virtual) Assignment: I need to make a board game class and derive a class of reversi (of any size board). It's required to put the print board in the parent class. I'm stuck on how to pass the board size to the parent to print it correctly.

#include <iostream>

using namespace std;

class Parent{
public:
    void someFuc(){cout << person << endl;}
};

class Kid: public Parent{
private:
    int person = 2;
};

int main()
{
    Kid b = Kid();

    b.someFuc(); // want it to print 2

    return 0;
}

Upvotes: 0

Views: 139

Answers (5)

Deduplicator
Deduplicator

Reputation: 45654

#include <iostream>

template <size_t a, size_t b> class Board;
template<> class Board<0,0> {
    Board<0,0>(size_t a, size_t b) : _a(a), _b(b) {}
    size_t _a, _b;
    void operator=(Board<0,0>&) = delete;
private:
    char* pBoard();
};
template<size_t a, size_t b> struct Board : Board<0,0> {
    friend class Board<0,0>;
protected:
    union{
        char collapsed[a*b];
        char field[a][b];
    };
};
inline char* Board<0,0>::pBoard() {
    return static_cast<Board<1,1>*>(this)->collapsed;
}

That allows you to declare a Board of any size and use any member of the child Board in the parent.
Also, the dimensions are in the parent because you need to save them for all boards.

Upvotes: 0

brader24
brader24

Reputation: 485

Make the board size a member of the parent if that is where it belongs and then provide a way for the child to set it either through a constructor or a method.

If you want to force the child class to set the board size on the parent, make a protected constructor for the parent that takes the board size and not include a public constructor. This prevents the base class from being created on its own. It can only be created by a derived class that must provide a board size.

#include <iostream>

using namespace std;

class Parent{
protected:
    Parent(int person)
    {
        m_person = person;
    }

public:
    void someFuc(){cout << person << endl;}

private:
    int m_person;
};

class Kid: public Parent{
public:
    Kid(int person) : Parent(person) {}
};

int main()
{
    Kid b = Kid(2);

    b.someFuc(); // want it to print 2

    return 0;
}

Upvotes: 1

caring-goat-913
caring-goat-913

Reputation: 4049

Consider using templates. The "parent" class would take a template argument which would be the board type. All boards abide by some interface or a compiler error will occur.

Check out Policy Based Design here: http://en.wikipedia.org/wiki/Policy-based_design

Parameters such as game type and board size could also be their own policies.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182761

1) If the functionality belongs in the base class, put it in the base class.

2) If the functionality belongs in the derived class, but should be provided by every such class derived from this base, make it virtual in the base.

3) If the functionality belongs in the derived class and should not be provided by every such class derived from this base, it should not be accessible in the parent class.

If you're in the case where virtual is the perfect solution but due to some absurd restriction can't use virtual, you'll have to roll your own virtual using either dynamic_cast or give the base class function pointers that are initialized in the constructor.

Upvotes: 0

dogiordano
dogiordano

Reputation: 714

The size should be stored in the parent because every Board has a size.

Upvotes: 0

Related Questions