Memberwise Assignment / Initialization

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Memory Management.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
C++ Object-Oriented Programming
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 14: More About Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
Inheritance Using work from:
Chapter 9 Classes: A Deeper Look, Part 1
Constant pointers and pointers to constants
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
Rule of Three Part 1 & 2.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Memberwise Assignment / Initialization The assignment operator, =, is used in two ways: to assign one object to another, or to initialize an object with an object’s data Examples (assuming class V): V v1, v2; … // statements that assign … // values to members of v1 v2 = v1; // assignment V v3 = v2; // initialization See pr11-05.cpp

Assignment When the assignment operator, =, is applied to objects, it copies the data member values of the object on the right to the data members of the object on the left. Such an assignment is referred to as memberwise assignment

Assignment For example, consider class Point { public: Point(); Point(double x, double y); . . . // other functions private: double x; double y; }; Point p1(2.0, 3.5); Point p2; The assignment, p2 = p1; assigns p1's data member values to their counterparts in p2, this is memberwise assignment.

Memberwise Assignment Although assignment looks similar to initialization, they are entirely different operations. In an assignment, no new object is created - the value of an existing object is simply changed. In the previous example, the x, y values stored in p2 are originally 0.0 and 0.0, respectively. After assignment, the x, y values are 2.0 and 3.5, respectively. See pr11-05.cpp

Initialization In C++, an initialization occurs every time a new object is created, as in Point p2 = p1; or Point p2(p1); The constructor that performs this type of initialization is called a copy constructor. See pr11-05.cpp

Copy Constructors a constructor function with the same name as the class A copy constructor is one that takes a reference parameter to another object of the same class The copy constructor uses the data in the object passed as parameter to initialize the object being created Reference parameter should be const to avoid potential for data corruption

Copy Constructor Copy constructors are declared in the class declaration, which is generally in the specification file, and defined in the class implementation file. The general form for declaring a copy constructor is classname(const classname &); For example: Point(const Point &pt);

Copy Constructor There are 3 important places where a copy constructor is called: When an object is created from another object of the same type (i.e. the newly created object is initialized to the data of another object of the same class) When an object is passed by value as a parameter to a function When an object is returned from a function

Copy Constructor If a copy constructor is not defined in a class, the compiler itself defines one, called a default copy constructor. A default copy constructor copies field-to-field, using memberwise assignment between objects (shallow copy) The default copy constructor works fine in most cases

Copy Constructor If the class does not have pointer variables with dynamically allocated memory, then the default constructor works fine But, if the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.

Initialization vs Assignment Consider the following: class Point { public: … private: double x; double y; }; Point p4; Point p1(2.0, 3.5); Point p2 = p1; // initialization, new object created Point p3 = p1; // initialization, new object created p4 = p1; // assignment, no new object created Both p2 and p3 are newly created objects that are initialized to a previously declared object, p1. For p4, the members are simply changed. See pr11-05.cpp

Copy Constructors Problems occur when objects contain pointers to dynamic storage: class CpClass { public: CpClass(int v=0) { p = new int; *p = v; } ~CpClass() { delete p; private: int *p; };

Default Constructor Causes Sharing of Storage CpClass c1(5); if (true) { CpClass c2=c1; } // c1 is corrupted // when c2 goes // out of scope and // its destructor // executes c1 c2 5

Problems of Sharing Dynamic Storage Destructor of one object deletes memory still in use by other objects Modification of memory by one object affects other objects sharing that memory See pr11-07.cpp, NumberArray.h, and NumberArray.cpp

Programmer-Defined Copy Constructors Used to make a deep copy of the object Avoids problems caused by memory sharing Can allocate separate memory to hold new object’s dynamic member data Can make new object’s pointer point to this memory Copies the data, not the pointer, from the original object to the new object See pr11-08.cpp, NumberArray2.h, and NumberArray2.cpp

Copy Constructor Example class CpClass { public: CpClass(const CpClass &obj){ p = new int; // create a new int object *p = *obj.p; // copy data from obj } CpClass(int v=0) { p = new int; *p = v; ~CpClass(){ delete p; private: int *p; };