The Constructors Lecture 7 Fri, Feb 2, 2007.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Memory Leaks and Dangling Pointers Lecture 5 Secs 2.4, 3.4 Mon, Jan 28, 2008.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
 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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
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:
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.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
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.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
CISC181 Introduction to Computer Science Dr
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Array Lists Chapter 6 Section 6.1 to 6.3
Automatics, Copy Constructor, and Assignment Operator
understanding memory usage by a c++ program
The Assignment Operator
Copy Constructor CSCE 121 J. Michael Moore.
Destruction and Copying
Indirection.
Assessment – Java Basics: Part 1
Inheritance: The Fundamental Functions
The Destructor and the Assignment Operator
Memory Leaks and Dangling Pointers
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Essential Class Operations
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
CSC212 Data Structure - Section RS
Review Chapter 10 PPT for full coverage.
Destruction and Copying
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
Essential Class Operations
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Rule Of Three Part 3.
Copy Constructors and Overloaded Assignment
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Presentation transcript:

The Constructors Lecture 7 Fri, Feb 2, 2007

The Four Fundamental Member Functions The functions The default constructor The copy constructor The destructor The assignment operator These four member functions are essential to the functioning of any class. 5/22/2019 Four Fundamental Member Functions

The Default Constructor The default constructor constructs an object for which no initial value is given. Prototype: Usage: The default constructor should initialize the data members to values that are appropriate for that type. Type::Type(); Type Object; 5/22/2019 Four Fundamental Member Functions

Purposes of the Default Constructor The default constructor is used when An object is created with no initial value specified. The members of an array are initialized automatically. When the new operator is used. When a static array is partially initialized. 5/22/2019 Four Fundamental Member Functions

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

The Automatic Default Constructor Is provided automatically if we write no constructor. Is not provided if we write any constructor; we must then write the default constructor, if we want one. Allocates memory for the data members. Invokes each data member’s own default constructor. 5/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions The Copy Constructor The copy constructor constructs an object which will be a copy of an existing object. Prototype: Usage: Type::Type(const Type&); Type ObjectA = ObjectB; Type ObjectA(ObjectB); 5/22/2019 Four Fundamental Member Functions

Purposes of the Copy Constructor The copy constructor is used when An object is created and initialized to the value of an existing object of the same type. A local copy of a value parameter is created during a function call. A copy of the return value is created when a function returns. 5/22/2019 Four Fundamental Member Functions

Four Fundamental Member Functions Point of Style Good style Use the copy constructor. Poor style Use the default constructor followed by the assignment operator. Rational r = s; Rational r; r = s; 5/22/2019 Four Fundamental Member Functions

The Automatic Copy Constructor Allocates memory for the data members. Invokes each data member’s copy constructor to copy values from the existing object. memB memA memC ObjectA ObjectB 5/22/2019 Four Fundamental Member Functions

The Automatic Copy Constructor This is called a shallow copy. Pointers get copied with no change in value. Therefore, the pointer in the new object will point to the very same memory as the pointer in the old object. Generally, this is not good. Instead, we want a deep copy. What would happen in the Vectr class if we made a shallow copy of a vector? 5/22/2019 Four Fundamental Member Functions