아래의 글은 제가 재학중인 UCLA의 CS 32: Data Structures의 렉쳐를 들으며 작성하는 글입니다.
Rather than to write a new class, it'd be nicer to inherit all of methods/ data of an existing, related class.
We can just add necessary members to a new subclass based on its super class.
Anytime we have such a relationship:
"A is a type of B", C++ inheritance is warranted.
- Reuse
Reuse is when you write code once in a base class and reuse the same code in your derived classes (to reduce duplication).
- Extension
Extension is when you add new behaviors (member functions) or data to a derived class that were not present in a base class.
- Specialization
Specialization is when you redefine an existing behavior (from the base class) with a new behavior (in your derived class).
Every public method in the base class is automatically resued/exposed in the derived class (just as if it were defined there).
And, as such, they may be used normally by the rest of your program.
And of course, your derived class can call them, too.
If you'd like your child class to reuse private member functions of the parent class, but don't want the rest of your program to use them, make them protected
instead of private
in the blase class.
However, never make your member variables protected or public.
If you expose your member variables to a child class, you violate encapsulation.
Extension: the process of adding methods or data to a derived class
All public extensions may be used normally by the rest of the program.
Specialization: overriding existing functions from the parent class in your child class
There should be virtual
keyword in both the original and replacement functions for clarity.
Use virtual
only when you intend to override in your child classes
Redefinition of a function with virtual
hides the base version of the function when using your subclass (child class).
- Solution:
If you already redefined your base function withvirtual
, but you watn to call the base class's version of the method,
you can do so by usingbaseclass::method()
.
In the above example, lily.getExcitedAboutCS()
will print (cout) "go bruins!".
Or you can just do lily.Student::cheer()
.
Q. When you define a derived object, which has both superclass and subclass parts, which one is constructed first?
A. C++ always constructs the base part fist, then the derived part second.
Order:
1. Call Robot's constructor
2. Call Battery's constructor (for m_bat object)
3. Call ShieldGenerator's constructor (for m_sg object)
4. Call ShieldedRobot's constructor
Q. When you destruct a derived object, which has both superclass and subclass parts, which one is destructed first?
A. C++ destructs the derived part first, then the base part second.
Order
1. Call ShieldedRobot's destructor
2. Call ShieldedGenerator's destructor
2. Call Robot's destructor
3. Call Battery's destructor
If a superclass requires parameters for construction, then you must add an initializer list to the subclass constructor!
Add :baseClass(parameter)
If there is a member object,
memberObject(parameter)
Instead of fixing Duck's weight as 2 as above, we can pass on lbs
to Animal class when creating Duck object.
Without defining assignment operators, it'll be a shallow copy.
Shallow copy: Copies the base data, then derived data
If your base and derived classes have dynamically allocated member variables, you must define assignment operators and copy constructors for the base class and also special versions of these functions for the derived class.