0

I am pretty new to computing, especially to C++ and the functionality that it provides. Please be gentle with the bashing :)

I would like to ask if it is possible to declare a subclass object in another subclass. For example, can I have

class Shape {
...
};

class Square: public Shape {
..
};

class Cube: public Shape {
...
Square sq1;
...
};

I am asking this since there is a fatal error LNK2019 when I tried to compile, but my IDE is not highlighting anything specific.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
Tyler
  • 13
  • 3

1 Answers1

0

Sure. There's nothing particularly different about Square than from any other class. Just the fact that it happens to be a subclass of another class makes no practical difference in this context.

Just like you can have:

class Shape { ... };

class Square { .. };

class Cube: public Shape { ... Square sq1; ... };

You can also have:

class Shape { ... };

class Square : public Shape { .. };

class Cube: public Shape { ... Square sq1; ... };

Of course, whether something is a subclass of another class changes which methods the class instance has, since it's going to inherit public methods from its superclass.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148