Rossella Lau Lecture 8, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Rossella Lau Lecture 7, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Stack and Heap Memory Stack resident variables include:
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Pointers OVERVIEW.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Memory Management.
Object Lifetime and Pointers
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
9-10 Classes: A Deeper Look.
Dynamic Memory.
9-10 Classes: A Deeper Look.
Presentation transcript:

Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance and polymorphism  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, Inheritance A sub class has all the properties of its base class  Data members and function members, no matter if it is private, protected, or public  The type of the base class A sub class can play as its parent class when passing as a parameter; e.g., in scopePlayer.cpp, Donald boxD(dewey); deduct7(dewey);  A sub class has many types – poly – morph (mask)

Rossella Lau Lecture 8, DCO10105, Semester B, Polymorphism  Polymorphism – many types  Same classes can play as different types (of ancestors)  Same expressions can perform different operations

Rossella Lau Lecture 8, DCO10105, Semester B, Same class acts as its ancestors  Same family members can be stored in the same container  Donald donalds[SIZE]; …… donalds[i] = dewey;  Vector donalds(SIZE); …… donald[i] = dewey;  One function can be called with the same family members  deduct7(Donald &) in scopePlayer.cpp

Rossella Lau Lecture 8, DCO10105, Semester B, Same expression but different operations  When family members stored in the same container, e.g., donalds[i].print() invokes  Dewey::print() if donalds[i] stores dewey  Huey::print() if donalds[i] stores huey  Donald::print() if donads[i] stores donald

Rossella Lau Lecture 8, DCO10105, Semester B, bindingPlayer1.cpp  A parent’s function can also be identified through the instance of the child class by using BaseClass:: e.g. dewey.Donald::withdraw(10);  How about : (static_cast (dewey)).withdraw (10);  It does not work! It does not have any effect even though dewey pretends to be a Donald !

Rossella Lau Lecture 8, DCO10105, Semester B, Static binding does not always work  The type cast of the above case is just a compiler binding, static binding, and dewey’s actual data type is still Dewey and it can’t really pretend to be a 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 executable 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, Pointer Variables  The statement int *p; is equivalent to the statement int* p; which is equivalent to the statement int * p;  The character * can appear anywhere between the data type name and the variable name  int* p, q; Only p is the pointer variable, not q. Here q is an int variable (Malik’s slide: 14-5)

Rossella Lau Lecture 8, DCO10105, Semester B, Pointer Variables  To avoid confusion, we prefer to attach the character * to the variable name int *p, q;  The following statement declares both p and q to be pointer variables of the type int int *p, *q; (Malik’s slide: 14-6)

Rossella Lau Lecture 8, DCO10105, Semester B, The Address of Operator (&)  The ampersand, &,  is called the address of operator  is a unary operator that returns the address of its operand (Malik’s slide: 14-7) Note that & here is the “address of” operator, not the symbol for “reference”

Rossella Lau Lecture 8, DCO10105, Semester B, The Dereferencing Operator (*)  C++ also uses * as a unary operator  When used as a unary operator, *,  commonly referred to as the dereferencing operator or indirection operator,  refers to the object to which its operand (that is, a pointer) points (Malik’s slide: 14-8)

Rossella Lau Lecture 8, DCO10105, Semester B, Dynamic Variables  Variables that are created during program execution are called dynamic variables  With the help of pointers, C++ creates dynamic variables  new and delete, to create and destroy dynamic variables, respectively  When a program requires a new variable, the operator new is used  When a program no longer needs a dynamic variable, the operator delete is used  In C++, new and delete are reserved words (Malik’s slide: 14-9)

Rossella Lau Lecture 8, DCO10105, Semester B,  Malik Exercise: 14:3-4  What is the output of the following C++ code? Exercises int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout << x << “ “ << y << endl; cout << *p << “ “ << *q << endl; int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p = q; *p = 78; 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, equivalent to 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 an ordinary 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 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  a = a; in Quadratic.cpp

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 to be sure whether 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, Shallow Copy vs Deep Copy  In a shallow copy, two or more pointers of the same type point to the same memory; that is, they point to the same data  In a deep copy, two or more pointers have their own data (Malik’s slide: 14-16)

Rossella Lau Lecture 8, DCO10105, Semester B, The Assignment Operator (Malik’s slide: 14-16)

Rossella Lau Lecture 8, DCO10105, Semester B, Using pointer for dynamic binding  bindingPlayer2.cpp  (static_cast (dewey))->withdraw (10); takes effect!  Sub classes should be able to pretend to be the base class and then plays its own role again such as the call playPrint(); on line 32  But the execution shows that for donald->print() of playPrint(), 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 & bindingPlayer3.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 --