background preloader

OOP_Design

Facebook Twitter

C++ Abstract class operator overloading and interface enforcement question. Design - Tips for avoiding second system syndrome. Language agnostic - Inheritance vs. Aggregation. 10.2 — Composition. Object composition In real-life, complex objects are often built from smaller, simpler objects.

10.2 — Composition

For example, a car is built using a metal frame, an engine, some tires, a transmission, a steering wheel, and a large number of other parts. A personal computer is built from a CPU, a motherboard, some memory, etc… Even you are built from smaller parts: you have a head, a body, some legs, arms, and so on. This process of building complex objects from simpler ones is called object composition. Broadly speaking, object composition models a “has-a” relationship between two objects. In C++, you’ve already seen that structs and classes can have data members of various types (such as fundamental types or other classes). Object Composition is useful in a C++ context because it allows us to create complex classes by combining simpler, more easily manageable parts.

Types of object composition There are two basic subtypes of object composition: composition and aggregation. Composition More examples. C++: Shallow vs Deep Copies. A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want.

The default copy constructor and assignment operator make shallow copies. A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences. If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.

A class that requires deep copies generally needs: Object-Oriented C++ Class Design - CProgramming.com. Understanding Interfaces When you're designing a class in C++, the first thing you should decide is the public interface for the class.

Object-Oriented C++ Class Design - CProgramming.com

The public interface determines how your class will be used by other programmers (or you), and once designed and implemented it should generally stay pretty constant. You may decide to add to the interface, but once you've started using the class, it will be hard to remove functions from the public interface (unless they aren't used and weren't necessary in the first place). But that doesn't mean that you should include more functionality in your class than necessary just so that you can later decide what to remove from the interface. If you do this, you'll just make the class harder to use. At the same time, just because adding methods to the public interface (probably) won't break anything that doesn't mean that you should start off with a tiny interface.

The public interface, then, should remain as constant as possible. Inheritance and Class Design.