Inheritance The Fundamental Functions Lecture 24 Mon Mar

  • Slides: 16
Download presentation
Inheritance: The Fundamental Functions Lecture 24 Mon, Mar 26, 2007 1/30/2022 Inheritance 1

Inheritance: The Fundamental Functions Lecture 24 Mon, Mar 26, 2007 1/30/2022 Inheritance 1

Inheritance of Constructors n A derived-class constructor will automatically invoke the base-class default constructor,

Inheritance of Constructors n A derived-class constructor will automatically invoke the base-class default constructor, unless instructed otherwise. n The base-class constructor is invoked before the derived-class constructor is executed. n The derived-class constructor may invoke a specific base-class constructor. 1/30/2022 Inheritance 2

Inheritance of Constructors n Suppose we create a List class and then derive an

Inheritance of Constructors n Suppose we create a List class and then derive an Array. List class and a Linked. List class from it. List Array. List 1/30/2022 Linked. List Inheritance 3

Inheritance of Constructors n Since Array. List and Linked. List have m. Size in

Inheritance of Constructors n Since Array. List and Linked. List have m. Size in common, we could put m. Size in the List class List {int m. Size; } class Array. List {int* element; } 1/30/2022 Inheritance class Linked. List {Node* head; } 4

Inheritance of Constructors n When we construct an Array. List, what happens? n The

Inheritance of Constructors n When we construct an Array. List, what happens? n The Array. List constructor will first call a List constructor which will initialize m. Size. Then it will initialize element. 1/30/2022 Inheritance 5

Inheritance of Constructors n Which List constructor will it call? n Unless we specify

Inheritance of Constructors n Which List constructor will it call? n Unless we specify otherwise, it will call the default List constructor. 1/30/2022 Inheritance 6

Invoking the Base Class Constructor n How do we specify a different constructor? n

Invoking the Base Class Constructor n How do we specify a different constructor? n We do it through an initializer. Array. List(parameters) : List(parameters) {body of the Array. List constructor} 1/30/2022 Inheritance 7

Invoking the Base Class Constructor Array. List(int sz, int value) : List(sz) { if

Invoking the Base Class Constructor Array. List(int sz, int value) : List(sz) { if (m. Size == 0) element = NULL; else { element = new int[m. Size]; for (int i = 0; i < m. Size; i++) element[i] = value; } return; } 1/30/2022 Inheritance 8

Inheritance of Destructors n The derived-class destructor automatically invokes the base-class destructor. n The

Inheritance of Destructors n The derived-class destructor automatically invokes the base-class destructor. n The base-class destructor is invoked after the derived-class destructor is executed. 1/30/2022 Inheritance 9

Inheritance of the Assignment Operator n The automatic assignment operator invokes the assignment operator

Inheritance of the Assignment Operator n The automatic assignment operator invokes the assignment operator for the base class. n A programmer-defined assignment operator must copy the base-class members. n This is a problem if the base-class members are private. 1/30/2022 Inheritance 10

Inheritance of the Assignment Operator n One can use the scope operator : :

Inheritance of the Assignment Operator n One can use the scope operator : : to invoke the assignment operator of the base class A {private: int a; }; class B : class A {private: int b; }; B& B: : operator=(const B& x) { a = x. a // Illegal A: : operator=(x); // Legal b = x. b; } 1/30/2022 Inheritance 11

Inheritance of the Assignment Operator n See the tip about the assignment operator. 1/30/2022

Inheritance of the Assignment Operator n See the tip about the assignment operator. 1/30/2022 Inheritance 12

Inheritance of the Assignment Operator n Suppose we create a class Array. Listw. Pos,

Inheritance of the Assignment Operator n Suppose we create a class Array. Listw. Pos, which is an Array. List that keeps track of a current position. n Array. Listw. Pos has one additional data member n 1/30/2022 int curr. Pos – the current position in the list. Inheritance 13

Inheritance of the Assignment Operator Array. Listw. Pos& operator=(const Array. Listw. Pos& list) {

Inheritance of the Assignment Operator Array. Listw. Pos& operator=(const Array. Listw. Pos& list) { if (this != &list) { Array. List: : operator=(list); curr. Pos = list. curr. Pos; } return *this; } 1/30/2022 Inheritance 14

Example: Inheritance n Header files n n n n 1/30/2022 person. h man. h

Example: Inheritance n Header files n n n n 1/30/2022 person. h man. h woman. h father. h mother. h marriedman. h marriedwoman. h Inheritance 15

Example: Inheritance n Programs n n 1/30/2022 Default. People. cpp Family. Tree. cpp The.

Example: Inheritance n Programs n n 1/30/2022 Default. People. cpp Family. Tree. cpp The. First. Family. cpp Changing. Places. cpp Inheritance 16