Introduction to Programming Lecture 39. Copy Constructor.

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.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Copy Constructors Fall 2008 Dr. David A. Gaitros
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
More C++ Features True object initialisation
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 CSC241: Object Oriented Programming Lecture No 05.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Learners Support Publications Constructors and Destructors.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
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.
Constructors and Destructors
Pointers and Dynamic Arrays
Introduction to Programming
Programming with ANSI C ++
CISC181 Introduction to Computer Science Dr
C++, OBJECT ORIENTED PROGRAMMING
Constructor & Destructor
Memberwise Assignment / Initialization
Basic notes on pointers in C
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object Oriented Programming (OOP) Lecture No. 9
Constructors and Destructors
Destruction and Copying
Indirection.
Dynamic Data Structures
Introduction to Programming
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Essential Class Operations
CS148 Introduction to Programming II
Introduction to Programming
Passing Arguments and The Big 5
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Destruction and Copying
Chapter 9: Pointers and String
Essential Class Operations
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
Presentation transcript:

Introduction to Programming Lecture 39

Copy Constructor

Review Pointers Pointers References References Memory Allocation Memory Allocation

Pointers A pointer is a special type of variable that contain a memory address

Void Pointer Void Pointer Pointer to an Integer Pointer to an Integer Pointer to a Character Pointer to a Character Pointer to a Float Pointer to a Float Pointer to Objects Pointer to Objects

References

&

const

Dynamic Memory Allocation

Native Operator new new delete delete

int *p ; p = new int ; delete p ; Dynamic Memory Allocation

int *p ; p = new int [ 10 ] ; delete [ ] p ; Dynamic Memory Allocation

class Matrix { private : int * m ; int row, col ; public : Matrix ( int rows, int cols ) Matrix ( int rows, int cols ) { m = new int [ rows * cols ] ; } } ; Example

delete [ ] m ;

Assignment

int i = 0 ; //Initialization int i ; i = 0 ; //Assignment

Matrix m1, m2 ; …… m2 = m1 ; m2 = m1 ; //Assignment Statement

Member to Member Assignment

m2 = m1

0xefffdad0 mx Pointing to the same region in memory int *m of m1 0xefffdad0 int *m of m2

0xefffdad0 mx Pointing to the same region in memory int *m of m1 0xefffdad0 int *m of m2

Copy Constructor

Call by value

Deep Copy Shallow Copy

Matrix ( Matrix & ) ;

Example class String { char * c ; char * c ; public : public : void copy ( char * s ) ; String ( char * data ) ; void print ( ) ; // etc. // etc. } ;

Example String s1 ( “test1” ) ; String s2 = s1 ; s1.copy ( “this is a test” ) ; s2.print ( ) ;

int i ; i = 10 ; i = i ;

Matrix m2 ( m1 ) ; Matrix m2 = m1 ;

Rules For dynamic memory allocation 1. Define copy constructor 2. Write assignment operator 3. Provide destructor

What have we covered today Review of pointers Review of pointers –Dynamic memory allocation new new Delete Delete For memory allocation in classes we must provide For memory allocation in classes we must provide –Constructor –Copy constructor –Assignment operator –destructor