Are these two examples equivalent:
class a {}; class b: public a {}; and
class a{}; ax; class b { public: ax; }; Are these two examples equivalent:
class a {}; class b: public a {}; and
class a{}; ax; class b { public: ax; }; in the first case, the class b openly inherits the class a . In the second case, you create an instance of the class a .
@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.
Source: https://ru.stackoverflow.com/questions/273423/
All Articles