ACtE0304 Features of object-oriented programming¶
Operator overloading¶
Operator overloading gives existing operators class-specific meaning.
General rules:
- at least one operand must be a user-defined type;
- built-in operator precedence and arity do not change;
- some operators cannot be overloaded.
Commonly overloaded operators:
| Category | Examples |
|---|---|
| Unary | +, -, !, ++, -- |
| Binary | +, -, *, /, ==, <, [] |
| Stream-like | <<, >> |
Operators that cannot be overloaded include . , .* , :: , ?: , and sizeof.
Member-versus-friend rule:
operator=,operator(),operator[], andoperator->must be non-static member functions;- arithmetic operators such as
operator*may be implemented as non-member friends when private access or symmetric conversions are useful.
Prefix/postfix distinction example:
The dummy int parameter marks postfix form.
Data conversion¶
Conversion may be controlled by:
- converting constructors taking one effective source argument;
- conversion functions such as
operator int() const.
Recognition cues:
- implicit conversions can cause surprising overload resolution;
expliciton constructors or conversion operators can prevent unwanted implicit conversion.
Inheritance forms¶
Inheritance supports code reuse by letting a derived class reuse accessible base-class behavior and extend or specialize it.
| Form | Shape |
|---|---|
| Single | one base -> one derived |
| Multiple | derived from more than one base |
| Multilevel | derived from a derived class |
| Hierarchical | several derived classes from one base |
| Hybrid | combination of forms |
| Multipath | one base reached through multiple inheritance paths |
Access-mode cue for public inheritance: "is-a" relationship is preserved at interface level.
Constructor/destructor order in inheritance¶
Rules:
- base-class constructor runs before derived-class constructor body;
- member subobjects are initialized before constructor body, in declaration order;
- destruction occurs in reverse order: derived destructor body first, then members, then base.
For multilevel inheritance:
- construction runs highest base -> intermediate bases -> most-derived class;
- destructor bodies run most-derived -> intermediate bases -> highest base.
Multipath ambiguity and virtual base¶
When a class inherits the same base through multiple paths, duplicated base subobjects can create ambiguity.
Virtual inheritance cue:
- use virtual base classes to share one common base subobject in a diamond hierarchy.
This is a language-mechanism fact, not a claim that every multiple-inheritance design should use it.
OOP-recognition traps¶
- Overloading and overriding are different: overloading changes parameter list within scope; overriding replaces virtual behavior in derived classes.
- Constructor order follows inheritance/member declaration rules, not the order written in the initializer list.
- Operator overloading cannot invent new operators.
OOP-features revision box¶
- Unary and binary operators can be overloaded for user-defined types.
- Overload resolution depends on parameter list, not return type alone.
- Single, multiple, multilevel, hybrid, and multipath inheritance are distinct shapes.
- Base constructors run before derived constructors.
- Destruction reverses construction order.
- Virtual inheritance addresses shared-base duplication in diamond patterns.