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

Slides:



Advertisements
Similar presentations
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Rossella Lau Lecture 7, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
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 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.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Java and C++, The Difference An introduction Unit - 00.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 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.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Pointers OVERVIEW.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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++
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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:
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.
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.
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.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Revisited What is variable address, name, value?
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Memory Allocation CS 217.
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  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 --