Copy Assignment CSCE 121.

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

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
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.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Andy Wang Object Oriented Programming in C++ COP 3330
Haidong Xue Summer 2011, at GSU
CS 2304: Operator Overloading
Lesson 14 Copy and Assignment
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
The dirty secrets of objects
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Pointers and Linked Lists
Operator Overloading CSCE 121 J. Michael Moore
Automatics, Copy Constructor, and Assignment Operator
Const in Classes CSCE 121 J. Michael Moore.
Heterogeneous aggregate datatypes
Classes with Dynamically Allocated Data
understanding memory usage by a c++ program
Automatics, Copy Constructor, and Assignment Operator
Overloading the << operator
The Assignment Operator
Copy Constructor CSCE 121 J. Michael Moore.
Copy Assignment CSCE 121 J. Michael Moore.
Destruction and Copying
CISC/CMPE320 - Prof. McLeod
Essential Class Operations
Destruction and Copying
COP 3330 Object-oriented Programming in C++
Move Semantics CSCE 121.
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Essential Class Operations
Overloading the << operator
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
C++ data types.
Rule of Three Part 1 & 2.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Destructors, Copy Constructors & Copy Assignment Operators
Rule Of Three Part 3.
Copy Constructors and Overloaded Assignment
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Presentation transcript:

Copy Assignment CSCE 121

Overload ‘=‘ Operator ‘=‘ is the assignment operator Allows class to perform expected deep copy behavior

Using Copy Assignment MyClass object1, object2; // modify object 1 object2 = object1;

Declaration ClassName& operator=(const ClassName& source); a = b; a.operator=(b);

Definition Prevent a memory leak! ClassName& Classname::operator=(const ClassName& source) { if (this != &source) { // don’t self assign // Delete old data // Allocate new memory // Copy data } return *this; Prevent a memory leak!

Copy Assignment vs. Copy Constructor Return: reference to self Function name: operator= Parameter: const reference to source object of same type Check for self-assignment Delete old data Allocate new memory Copy data from source Return: None (all constructors) Function name: ClassName Parameter: const reference to source object of same type Allocate new memory Copy data from source If we compare the copy assignment operator to the copy constructor, we see first of all that they take the same parameter: a const reference to the source object. And it we look at what they do, we see that the copy constructor is the same as the copy assignment operator, minus a couple of missing steps that aren’t needed. Now why would we not need to check for self-assignment and delete old data? ... Because the object is just being created, so there’s nothing to delete yet, and it can’t be equal to the source because it’s not initialized yet.