Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design Lecture 8: Polymorphism & C++ pointer Inheritance and polymorphism Type cast in C++ Pointer, dynamic memory, and pointer’s effect on class construction Dynamic binding and virtual function -- By Rossella Lau
Rossella Lau Lecture 8, DCO10105, Semester B, A sub class has all the properties of its base class Data members, no matter if it is private, protected, or public Function members, no matter if it is private or not The type of the base class, e.g., line 17 in DonalFamily.h Polymorphism – many types Same classes can play as different types (of ancestors) Same expressions can perform different operations Polymorphism
Rossella Lau Lecture 8, DCO10105, Semester B, Our Donald’s family version 1.1 Notes on the DonaldFamily.h The constructor of a sub class can use its parent’s constructor To identify a member in the parent class that is identical to its own member, use BaseClass:: to identify, e.g., line 34 scopePlayer2.cpp A parent’s function can also be identified through the instance of the child class by using BaseClass:: e.g. line 38
Rossella Lau Lecture 8, DCO10105, Semester B, Type cast in C++ To play the type of a parent class, one can use type cast in C++ static_cast ( variable ) Temporary change a variable’s type to ClassName e.g., line 42 in scopePlayer2.cpp Formal C uses ( ClassName ) variable const_cast ( variable ) Sometimes use to cast away the constant attribute of a parameter in order to get through the compiler reinterpret_cast ( variable ) Seldom use dynamic_cast ( variable ) usually apply to pointers and not recommended to use
Rossella Lau Lecture 8, DCO10105, Semester B, Static binding does not always work However, line 42 does not have any effect even though dewey pretends a Donald The type cast on line 42 is just a compiler binding, static binding, and dewey’s actual data type is still Dewey and it can’t really pretend Donald during execution In C++, to achieve actual execution type cast, dynamic binding, one should use pointer and define function to be overridden as virtual function
Rossella Lau Lecture 8, DCO10105, Semester B, Data and memory All data required in a program must be in the memory Memory is addressed from zero to, e.g., 512M-1 Data in a location is referenced by an address Data declared in a program will be assigned some memory spaces and referenced by the beginning of the address … Memory in vertical view
Rossella Lau Lecture 8, DCO10105, Semester B, Simple data referencing in memory x's address is, for example, 2000 with the value of 0, and y is at … x y …… {int a, b; …… }
Rossella Lau Lecture 8, DCO10105, Semester B, Memory allocation for simple data It depends on both the hardware and the compiler Usually, for the C or C++ programs, the compiler/ system assigns a truck of memory for allocating: Static space Data survival while the program is running Syntax: e.g., static int a; and global variables Automatic space (or called a stack) Data are alive only when data’s corresponding scope is running Syntax: e.g., int a;
Rossella Lau Lecture 8, DCO10105, Semester B, Pointer A pointer stores the value of an address In data declaration, e.g., int *p; p is a pointer; the content in the address of p is integer type, i.e., address 2000 stores an integer In program statement, the identifier, p, refers to an address (i.e., 2000) or it will be assigned an address the identifier with an * (dereferencing), *p, refers to the data/object in the address of p &id means the address of id … x y p
Rossella Lau Lecture 8, DCO10105, Semester B, Pointer definition & expression To declare a pointer e.g,. int * ptr; To assign the address of a datum to a pointer, e.g., int a, b; ptr = &a; To refer to the datum pointed to by a pointer (dereferencing) e.g., *ptr = 2; // same as a = 2 b = *ptr; // same as b = a
Rossella Lau Lecture 8, DCO10105, Semester B, Pointer - a conceptual sense … x y p 0 … x y p
Rossella Lau Lecture 8, DCO10105, Semester B, More details about pointer’s related syntax Malik’s slides: 14: 5-9 Malik Exercise: 14:3 What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout << x << “ “ << y << endl; cout << *p << “ “ << * q << endl;
Rossella Lau Lecture 8, DCO10105, Semester B, Array and pointer In C, an array id is also a pointer For static array, it is a constant pointer For dynamic array, it can be used as a normal pointer The array id, array, refers to the starting address of the array; i.e., array refers to the address of array[0] E.g., int *array; or int array[SIZE]; array is the starting address of the array array is also the address of array[0] *array is the same as array[0] If it is a dynamic array (the first form), it allows for int *ptr; ptr = arrary; array = ptr; However, if it is a static array (the second form), array is a constant pointer, it does not allow for array = ptr;
Rossella Lau Lecture 8, DCO10105, Semester B, Null pointer A pointer can have null value, e.g., ptr = 0; This pointer with a value of binary zero is also called a null pointer It represents that the pointer refers to nothing Checking: if (ptr) // to check if a pointer refers to something if (!ptr) // to check if a pointer is null if (ptr == NULL) // C style to check for a null pointer
Rossella Lau Lecture 8, DCO10105, Semester B, Parameter passed by pointer (for efficiency) When an object is passed to a function, copying of the object is not efficient Passed by a pointer, other than passed by a reference, is more efficient: only the value of the pointer is copied Calling functionCalled function copying E.g., functions in array.cpp of Lecture 4
Rossella Lau Lecture 8, DCO10105, Semester B, Constant pointers/reference Objects passed by pointers may be modified since the object is pointed to by the called function On many occasions, objects are only for reference purposes; e.g., parameters of many operators such as +, -, *, /, cout. C++ provides a keyword const to specify that an object (including pointers) can be a constant and/or the object referenced can be a constant; i.e., the object referenced remains unchanged; e.g.,: int const *cip; // pointer to an int const int *const icp; // pointer const to an int int const *const cicp; // pointer const to an int const
Rossella Lau Lecture 8, DCO10105, Semester B, …… a_ptr = a_fun() …… int *a_fun() { int a; …… return &_a; // space of a // released // before a_fun // ends } Danger pointers Dangling pointer: a pointer points to an object which is not available or overwritten by others Invalid address reference; e.g., use of null pointers.
Rossella Lau Lecture 8, DCO10105, Semester B, The declaration of a pointer does not provide any memory space for its target Dynamic space can be obtained by the operator new Dewey *p = new Dewey; // get space for an instance Dynamic space is in an area managed by the operating system and is permanent – it allows the object to be created and then used in different functions C-style gets dynamic space by using malloc() Pointer and memory allocation //Misuse of null pointer int *p; *p = 1320; // illegal!!
Rossella Lau Lecture 8, DCO10105, Semester B, Memory allocation for dynamic array The following will cause the system to fail when length is a variable, int array[length]; To obtain space for an array: int *array = new int[length]; Remember to avoid misuse of null pointers: int *array; cout << *array; // array does not refer // to any real block!
Rossella Lau Lecture 8, DCO10105, Semester B, Dynamic memory de-allocation Unlike Java, C does not automatically resume dynamic spaces If one continues to do allocation, the system will crash because of running out of memory – it is also called memory leak. It is required to de-allocate the occupied space before the program is terminated delete dewey; delete[] array; //must use this form for array C-style: delete()
Rossella Lau Lecture 8, DCO10105, Semester B, Notes to memory de-allocation Local variables defined in each function will be automatically released just before a function is terminated Instances declared as a real variable, not a pointer, e.g., Donald donald; will also be released in the same way Global variables are automatically released just before a program is terminated However, dynamic space assigned by using the operator new will not be released as in the above manner.
Rossella Lau Lecture 8, DCO10105, Semester B, Pointer and class A member function in OOP must be invoked with an instance object; e.g., dewey.print(); The instance object is the associated object of each member function defined in a class In Java, the associated object is “ this ”, a reference In C++, “ this ” is a pointer To refer to the associated object, use *this To identify a member, e.g., use this print();
Rossella Lau Lecture 8, DCO10105, Semester B, Pointer as a data member When there is a pointer data member, most likely, it will point to dynamic memory; e.g., class IntArray stores a dynamic array, a pointer Care should be taken during class construction since the following can be automatically generated by the compiler: Default constructor & Destructor Copy constructor & Assignment operator Care should be taken that if a shallow copy or a deep copy is needed (Malik’s slide: 14: 16, 21) Class IntArray, similar to classes in the STL, performs deep copy
Rossella Lau Lecture 8, DCO10105, Semester B, Using pointer for dynamic binding scopePlayer3.cpp Line 40 takes effect! Sub classes should be able to pretend the base class and then plays its own role again such as the call on line 44 But the execution shows that for line 44, it uses only the print() of the base class
Rossella Lau Lecture 8, DCO10105, Semester B, Dynamic binding To simplify defining many functions for playPrint(), the print() of the base class can be defined as a virtual function in order to allow one expression to perform different operations E.g., DonaldFamilyVirtual.h & scopePlayer4.cpp Change void print() const; to virtual void print() const; The execution inside playPrint() can perform different print() according to the actual data type of the passed object! Dynamic binding in C++ should be achieved by both pointers and virtual functions
Rossella Lau Lecture 8, DCO10105, Semester B, Summary Polymorphism means an object can play different roles through static binding or dynamic binding Dynamic binding in C++ must be achieved by pointers and virtual functions A pointer stores an address and can apply de-referencing and object member identification Pointer is a powerful but also a dangerous feature and thus in C++, reference is used more When there is a pointer data member, care must be taken in defining default constructor, copy constructor, assignment operator, and destructor
Rossella Lau Lecture 8, DCO10105, Semester B, Reference Malik: END --