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.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: Program Design Including Data Structures, 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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Inheritance, Polymorphism, and Virtual Functions
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 13: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
OOP Languages: Java vs C++
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CHAPTER 15 POINTERS, CLASSES, AND VIRTUAL FUNCTIONS.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Chapter 6: User-Defined Functions
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
A First Book of C++ Chapter 12 Extending Your Classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Constructors and Destructors
Chapter 7: User-Defined Functions II
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Memberwise Assignment / Initialization
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
VIRTUAL FUNCTIONS RITIKA SHARMA.
Class: Special Topics 2 For classes using memory allocation
Presentation transcript:

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 – Danger: deleting one deletes the data pointed to by all of them Deep copy: when the contents of the memory pointed to by a pointer are copied to the memory location of another pointer – Two copies of the data 1C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Classes and Pointers: Some Peculiarities Example class: Example program statements: 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Destructor If objectOne goes out of scope, its member variables are destroyed – Memory space of dynamic array stays marked as allocated, even though it cannot be accessed Solution: in destructor, ensure that when objectOne goes out of scope, its array memory is deallocated: 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Assignment Operator After a shallow copy: if objectTwo.p deallocates memory space to which it points, objectOne.p becomes invalid Solution: extend definition of the assignment operator to avoid shallow copying of data 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Copy Constructor Default member-wise initialization: Initializing a class object by using the value of an existing object of the same type Example: ptrMemberVarType objectThree(objectOne); Copy constructor: provided by the compiler – Performs this initialization – Leads to a shallow copying of the data if class has pointer member variables 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Copy Constructor (cont’d.) Similar problem occurs when passing objects by value Copy constructor automatically executes in three situations: – When an object is declared and initialized by using the value of another object – When an object is passed by value as a parameter – When the return value of a function is an object 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Copy Constructor (cont’d.) Solution: override the copy constructor For classes with pointer member variables, three things are normally done: – Include the destructor in the class – Overload the assignment operator for the class – Include the copy constructor 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Inheritance, Pointers, and Virtual Functions Can pass an object of a derived class to a formal parameter of the base class type Compile-time binding: the necessary code to call specific function is generated by compiler – Also known as static binding or early binding Virtual function: binding occurs at program execution time, not at compile time – Declared with reserved word virtual 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Inheritance, Pointers, and Virtual Functions (cont’d.) Run-time binding: – Compiler does not generate code to call a specific function: it generates information to enable run- time system to generate specific code for the function call – Also known as dynamic or late binding Note: cannot pass an object of base class type to a formal parameter of the derived class type 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition

vtable For Virtual Functions 10http://

Inheritance, Pointers, and Virtual Functions (cont’d.) Values of a derived class object can be copied into a base class object Slicing problem: if derived class has more data members than base class, some data could be lost Solution: use pointers for both base and derived class objects 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Classes and Virtual Destructors Classes with pointer member variables should have the destructor – Destructor should deallocate storage for dynamic objects If a derived class object is passed to a formal parameter of the base class type, destructor of the base class executes – Regardless of whether object is passed by reference or by value Solution: use a virtual destructor (base class) 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Classes and Virtual Destructors (cont’d.) Virtual destructor of a base class automatically makes the destructor of a derived class virtual – After executing the destructor of the derived class, the destructor of the base class executes If a base class contains virtual functions, make the destructor of the base class virtual 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Abstract Classes and Pure Virtual Functions New classes can be derived through inheritance without designing them from scratch – Derived classes inherit existing members of base class – Can add their own members – Can redefine or override public and protected member functions Base class can contain functions that you would want each derived class to implement However, base class may contain functions that may not have meaningful definitions in the base class 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Abstract Classes and Pure Virtual Functions (cont’d.) Pure virtual functions: – Do not have definitions (bodies have no code) Example: virtual void draw() = 0; Abstract class: a class with one or more virtual functions – Can contain instance variables, constructors, and functions that are not pure virtual – Class must provide the definitions of constructor/functions that are not pure virtual 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Address of Operator and Classes & operator can create aliases to an object Example: int x; int &y = x; x and y refer to the same memory location y is like a constant pointer variable y = 25; sets the value of y (and of x ) to 25 x = 2 * x + 30; updates value of x and y 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Address of Operator and Classes (cont’d.) Address of operator can also be used to return the address of a private member variable of a class – However, if you are not careful, this operation can result in serious errors in the program 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Summary Pointer variables contain the addresses of other variables as their values Declare a pointer variable with an asterisk, *, between the data type and the variable Address of operator ( & )returns the address of its operand Unary operator * is the dereferencing operator Member access operator ( -> ) accesses the object component pointed to by a pointer 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Summary (cont’d.) Dynamic variable: created during execution – Created using new, deallocated using delete Shallow copy: two or more pointers of the same type point to the same memory Deep copy: two or more pointers of the same type have their own copies of the data Binding of virtual functions occurs at execution time (dynamic or run-time binding) 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition