ece150: add inheritance
This commit is contained in:
parent
9806ca4e94
commit
42192bb36d
@ -129,6 +129,12 @@ Arrays are contiguous in memory and default to 0 when initialised. If field init
|
||||
|
||||
Because arrays do not check bounds, `array[n+10]` or `array[-5]` will go to the memory address directed without complaint and ruin your day.
|
||||
|
||||
| Pros | Cons |
|
||||
| --- | --- |
|
||||
| Random access is $O(1)$ | $O(n)$ push front |
|
||||
| | Fixed size and unused allocated memory |
|
||||
| | Concatenation is slow |
|
||||
|
||||
### Local arrays
|
||||
|
||||
Local arrays cannot be assigned to nor returned from a function. If an array is marked `const`, its entries cannot be modified.
|
||||
@ -592,3 +598,57 @@ If a move occurs and the compiler determines that the original object is no long
|
||||
During construction, default initialisation picks the one with the fewest parameters if ambiguous. Parameters passed by value are **copied** by reference via the copy constructor.
|
||||
|
||||
Much like statically allocated arrays, dynamically allocated arrays also automatically dereference when accessed by index.
|
||||
|
||||
## Linked lists
|
||||
|
||||
Dynamic memory allocation for many objects instead of one like arrays is slow.
|
||||
|
||||
## Inheritance
|
||||
|
||||
All member functions and the destructor must be `virtual` functions if they can be inherited.
|
||||
|
||||
```cpp
|
||||
class Base {
|
||||
public:
|
||||
Base();
|
||||
virtual ~Base();
|
||||
virtual Base get_base() const;
|
||||
virtual void set_base();
|
||||
virtual void do_base() const;
|
||||
```
|
||||
|
||||
A class that inherits another should contain `public <Base>` after the name of the class. Overriden functions must have `override` if they should have the same type signature. Otherwise, they reference the base function.
|
||||
|
||||
```cpp
|
||||
class ExtendedBase: public Base {
|
||||
public:
|
||||
ExtendedBase();
|
||||
Base get_base() const override;
|
||||
void set_base() override;
|
||||
virtual void do_base() override;
|
||||
}
|
||||
```
|
||||
|
||||
Functions can be overriden completely ignoring the function signature by excluding the `override` keyword.
|
||||
|
||||
The base class's functions implicitly refer to the current class, so they can be directly called:
|
||||
|
||||
```cpp
|
||||
void set_base() override {
|
||||
Base::set_base();
|
||||
}
|
||||
```
|
||||
|
||||
### protected
|
||||
|
||||
The `protected` access keyword only allows the original class as well as classes that extend the original one to access it.
|
||||
|
||||
### Extending exceptions
|
||||
|
||||
Exceptions should have two constructors: one for a char array pointer and another a string for the exception message, as well as any additional parameters as desired. The base exception constructor (not `std::exception` because that can't be instantiated`) should be called to do all of the base constructor things.
|
||||
|
||||
In addition, a `what()` function with the following signature should always be defined that cannot throw an exception, returning a C-style array.
|
||||
|
||||
```cpp
|
||||
char const *error::what() const noexcept;
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user