The Destructor and the Assignment Operator

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 14: Overloading and Templates
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Classes: A Deeper Look Systems Programming.
Chapter 13: Overloading.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 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.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Learning Objectives Pointers as dada members
Chapter 13: Overloading and Templates
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
OBJECT ORIENTED PROGRAMMING
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.
Programming with ANSI C ++
Operator overloading Conversions friend inline
The Three Attributes of an Identifier
Constructors & Destructors.
Class: Special Topics Copy Constructors Static members Friends this
Chapter 15: Overloading and Templates
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
C++ for Engineers and Scientists Second Edition
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
7 Arrays.
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 4 (part 2).
10.2 const (Constant) Objects and const Member Functions
Constructors and destructors
CS148 Introduction to Programming II
7 Arrays.
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Object Oriented Programming (OOP) Lecture No. 12
SPL – PS3 C++ Classes.
Presentation transcript:

The Destructor and the Assignment Operator Lecture 8 Thu, Feb 8, 2007

Four Fundamental Member Functions The Destructor The destructor destroys an object, i.e., it deallocates the memory used by the object. Prototype: Usage: The destructor is not invoked directly. Type::~Type(); 2/22/2019 Four Fundamental Member Functions

Example: The Vector Class vectr.h vectr.cpp VectrTest.cpp 2/22/2019 Four Fundamental Member Functions

Purpose of the Destructor The destructor is used to destroy an object when it passes out of scope. A local variable passes out of scope when execution returns from a function. A variable declared within a block {} passes out of scope when execution leaves that block. A volatile object passes out of scope when the evaluation of the expression in which it occurs is completed. 2/22/2019 Four Fundamental Member Functions

The Automatic Destructor Invokes each data member’s destructor. Deallocates the memory used by the data members. The automatic destructor does not deallocate memory that the data members point to. 2/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions The this Pointer Every (non-static) member function has a hidden parameter named this. this is always the first parameter in such a function. this is a constant pointer to the object (Type* const this) that invoked the member function. this provides us with a name for the invoking object. 2/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions The this Pointer When we write the prototype of a member function as the actual prototype is this is a constant pointer to an object. Type::Function(Parmeters); Type::Function(Type* const this, Parmeters); 2/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions The this Pointer Furthermore, when we write the prototype of a member function as the actual prototype is this is a constant pointer to a constant object. Type::Function(…) const; Type::Function(Type const* const this, …); 2/22/2019 Four Fundamental Member Functions

Usage of the this Pointer Inside a member function, we refer to a data member as This is interpreted as member-name this->member-name 2/22/2019 Four Fundamental Member Functions

Usage of the this Pointer Inside a member function, we invoke another member function by writing This is interpreted as function(parameters); this->function(parameters); 2/22/2019 Four Fundamental Member Functions

The Assignment Operator The assignment operator assigns to an existing object the value of another existing object of the same type. Prototype: Usage: The assignment operator must be a member function. Type& Type::operator=(const Type&); ObjectA = ObjectB; 2/22/2019 Four Fundamental Member Functions

Purpose of the Assignment Operator The assignment operator is use to assign the value of one object to another object in an assignment statement. 2/22/2019 Four Fundamental Member Functions

Form of the Function operator=() Type& Type::operator=(const Type& value) { if (this != &value) // Clear out the old value // Assign the new value } return *this; 2/22/2019 Four Fundamental Member Functions

Form of the Function operator=() Typically we write two member functions makeEmpty() makeCopy() Type& Type::operator=(const Type& value) { if (this != &value) makeEmpty(); // Clear out old value makeCopy(value); // Copy new value } return *this; 2/22/2019 Four Fundamental Member Functions

The Copy Constructor and the Destructor Then we can also write the copy constructor and destructor more easily. Type::Type(const Type& x) { makeCopy(x); return; } Type::~Type() makeEmpty(); 2/22/2019 Four Fundamental Member Functions

The Automatic Assignment Operator Uses each data member’s assignment operator to assign values to them from the other object. 2/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions Multiple Assignments The assignment operator is right-associative. The statement is equivalent to a = b = c; a = (b = c); 2/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions Multiple Assignments What about the statement (a = b) = c; 2/22/2019 Four Fundamental Member Functions