Are these two examples equivalent:

class a {}; class b: public a {}; 

and

 class a{}; ax; class b { public: ax; }; 
  • @oasistravels, which language? - zb '
  • 7
    in the first case - inheritance, in the second - composition - Nofate
  • @eicto, C ++ same - Nofate
  • What does “equivalent” mean? Different code, works differently. - VladD

3 answers 3

in the first case, the class b openly inherits the class a . In the second case, you create an instance of the class a .

  • What what? In general, the question, of course, is incorrect. But the answer also does not go to the level of technical details. Indeed, when inheriting, the base class "as it were" is included in the descendant class. - gecube

@oasistravels , you just need to read about the relationship between objects (has-a, is-a) and such questions will disappear. Not bad about this in Salter is written. In general, I advise you to read, but if you are a beginner, then it would be better to start with Daytel .

PS I apologize for the quality pdf'ki.

    Answer: not equivalent. I'll try to explain on the fingers.

     class a {}; class b: public a {}; 

    In this code, class b is openly inherited from class a, which means it inherits both the interface and the implementation of class a. In this case, we can say that b - is a type of class a. If you project on the auto industry, you can safely say that the Audi A6 is a car

     class Auto {}; class AudiA6 : public Auto {}; 

    In the second example, we can talk about the composition:

     class a{}; ax; class b { public: ax; }; 

    Here we can say that the class b is realized by means of the class a, or the class b contains the class a. Example: any car has an engine under the hood due to which it can do work:

     class Engine {}; class Auto { private: Engine* m_engine; }; 

    It is incorrect to say that a car is a type of engine, but it is clear that the engine is an integral part of a car.

    Such is the difference, I hope these examples will slightly open the veil of secrecy.