Machineboy空

Object-Oriented Data Structures # WEEK04 : Inheritance 상속 본문

Computer/자료구조

Object-Oriented Data Structures # WEEK04 : Inheritance 상속

안녕도라 2024. 2. 7. 10:59

 4.6 Inheritance

Inheritance allows for class to inherit all member functions and data from a base class into a derived class.

 

Generic to Specialized

A base class is a generic form of a speciallized, derived class.

without having to rewrite logic, 

모든 도형에는 길이가 있으며 이 길이는 여러 도형 간에 공유될 수 있으므로 generic / volume을 구하는 것은 cube 만의 specialized

 

Initialization

When a derived class is initialized, the derived class must construct the base class:

  • Cube must construct Shape
  • By default, uses default constructor
  • Custom constructor can be used with an initialized list

 

Access Control

 When a base class is inherited, the derived class:

  • Can access all public members of the base class
  • Can not access private members of the base class

Initializer List

The syntax to initialize the base class is called the initializer list and can be used for several purposes:

  • Initialize a base class
  • Initialize the current class using another constructor
  • Initialize the default values of member variables

 

one parameter constructor initialize private member variables without ever having to do anything in the code itself. it' s a way to clean up our code into reuse a lot of our code, 

 

As we build more and more data structures, we're going to start building small pieces that we can injerit from to build bigger and bigger systems and more complicated data structures without having to think of all of logic.