-3

I read about defining a struct like:

struct someStruct {
    int x;
    int y;
};

struct otherStruct : public someStruct {};

So my question is about the definition of otherStruct. What does this definition do? I'm new in C++ so i only want to know under which key word i can find the definition of otherStruct to read about them in a book.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ventu
  • 780
  • 1
  • 12
  • 25
  • 3
    [inheritance](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%2B%2B%20inheritance) – bolov Jan 30 '15 at 11:37
  • 4
    Are you asking how inheritance works? That's rather too large a subject for a simple answer, but your [introductory book](http://stackoverflow.com/questions/388242) should have a chapter about it. – Mike Seymour Jan 30 '15 at 11:40
  • @ChristinaObermaier [German] Guck dir mal das hier an... http://openbook.galileo-press.de/c_von_a_bis_z/ ist zwar C, aber davon kannst du C++ auch nach und nach lernen. – Blacktempel Jan 30 '15 at 12:12
  • I only asked what this syntax part means. Now i know that otherStruct extends someStruct and that all i wanted to konw! – Ventu Jan 30 '15 at 15:37

1 Answers1

2

This answer is just a scratch on the surface of the inheritance concept of OOP and it does not cover all its aspects. You should read a book about C++ (or about OOP in general) to get a complete answer.

The part struct otherStruct : public someStruct says that otherStruct extends someStruct with public inheritance. In simple words, public inheritance does not change the visibility of the members (properties and methods) inherited from the base class.

The declaration block of the new struct ({}) is empty. It does not add any new members to those inherited from struct someStruct.

If you compare someStruct and otherStruct by their memory footprint and behaviour, they are identical. But they are different types and they cannot be replaced one for the other.

However, a pointer to a variable of type otherStruct can be used where a pointer to struct someStruct is expected (because otherStruct, by extending someStruct has all the properties expected from someStruct) but the other way around is not possible.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thank you for your help. I know what OOP is and its concepts. What i do not know is writing c++.. so i did not know what the syntax ment. Now i know that otherStruct extends someStruct. Thats all i wanted to know! – Ventu Jan 30 '15 at 15:33