-1

I'm new to object oriented programming and am struggling a bit with how best to write classes.

I am trying to abstract the idea of sorting to objects that are not just lists of numbers. I have an abstract base class, SortableContainer, which contains all the necessary virtual functions for comparing and swapping elements, along with some overloaded operators. I then have two classes derived from that, MVector and CoordinateArray. Both of these derived classes have proper definitions for all the virtual functions in the base class. Everything up to this point has worked just fine. MVector just stores vector-like objects and CoordinateArray stores vectors of coordinates onto which a notion of 'less than' has been defined.

My problem now is that I have created a new class, Life, which I want to use to implement the game of life using a CoordinateArray object to store the alive cells. The outline of my Life class looks like this:

class Life
{
public:
   CoordinateArray LiveCells;

   Life();
};

When I create a Life object and initialise it with the coordinates of some alive cells, none of the member functions defined in the CoordinateArray derived class will work. How can I fix this? Do I have to derive the Life class from the SortableContainer class and then override all the pure virtual functions? Any help or direction to help will be much appreciated.

mcsim
  • 1,647
  • 2
  • 16
  • 35
Jamgreg
  • 15
  • 1
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Nov 13 '17 at 18:00
  • 1
    Why do you have all these classes? They seem completely unnecessary for the problem at hand. (And for the vast majority of problems, for that matter.) – molbdnilo Nov 13 '17 at 18:03
  • Why do you say that "none of the fuctions ... will work"? I don't get you point. Could you give an example? – mcsim Nov 13 '17 at 18:09
  • What exactly happens that's not expected? What exact does not happen that is expected? "will not work" is not a problem description, much less a question. –  Nov 13 '17 at 18:32
  • Does your CoordinateArray class have a public interface? A class outside of the inheritance hierarchy can only use public functions of the class. – Basya Nov 13 '17 at 18:43
  • It would be great to see what your `CoordinateArray` class looks like, as well as examples of what you are trying to do with `Life` and what is going wrong. If you're expecting a `Life` object to have member functions from `CoordinateArray`, then it needs to inherit from `CoordinateArray`; if you just want the `CoordinateArray` object *in* `Life` to use those functions, then you just need to make sure they were declared public. – colopop Nov 13 '17 at 19:05
  • @molbdnilo The other classes were made as part of a larger project - I then have to use the sorting functions as a way to implement the game of life – Jamgreg Nov 13 '17 at 19:17
  • @Arkadiy apologies for not being clear - One example of something that won't work is that I have overloaded the << operator in my `CoordinateArray` class, but when I instantiate a `Life` object and try to print it, it tells me "no operator "<<" matches these operands". Similar errors occur when I try to access the size of a `Life` object. Thanks for your reply – Jamgreg Nov 13 '17 at 19:22
  • @Basya from all the responses I am guessing this is where the issue lies - my functions have all been written under a public interface but I perhaps have made a slip somewhere with the syntax – Jamgreg Nov 13 '17 at 19:24
  • @colopop Thanks for your reply, I wasn't sure about putting too much code in my question but I shall outline my `CoordinateArray` class: – Jamgreg Nov 13 '17 at 19:33
  • @colopop class CoordinateArray : public SortableContainer { public: // constructors CoordinateArray() {} explicit CoordinateArray(int n) : v(n) {} // creates a square coordinate array of length root n // access element (lvalue) IntegerCoordinate &operator[](unsigned int index) { assert(index < v.size()); return v[index]; } There are then several other member functions (under public:) which include functions for swapping elements, etc. It ends with `private: std::vector v;` where I have also defined an `IntegerCoordinate` struct – Jamgreg Nov 13 '17 at 19:36
  • Please edit the updates into your questions. Code is not easy to look at in the comments. Also, please clarify what you mean by "try to access the size of a Life object" - do you mean `sizeof`? –  Nov 13 '17 at 21:04

3 Answers3

0

To answer your question simply and in a pragmatical way, yes, if you want your Life object to have some member functions they need to come from somewhere, the most straight forward way is to derive it from a class containing those member functions, composition does not transfer members to the owner, inheritance does that:

class Life : public CoordinateArray
{
public:
   Life();
};

Now you can use your Life objects as a CoordinateArray object. If anything is pure virtual in the parent class you will indeed need to implement it in the derived class, but even if something is not you can still reimplement it in the child class to overwrite the parent behaviour.

I avoided conception and design problematics on purpose, this is mainly a technical answer, judging this from a design point of view requires more context and has some subjective side too so that's another story altogether.

Drax
  • 12,682
  • 7
  • 45
  • 85
0

It would be useful to see the declaration of CoordinateArray. However, I suspect you need to make public methods in CoordinateArray. If the methods are declared private (i.e., they come after private: in your class declaration), then they can only be used inside the class's own scope. If they're declared public (underneath public:), then they can be used in any scope. (As a sidenote, there is one other classification, protected, which means they can be used by the class and any of its subclasses.)

colopop
  • 441
  • 3
  • 13
-1

In order to initialize co-ordinates of some live cells declare the class CoordinateArray LiveCells public to Life

class Life::public CoordinateArray{ }
handlerFive
  • 870
  • 10
  • 23