You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/cpp-learn.md
+235Lines changed: 235 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3394,3 +3394,238 @@ int main()
3394
3394
### 20.7. Multiple inheritance
3395
3395
- C++ provides the ability to do multiple inheritance. Multiple inheritance enables a derived class to inherit members from more than one parent.
3396
3396
- Avoid multiple inheritance unless alternatives lead to more complexity.
3397
+
3398
+
## 21. Virtual Functions - Polymorphism
3399
+
### 21.1. Pointers and references to the base class of derived objects
3400
+
-**Pointers, references, and derived classes:** We can not only assign `Derived pointers and references` to `Derived objects`, but also assign `Base pointers and references` to `Derived objects`.
3401
+
> **chỉ có thể gọi các hàm/thành viên có trong Base -> need virtual**
3402
+
> Derived chứa phần dữ liệu của Base ở đầu đối tượng, nên con trỏ Base* có thể trỏ đến vùng đó mà không sai về mặt địa chỉ.
3403
+
> Compiler đảm bảo phần đầu tiên của Derived có cùng layout với Base.
3404
+
- e.g.
3405
+
```cpp
3406
+
#include<iostream>
3407
+
3408
+
intmain()
3409
+
{
3410
+
Derived derived{ 5 };
3411
+
Derived& rDerived{derived};
3412
+
Derived* rDerived{&derived};
3413
+
3414
+
3415
+
// These are both legal!
3416
+
Base& rBase{ derived }; // rBase is an lvalue reference (not an rvalue reference)
3417
+
Base* pBase{ &derived };
3418
+
3419
+
std::cout << "derived is a " << derived.getName() << " and has value " << derived.getValue() << '\n';
3420
+
std::cout << "rBase is a " << rBase.getName() << " and has value " << rBase.getValue() << '\n';
3421
+
std::cout << "pBase is a " << pBase->getName() << " and has value " << pBase->getValue() << '\n';
3422
+
3423
+
return 0;
3424
+
}
3425
+
3426
+
```
3427
+
-**Use for pointers and references to base classes:** let us pass any derived object to a single function instead of writing one for each class.
3428
+
However, since `rBase` is an `Based reference`, calling `Base.fucntion()` runs `Base::fucntion()` unless the function is `virtual`.
3429
+
3430
+
### 21.2. Virtual functions and polymorphism
3431
+
-**Virtual functions:**
3432
+
- is a special type of member function that, when called, resolves to the `most-derived version` of the function for the actual type of the object being referenced or pointed to.
3433
+
- is considered a match if it has the same signature (name, parameter types, and whether it is const) and return type as the base version of the function. Such functions are called overrides.
3434
+
- to make a function virtual, simply place the `virtual` keyword before the function declaration.
std::cout << "rBase is a " << rBase.getName() << '\n';
3459
+
3460
+
return 0;
3461
+
}
3462
+
3463
+
// RESULT: rBase is a Derived
3464
+
```
3465
+
-**Return types of virtual functions:** the return type of a virtual function and its override must match, otherwise the compilation will fail.
3466
+
-**Do not call virtual functions from constructors or destructors:** because of the class hadn’t even been created yet
3467
+
3468
+
-**polymorphism** refers to the ability of an entity to have multiple forms (the term “polymorphism” literally means “many forms”)
3469
+
-**Compile-time polymorphism** refers to forms of polymorphism that are resolved by the compiler. These include **function overload resolution**, as well as **template resolution**.
3470
+
-**Runtime polymorphism** refers to forms of polymorphism that are resolved at runtime. This includes virtual function resolution.
3471
+
3472
+
### 21.3. The override and final specifiers, and covariant return types
3473
+
-**The override specifier:** the `override` specifier can be applied to any virtual function to tell the compiler to enforce that the function is an override.
3474
+
-`override` specifier is placed at the end of a member function declaration.
3475
+
- If a member function is const and an override, the const must come before override.
3476
+
- We should use the virtual keyword on virtual functions in a base class.
3477
+
3478
+
-**The final specifier:** The final specifier can be used to tell the compiler to enforce a virtual function to be unable to override.
3479
+
- used in the same place the override specifier is
0 commit comments