Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]

Similar presentations


Presentation on theme: "CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]"— Presentation transcript:

1 CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0] = new Clock( 14, 12, 0 ); clockPtr[1] = new AlarmClock( 8, 54, 12 ); clockPtr[0]->display(); cout << endl << endl; clockPtr[1]->display(); cout << endl; Output: 14:12:00 08:54:12 Alarm is not set Alarm time 00:00:00

2 CPSC 252Inheritance II Page 2 Interestingly, the output is exactly what we wanted. Even though both pointers are declared to be pointers to Clock objects, the statement: clockPtr[1]->display(); invokes AlarmClock::display(). In order to understand why this occurs, we need to introduce some new terminology. Static binding or compile-time binding causes the compiler to determine which version of an overridden function to call when the program is compiled. When the determination of which version of an overridden function to call occurs while the program is running we call this dynamic binding or run-time binding.

3 CPSC 252Inheritance II Page 3 Suppose that objectPtr is a pointer to a base class and that objectRef is a reference to a base class. Further suppose that func() is overridden in the derived class. When an expression of the type objectPtr->func() or objectRef.func() is encountered: - if func() is declared to be virtual then the type of object pointed to by objectPtr (or objectRef ) determines which version of func() to call. This determination must occur at run-time - dynamic binding. - if func() is not declared to be virtual then the type of pointer (or reference variable) determines which version of func() to call. This determination occurs at compile- time – static binding.

4 CPSC 252Inheritance II Page 4 Note: dynamic binding occurs only when a pointer or reference variable is used to invoke a virtual member function. If we assign an object of the derived class to a variable of the base class, then we get the object-slicing that we talked about previously.

5 CPSC 252Inheritance II Page 5 Returning to our Clock example, notice that the display() function is declared to be virtual in the base class. Hence, dynamic binding occurs when the statement: clockPtr[1]->display(); is encountered. Even though clockPtr was declared to be a pointer to a Clock object, it is AlarmClock::display() that is invoked! This occurs because clockPtr is actually pointing to an object of type AlarmClock. The fact that a pointer to the base class can in fact point to any object in its inheritance hierarchy is called polymorphism which literally means, many forms. All object-oriented programming languages must support polymorphism.

6 CPSC 252Inheritance II Page 6 Inheritance, Pointers and Type Casting Consider the statement: clockPtr[1] = new AlarmClock( 17, 34, 0 ); We are assigning the address of an AlarmClock object to a pointer to a Clock – this is OK, an AlarmClock is-a Clock. It is possible, but not safe, to cast a pointer to a Clock object into a pointer to an AlarmClock object as follows: AlarmClock* myAlarmClockPtr; myAlarmClockPtr = ( AlarmClock* ) new Clock( 7, 0, 0 ); Now we have to be careful not to do something like: myAlarmClockPtr->setAlarm( true ); because myAlarmClockPtr is actually pointing to a Clock object.

7 CPSC 252Inheritance II Page 7 Inheritance, Pointers and Type Casting This kind of assignment is only safe if the object really is an AlarmClock, which it could be: AlarmClock* myAlarmClockPtr; Clock *myClockPtr; myClockPtr = new AlarmClock(7, 0, 0); // This “forgets” that we have an AlarmClock // and allows us to treat it as a Clock myAlarmClockPtr = ( AlarmClock* ) myClockPtr; // This “gets back” the AlarmClock


Download ppt "CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]"

Similar presentations


Ads by Google