Download presentation
Presentation is loading. Please wait.
1
Inheritance: The Fundamental Functions
Lecture 24 Mon, Mar 26, 2007 2/17/2019 Inheritance
2
Inheritance of Constructors
A derived-class constructor will automatically invoke the base-class default constructor, unless instructed otherwise. The base-class constructor is invoked before the derived-class constructor is executed. The derived-class constructor may invoke a specific base-class constructor. 2/17/2019 Inheritance
3
Inheritance of Constructors
Suppose we create a List class and then derive an ArrayList class and a LinkedList class from it. List ArrayList LinkedList 2/17/2019 Inheritance
4
Inheritance of Constructors
Since ArrayList and LinkedList have mSize in common, we could put mSize in the List class. class List {int mSize;} class ArrayList {int* element;} class LinkedList {Node* head;} 2/17/2019 Inheritance
5
Inheritance of Constructors
When we construct an ArrayList, what happens? The ArrayList constructor will first call a List constructor which will initialize mSize. Then it will initialize element. 2/17/2019 Inheritance
6
Inheritance of Constructors
Which List constructor will it call? Unless we specify otherwise, it will call the default List constructor. 2/17/2019 Inheritance
7
Invoking the Base Class Constructor
How do we specify a different constructor? We do it through an initializer. ArrayList(parameters) : List(parameters) {body of the ArrayList constructor} 2/17/2019 Inheritance
8
Invoking the Base Class Constructor
ArrayList(int sz, int value) : List(sz) { if (mSize == 0) element = NULL; else element = new int[mSize]; for (int i = 0; i < mSize; i++) element[i] = value; } return; 2/17/2019 Inheritance
9
Inheritance of Destructors
The derived-class destructor automatically invokes the base-class destructor. The base-class destructor is invoked after the derived-class destructor is executed. 2/17/2019 Inheritance
10
Inheritance of the Assignment Operator
The automatic assignment operator invokes the assignment operator for the base class. A programmer-defined assignment operator must copy the base-class members. This is a problem if the base-class members are private. 2/17/2019 Inheritance
11
Inheritance of the Assignment Operator
One can use the scope operator :: to invoke the assignment operator of the base class. 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; } 2/17/2019 Inheritance
12
Inheritance of the Assignment Operator
See the tip about the assignment operator. 2/17/2019 Inheritance
13
Inheritance of the Assignment Operator
Suppose we create a class ArrayListwPos, which is an ArrayList that keeps track of a current position. ArrayListwPos has one additional data member int currPos – the current position in the list. 2/17/2019 Inheritance
14
Inheritance of the Assignment Operator
ArrayListwPos& operator=(const ArrayListwPos& list) { if (this != &list) ArrayList::operator=(list); currPos = list.currPos; } return *this; 2/17/2019 Inheritance
15
Example: Inheritance Header files person.h man.h woman.h father.h
mother.h marriedman.h marriedwoman.h 2/17/2019 Inheritance
16
Example: Inheritance Programs DefaultPeople.cpp FamilyTree.cpp
TheFirstFamily.cpp ChangingPlaces.cpp 2/17/2019 Inheritance
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.