c++ composition over inheritance. Compose when there is a "has a" (or "uses a") relationship, inherit when "is a". c++ composition over inheritance

 
 Compose when there is a "has a" (or "uses a") relationship, inherit when "is a"c++ composition over inheritance  In C++ you can either inherit both interface and implementation together (public inheritance) or you can inherit only the implementation (private inheritance)

Field: a named property of some type, which may reference another object (see composition) Method: a named function or procedure, with or without parameters, that implements some behavior for a class. That doesn't mean use it to the complete exclusion of inheritance. Composition: Have a member of type "Class B" in class A, thus being able to use its functionality. For example, a Car has components like the engine, wheels, etc. Composition to the rescue. How this method is implemented, whether by composition, generics or some other technique, is orthogonal. Inheritance breaks encapsulation, a change in the parent class can force a change in the sub classes, while Composition respects the interface. Reading the C++ faq, gives you an example on using private inheritance, but I seems easier to use composition + strategy pattern or even public inheritance than private inheritance. It allows us to create a new class (derived class) from an existing class (base class). Prefer Composition over Inheritance. Rewriting all the List methods may be annoying, but hardly impossible. Now you can have a class StudentWorker that inherits from. 1 Answer. g. 3. With Java-style object inheritance, reasoning about behavior can become very complicated, as a function call may resolve to a superclass definition, or a subclass in the inheritance chain. If an object contains the other object and the contained object cannot. . Với nguyên lý Composition over Inheritance ta gom các phương thức chung vào một đối tượng riêng sau đó thực hiện tham chiếu các đối tượng này vào đối tượng mới được khởi tạo. e. Why to. manages the lifecycle) of another object. Composition allows to test the implementation of the classes we are using independent of parent or child class. In Go, composition is preferred over inheritance as a way of structuring code and achieving code reuse. Generics with inheritance design - need help to fix. " What benefits was it giving you in this case? I would avoid blindly following "prefer composition over inheritance" like it's gospel. Inheritance: a class may inherit - use by default - the fields and methods of its superclass. Composition: “has a. In the world of Object-Oriented Programming (OOP) you may have heard the statement 'favour composition over inheritance'. Inheritance was designed, first and foremost, to model an "is-a" relationship through a hierarchy. Go for example has no inheritance. Meyers effective C++ : Item 20: Avoid data members in the public interface. Inheritance: a class may inherit - use by default - the fields and methods of its superclass. Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should favor polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) over. I know that the standard is "favor composition over inheritance", but that would make it so accessing the fields of B would be like "B. while inheritance can be described as is-a relation like a Canary is a bird, composition can be described as has-a relation like a Canary has a flying behavior, so instead of building hierarchy of classes, your classes will be like this. Vehicle* p = new Roadster(); Just to repeat it, non-public inheritance in C++ expresses a has-a relationship. Use inheritance over composition in Python to model a clear is a relationship. Overridden functions are in different scopes. Composition can be denoted as being an "as a part" or "has a" relationship between classes. Inheritance, the "is a" relationship, is summed up nicely in the Liskov Substitution Principle. The classic alternative in this case is the decorator pattern of interface implementation with composition: the new object contains. }; How are “private inheritance” and “composition” similar? private inheritance is a syntactic variant of composition (AKA aggregation and/or has-a). I think this is a good reason to consider inheritance instead of containment - if one follow the premise that those functions should be members (which I doubt). Composition over inheritance [A] Composition over inheritance is generally a good rule to follow, [B] but there are some cases where inheritance is a must. If there is a has-a (n) relationship, I would generally use composition. Pros: Reusable code, easy to understand; Cons: Tightly coupled, can be abused, fragile; Composition. An 'Address' class can contain some properties and functions and then be used as a property of a 'Student' class. In this tutorial, we’ll cover the basics of inheritance and composition, and we’ll focus strongly on spotting the differences between the two types of relationships. Instead of putting all your code in your outermost classes' methods, you can create smaller classes with smaller scopes, and smaller methods, and reuse those classes/methods throughout. 3. g. Composition plays a major role in the design of object-oriented systems. So if we want to keep the analogy of cars, we can say that a Car can privately inherit from the hypothetical Engine class - while it still publicly inherits from Vehicle. The composition is a design technique in java to implement a has-a relationship. Add a comment. Of course, c++ stacks are not iterable, so there is (intentianal or not) a very different. As mentioned earlier, the beauty of our craft, is that it is sometimes more of an art then a. However, this one is usually referring to interfaces. ComposedOfAbstractBase is not a solution. Some people said - check whether there is “is-a” relationship. It's more-or-less invisible to outsiders, and is sometimes described as meaning "is implemented in terms of a". So, in the code "A created" would be printed first. I understand that you want to avoid. 11 1. For example, if you write a Stack class in C++ using an std::vector, you don't want to derive Stack from vector. That book was called Design Patterns: Elements of Reusable Object-Oriented Software . Composition over Inheritance Inheritance is tightly coupled whereas composition is loosely coupled. 5 Answers. Favoring Composition over Inheritance is a principle in object-oriented programming (OOP). Improve this answer. So this question is specifically tagged C++, because the low level details are language dependent. Inheritance was created for a reason. There are situations when inheritance should be favored over composition, and the distinction is much more clear cut than a matter of style. 25. Scala 3 added export clauses to do this. While inheritance is a useful way to share functionality, it does have drawbacks. When an object of a class assembles objects from other classes in that way, it is called composition. Mixins are a flexible form of inheritance, and thus a form of composition. 19]: ". If inherited is a class template itself, sometimes need to write this->a to. Prefer using composition over inheritance when you need to reuse code and the types don’t have an “is a” relationship. – user2357112. If you do not need the strong relationship modeled by inheritance, then composition is the better choice. Another thing to consider when using inheritance is its “Singleness”. 5. Back to the first point: "Prefer composition over inheritance" is a just good heuristic. Less coupling between classes. C++ provides a unique variant on derivation which is a form of syntactic sugar for composition, although with some important differences. To get the higher design flexibility, the design principle says that composition should be favored over inheritance. Thats the secret — “Favor…The recommendation to prefer composition to inheritance does not mean "never ever use inheritance". This is not at all what is meant by composition. Without an explicit access modifier, class members are private, and struct members public. someMethod (); } void anotherMethod () { a. Overview. This is about inheritance versus composition - Java's Stack is-a Vector, while C++'s stack has-a deque inside of it. Like I stated before, I want the knowledge that B is a superset of A to be an implementation detail. “Favor composition over inheritance” is a design. Object Adapter uses composition and can wrap classes or interfaces, or both. ". Class Inheritance is defined statically while object Composition is defined dynamically. struct Base { id: f32, thing: f32, } struct Inherit { use Base::id x: f32, y: f32, } in that case Inherit would only have "id" and not "thing". Composition Over Inheritance. Unlike composition, private inheritance can enable the empty base optimization. 3 Answers. “has-a”). Inheritance among concrete types of DTOs is a bad practice. for example you could pass a stack to a function that takes a list and iterates over it. Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should favor polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) over inheritance from a base or parent class. It uses two main techniques for assembling and composing functionality into more complex ones, sub-typing and object composition. Composition over inheritance. The case your advice actually warns against is doing something like: class MasterChecker: public DiskChecker, public TemperatureChecker where inheritance is abused to aggregate the base class subobjects. When you establish an. Stack, which currently extends java. Examples: abuse of inheritance. 極端な話、例えば、親クラスと子クラスを開発している人が別々だった場合、 継承をしてしまうと名前空間がごっちゃになり、責任の分解点が曖昧になってしまいます。In C++, it is possible to inherit attributes and methods from one class to another. Leaking. By the end of this article, you. Stephen Hurn has a more eloquent example in his articles “Favor Composition Over Inheritance” part 1 and. As you are asking for a technique/design pattern, the term "composition over inheritance" fits best here I think. Composition is often preferred over inheritance because it promotes code. Inheritance Examples. Virtual inheritance. be concerned with or responsible for as little as possible. Composition involves a "has-a" relationship between. e. Lets take a look at one of the "classical" diagrams for proxy pattern (from wiki ): I would argue that "If proxy class should implement all of the methods of original class" statement is not true - the proxy class should implement all of the "contract" methods ( Subject interface) and it hides the implementation detail i. For example. A class can be created once and it can be reused again and again to create many sub-classes. (There isn't even always cost to calling a virtual member). For an id-expression, name lookup begins in the class scope of this; for a qualified-id, name lookup begins in the scope of the nested-name-specifier. Inheritance is beneficial because it allows you to avoid writing the same classes over again, thereby saving you time and effort. You cannot do multiple inheritance in C# because it is not supported like C++. Besides that, inheritance is one of the most effective ways to break encapsulation in C++ (second only to friendship), so its use kind of contradicts the 'maintain encapsulation' requirement from the question title. On the other hand, I've never found a place where we have used inheritance where I couldn't have used some other construct instead. Inheritance: a class may inherit - use by default - the fields and methods of its superclass. Sorted by: 8. Composition over Inheritance 意为优先考略组合,而不是继承。有些程序员没懂,有些程序员把它奉为真理与黄金法则。 前日在做游戏开发(和我白天的工作无关,兴趣爱好而已),在对游戏对象建模时,我对这句话有了新的理解。Composition并不总是比Inheritance好。Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. That's why it exists. When you only want to "copy" functionality, use delegation. While Composition gives the owner ship to the created object. With composition, it's easy to change behaviour on the fly with Dependency Injection / Setters. Composition. A shape, a triange, an equilateral triangle. – jscs. ” You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). Mixins are really just a way to do inheritance. Note that at least for this example, the CompositionRobot is usually considered to be the better approach, since inheritance implies an is-a relationship, and a robot isn't a particular kind of Arms and a robot isn't a particular kind of Legs (rather a robot has-arms and has-legs ). @Jim: std::vector's interface is quite huge, and when C++1x comes along, it will greatly expand. 1. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. But, that can sometimes lead to messy code. Like Inheritance, Composition is a concept in object-oriented programming that models the relationship between two classes. Composition comes in handy if you wanted something like logging; a task perhaps performed by the player class, but not directly related to the player. Inheritance needs to be used very carefully. Step 1: C c is default initialization. We group the "inheritance concept" into two categories: derived class (child) - the class that inherits from another class. You should prefer inheritance when inheritance is more appropriate, but prefer composition when composition is more appropriate. ” How then should the implementation be shared? Further thoughts. Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a particular problem. Changing other people's code always has a risk of introducing bugs because you may not fully understanding how the code works. Implementing inheritance in C++: For creating a sub-class that is inherited from the base class we have to follow the below syntax. Abstract classes or interfaces are only useful with inheritance. For example, a car is a kind of vehicle. . However in Inheritance, the base class is implicitly contained in the derived class. You can use it to declare a test class like. The First Approach aka Inheritance. it has no non-static data members other than bit-fields of size 0, no virtual functions, no virtual base classes, and no non-empty base classes), it will not contribute to the size of. Say we do have some base logic we want all discounts to apply and we put it in a BaseDiscount class as you suggest. In Go, composition is favored over inheritance. g. Composition over inheritance. If you are not sure whatever or not composition provides better reusability, "Prefer composition over inheritance" is a good heuristic. , class Foo : private Bar { public: //. Apr 22, 2013 at 23:13 @RobertHarvey: +1. I understand the advantages of composition over inheritance. Field: a named property of some type, which may reference another object (see composition) Method: a named function or procedure, with or without parameters, that implements some behavior for a class. Chapter 1 is a discussion of object-oriented design techniques, based on the authors' experience, which they believe would lead to good object-oriented software design, including: "Program to an interface, not an implementation. While they often contain a. So let’s define the below interfaces:Composition. Here is an example of what I would like to achieve :Composition over Inheritance is a principle in object-oriented programming that suggests that classes should achieve polymorphism through composition rather than through inheritance. I would like to achieve the polymorphic behavior through composition , instead of multilevel inheritance. Pros: Reusable code, flexibility, loosely coupled; Cons: Harder to understand; We don’t mean that inheritance is a bad thing, it’s great and we will still need and use inheritance. Since AbstractBase is, as the name suggests, abstract - you cannot hold one by value. over 'core'. Function composition is the process of applying a function to the output of another function. – Bart van Ingen Schenau. In C++, this is wrong. Composition over inheritance is a principle in object-oriented programming that suggests prioritizing the use of composition to achieve polymorphic behavior and code reuse, instead of relying. . The purpose of composition is obvious: make. In object-oriented programming (OOP),. The point of the composite pattern is that a Leaf object represents the simple case, a Composite object represents the complex case, and client code can treat both cases the same. class A : private B { virtual int doMethodA (); };Inheritance: For any bird, there are a set of predefined properties which are common for all the birds and there are a set of properties which are specific for a particular bird. Composition is in contrast to inheritance, it enables the creation of complex types by combining objects (components) of other types, rather than inheriting. 1. As your example demonstrates, interfaces are often a useful tool for using composition instead of inheritance. To be more concrete: use inheritance to model "is-a" relations. In object-oriented programming, inheritance, and composition are two fundamental techniques for creating complex software systems. You do composition by having an instance of another class as a field of your class instead of extending. (That’s not always the case: in. There's all sorts written on this subject. In C++, a virtual base class is used to avoid the “dreaded diamond problem” that arises when multiple inheritance is involved. Oct 13, 2013 at 14:12. Sử dụng Composition để thay thế Inheritance. a Campaign has a Client. The syntax for composition is obvious, but to perform inheritance there’s a new and different form. “Favor object composition over class inheritance” The Gang of Four, “Design Patterns: Elements of R. " Public inheritance allows derived classes to access public members of the abstract class, while private inheritance hides them. Composition is better, and using composition over private inheritance is better in my opinion. First of all, the alternative for composition is private inheritance (and not public one) since both model a has-a relationship. Is it fine to violate Composition Over Inheritance when necessary? Hot Network Questions If someone is volunteering information does that mean they are being transparent?UE4 does not allow multiple inheritance from UObject-based classes (i. Then, use black box code reuse, instead, a. Let's. Multiple Inheritance: Subclass inherited. Composition and/or aggregation usually provide as good or better. Apr 10, 2017 at 16:17. In this video, you can learn the difference between Composition and Inheritance in object oriented programming languages. Example 1: A Company is an aggregation of People. : Apple (derived class) is a Fruit (base class), Porsche is a Car etc. They are the building blocks of object oriented design, and they help programmers to write reusable code. C++ has ‘multiple inheritance’, JAVA has a single class inheritance,. Implementation inheritance has two areas of difficulty: the fragile base class problem and the static nature of inheritance relationships. . It is known as object delegation. Most of the references I've found to private inheritance are poor uses, and I agree that it is rarely. Composition is a “has-a” relationship, used to design a class on what it does. Composition over Inheritance. 1) When the class than you want to use is abstract (you cannot use aggregation). There's no choice here, and the advice didn't say you should never use inheritance even when composition isn't an alternative. You cannot change. Object composition can promote code reuse because you can delegate implementation to a different class, and include that class as a member. g. 1) Traits don't avoid forwarding functions with composition because traits work independently from composition. For example, Here, the Dog class is derived from the Animal class. Strategy corresponds to "some changeable algorithm" in terms of DDD, thus has real impact on domain. Like I stated before, I want the knowledge that B is a superset of A to be an implementation detail. In lack of a better term, a Interface is "more. Then you have interfaces or (depending on the language) multiple inheritance. use aggregation if you want to model "has-a" and "is implemented as a. Highly recommended reading, by the way. use interface segregation for the type you refer to, in order not to have a dependency on something you shouldn't need to care about. Now we want to add a second class, which is a 'specialisation' of A but has additional data which relates to the data in A. This is because of a limitation of the CLR. Objective C allows you to forward messages to another object, probably other message based languages like Smalltalk can do it too. I'm not a C++ programmer, so I have no idea what code generation tools are available to you. hiding the unwanted methods one by one is tedious). If inherited is a class template itself, sometimes need to write this->a to access members, which is. For example,. Correct me if I'm wrong, but composition is an alternative to inheritance. Learn more…. . The subclass uses only a portion of the methods of the superclass. E. Meyers effective C++ : Item 20: Avoid data members in the public interface. With inheritance, we get a tight coupling of code, and changes in the base class ripple down the hierarchy to derived classes. Money ), with all of its members. We cover how to instantiate a class instance object inside another class to create a loosely coupled relationship. You may want to prefer inheritance over composition when you want to distinguish semantically between "A is a B" and "A. Composition is a way of building complex objects by combining smaller, simpler objects. Modernize how you debug your Rust apps — start monitoring for free. What are MVP and MVC and what is the difference?When you inherit, you are saying, “This new class is like that old class. Prefer composition over inheritance? 890. I would like to use composition and to write good forwarding methods for every possible overload (noexcept, const, volatile) using C++ capabilities. Simple rules: A "owns" B = Composition : B has no meaning or purpose in the system without A. In fact, to a great extent, implementation inheritance is simply interface inheritance + implicit delegation of all methods - it's simply a boilerplate reduction tool over interface inheritance. The implements in typescript only ensures that a class conforms to a sub-type (e. A quick search of this stackexchange shows that in general composition is generally considered more flexible than inheritance but as always it depends on the project etc and there are times when inheritance is the better choice. At first, it provided dynamic polymorphism. The biggest point of confusion and contention seems to be composition versus inheritance, often summarized in the mantra “favor composition over inheritance”. Share. g. 2. Inheritance is a limited form of composition. Mantras Considered Harmful As a heuristic, ‘favor composition over inheritance’ is okay, however, I am not a fan of mantras. Hello everyone, I am trying to understand composition versus inheritance in C++. . An alternative is to use “composition”, to have a single class. Interfaces cannot contain a default implementation the same way that a base class can. It's usually inferior to composition, but it makes sense when a derived class needs access to protected base class members or needs to redefine inherited virtual functions. Overloading is used when the same function has to behave differently depending upon parameters passed to them. Remember, prefer composition over inheritance. 1 Member name lookup determines the meaning of a name (id-expression) in a class scope (6. g. Mantras Considered Harmful As a heuristic, ‘favor composition over inheritance’ is okay, however, I am not a fan of mantras. "Composition over inheritance" is a short (and apparently misleading) way of saying "When feeling that the data (or behaviour) of a class should be incorporated into another class, always consider using composition before blindly applying inheritance". However, for properties specifically, you're a bit stuck. Inheritance is a feature or a process in which, new classes are created from the existing classes. The main purpose of inheritance in Object Orientated Programming (OOP) is to give the user ability to change the behavior of the libraries, without actually changing already working and debugged code. You mentioned that DLAContainer has a number of other. You're holding a dangling reference. Inheritance is tightly coupled whereas composition is loosely coupled. Composition is a "has-a". Vector. Composition over inheritance in OOP is the principle that classes should achieve polymorphism and code reuse by composition, instead of through inheritance. In c# you can inherit many interfaces, but only one base class. Inheritance is the system in object oriented programming that allows objects to support operations defined by anterior types without having to provide their own definition. In this article, you’ll explore inheritance and composition in Python. Inheritance breaks encapsulation, a change in the parent class can force a change in the sub classes, while Composition respects the interface. For sample, you could have a base class. Composition in Java. In this tutorial, we’ll explore the differences. For example, in a summary of C++ in his book on Objective C, Brad Cox actually claimed that adding multiple inheritance to C++ was impossible. 2) When you want to use protected methods. Some important advantages of inheritance are as follows: Inheritance allows the user to reuse existing code in many situations. It's about knowledge, not code. 0. The composition is achieved by using an instance variable that refers to other objects. ”. Aggregation and Composition. Perhaps it adds additional metadata relating to the entries in A. base class (parent) - the class being inherited from. like C++) inheritance is the only practical way to say "this object implements this interface". With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. A book that would change things. a = 5; // one less name. The pithiest way to sum it up is: Prefer composition. Composition over inheritance [A] Composition over inheritance is generally a good rule to follow,[B] but there are some cases where inheritance is a mustYour conclusion in B implies that you are understanding A to mean "composition should always be used instead of inheritance". By leveraging composition,. Composing Functions. Combination: Combining both classes and creating a new class containing all the members A and B had. That's a guideline, not a "principle," and certainly not an absolute commandment. mixin and multiple inheritance have the same form. Composition allows for greater flexibility in modifying objects, while inheritance provides a more rigid and hierarchical structure. While they often contain a. However, that is somewhat wasteful b/c the general case would be CompositeParameters which contained just one Parameter. 4. Rather, I directly saw 2 or 3 different ways to implement Composite Design Pattern in Modern C++. If the base class need to be instantiated then use composition; not inheritance. Templates on the other hand are "horizontal" and define parallel instances of code that knowns nothing of each other. There are situations when inheritance should be favored over composition, and the distinction is much more clear cut than a matter of style. Eg. 4. 1. Object Delegation means using the object of another class as a class member of another class. core guidelines. When books and articles refer to "prefer composition over inheritance", they are specifically not talking about interfaces; they're talking about state and behaviour inherited from a base class. In this tutorial we learn an alternative to inheritance, called composition. Consider the differences and similarities between the classes of the following objects: pets, dogs, tails, owners. We can add another component to accommodate any future change instead of restructuring the inheritance hierarchy. And please remember "Prefer composition. So, there are many rules to follow, such as 'composition over inheritance' one for c++. Therefore, in the object-oriented way of representing the birds, we. "which has destroyed the benefits that the composition pattern was giving me. Everyone have see that classic example of Shape, Rectangle extends Shape and so forth. Over on StackOverflow, I was asked if I could come up with an example where private inheritance would be preferred to composition (in C++). I* anXYZ = new Z ( new Y ( new X ( new A. 19]: ". Further, you can avoid the forward declaration in the first example by just defining your classes in reverse order. All that without mentioning Amphibious. With the use of MinGW 4. The fact that it has been overused doesn't mean that it doesn't have legitimate uses. This means that the default ctor C::C () will be used. This applies, in spades, to third party software. Interfaces should handle one responsibility only. I am especially interested how private inheritance and composition differ on a much deeper technical level. e. Though it is possible to mimic inheritance using composition in many situations, it is often unwieldy to do so. Classes and objects created through composition are loosely coupled, which. It is a special type of aggregation (i. TEST_CLASS (className) { TEST_METHOD (methodName) { // test method body } // and so on } That's it. When you inherit, you are saying, “This new class is like that old class. An alternative is to use “composition”, to have a single class. prefer composition over inheritance ,and so on known articles about the abuse of inheritance. In OO design, a common advice is to prefer composition over inheritance. The question being: Am I going against the "Composition over Inheritance" rule? If so, is this perfectly fine, or is there a way to adhere to CoI while achieving code reuse? Note: I don't need or want polymorphism--when I use run(), I'm always calling it using the concrete (Cat/Dog/Sloth) classes, instead of the base Animal class. Use inheritance only if the base class is abstract. 25. This is a common approach in a lot of programming languages and. With inheritance, we get a tight coupling of code, and changes in the base class ripple down the hierarchy to derived classes. prefer composition over inheritance, because inheritance is always a strong coupling (any change in the parent class might require a change in all the child classes) and furthermore, it's defined at compile time. But inheritance has. I found this statement from the gang of four's "Design Patterns" particularly odd; for some context, the authors are comparing inheritance versus composition as reuse mechanisms [p. In C++, we have private and multiple inheritance, which enables us to add private methods to classes by just inheriting from the class declaring these methods. We see the following relationships: owners feed pets, pets please owners (association) a tail is a part of both dogs and cats (aggregation / composition) a cat is a kind of pet (inheritance / generalization) The figure below shows the three types of. Class inheritance reflects. They are absolutely different. Use generalization when you have a class that shares common properties with a set of objects, but can also have other diferent properties or behavior. As Rust has a comprehensible generics system, generics could be used to achieve polymorphism and reusing code. Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Inheritance đại diện cho mối quan. Inheritance is about the relationship of class and class. This is inheritance, when the Child class is created the parent is created because the child inherits from parent. Scala 3 added export clauses to do this. 1. The Second Approach aka Composition. has_those_data_as_a_member memb; memb. – Herb Sutter & Andrei Alexandrescu. Code reusebility: Các lớp con có các properties và functions của lớp cha -> Có thể giảm sự duplicate code giữa các lớp con bằng cách đặt các phần code bị duplicate vào lớp cha. Just seems like a very odd case. The examples assume that the reader knows what base() does in C#, and how it's different from typical C++ approaches, and thus do nothing to illustrate actual differences between. It means use inheritance appropriately. The key part is that you don't want to expose the non-const vector methods, so inheritance isn't an option (because: 1.