CS 32 Lecture 6. Inheritance

Jene Hojin Choi·2021년 7월 22일
0

C++

목록 보기
4/17
post-thumbnail

아래의 글은 제가 재학중인 UCLA의 CS 32: Data Structures의 렉쳐를 들으며 작성하는 글입니다.


📝 Inheritance

Why do we use it?

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.

When do we use it?

Anytime we have such a relationship:
"A is a type of B", C++ inheritance is warranted.

3 Uses of Inheritance

  • 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).

✔️ Reuse

Public method in parent class is automatically reused

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.

Protected can be used instead private for some cases

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.

Member variables should be private

However, never make your member variables protected or public.
If you expose your member variables to a child class, you violate encapsulation.

Summary

✔️ Extension

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

keyword virtual

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

Method visibility - virtual hides base version

Redefinition of a function with virtual hides the base version of the function when using your subclass (child class).

What if we want to use the hidden base-class methods?

  • Solution:
    If you already redefined your base function with virtual, but you watn to call the base class's version of the method,
    you can do so by using baseclass::method().

In the above example, lily.getExcitedAboutCS() will print (cout) "go bruins!".
Or you can just do lily.Student::cheer().

✔️ Inheritance and Construction, Destruction

Order of construction

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

Order of destruction

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 Robot is a subclass of Class Machine

✔️ Inheritance and Initializer Lists

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)

We can pass on parameters to base class

Instead of fixing Duck's weight as 2 as above, we can pass on lbs to Animal class when creating Duck object.

✔️ Inheritance and Assignment Operators

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.

0개의 댓글