Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty

Slides:



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

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Classes Separating interface from implementation
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
ECE122 Feb. 22, Any question on Vehicle sample code?
 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.
More C++ Features True object initialisation
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:
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
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.
Programming with ANSI C ++
CISC181 Introduction to Computer Science Dr
CS410 – Software Engineering Lecture #11: C++ Basics IV
Introduction to Classes
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Object-Oriented Programming (OOP) Lecture No. 32
Dynamic Memory Allocation
Pass by Reference, const, readonly, struct
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object-Oriented Programming (OOP) Lecture No. 36
Contents Introduction to Constructor Characteristics of Constructor
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Constructors and Other Tools
Constructors and destructors
Constructors and Destructors
Constructors and Destructors
CS148 Introduction to Programming II
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Classes Class Data Members & Initializers
SPL – PS3 C++ Classes.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Presentation transcript:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Default and Copy Constructors Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics Object-oriented programming with structures and classes Accessing members and controlling access to members Constructor and destructor functions Closer look at constructors Explicit invocation Default parameters Initialization lists Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture Continuing study of constructors Default constructors Copy constructors Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Examples taken from this book are indicated in slides by the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recap: Constructor and Destructor Functions Constructor: Invoked automatically when an object of the class is allocated Object is allocated first, then constructor is invoked on object Convenient way to initialize data members Destructor: Invoked automatically when an object of the class is de-allocated Destructor is invoked on object first, then object is de-allocated Convenient way to do book-keeping/cleaning-up before de-allocating object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Non-default constructor of V3 Default constructor of V3 A constructor that doesn’t take any arguments is called a “default constructor” class V3 { private: double x, y, z; public: V3(double vx, double vy, double vz) { x = vx; y = vy; z = vz; return; } V3() {x = y = z = 0.0; return;} … Destructor and other member functions … }; Non-default constructor of V3 Default constructor of V3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Arrays and Default Constructors Suppose we want to define an array of V3 objects 100 objects of class V3 must be allocated Which V3 constructor should be invoked on each of them? Default constructor (one without any arguments) What if we had not defined a default constructor for V3? Could be by oversight or even by design V3 myArray[100]; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Arrays and Default Constructors If no constructor is defined for a class, C++ compiler will provide a bare-bones default constructor No parameters and does nothing in its body Allows array of objects to be defined Similar default destructor also provided by C++ compiler If a non-default constructor is defined, but not a default constructor, C++ compiler will NOT provide a bare-bones default constructor Arrays of such objects cannot be defined !!! Best practice: Define default constructors Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Copy Constructor Suppose a new object is created by making a copy of another object of the same class V3 myFunc(V3 a) { V3 v; v = a.scale(2.0); return v; } int main() { V3 a(0.0, 1.0, 2.0); V3 a1 = a, a2; a2 = myFunc(a); return 0; Case 1: Initalization in declaration Case 2: Parameter passing by value Case 3: Function returning object (May be optimized away by compiler) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

No need for copy constructor Regular assignment statements do not need copy constructor since they do not create a new object V3 myFunc(V3 a) { V3 v; v = a.scale(2.0); return v; } int main() { V3 a(0.0, 1.0, 2.0); V3 a1 = a; a1 = myFunc(a); return 0; Regular assignment: No need for copy constructor Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Ordinary constructors Copy Constructor A copy constructor must be specified separately from an ordinary constructor class V3 { private: double x, y, z; public: V3(double vx, double vy, double vz) { x = vx; y = vy; z = vz; return; } V3() {x = y = z = 0.0; return;} V3(const V3 &src) {x = src.x; y = src.y; z = src.z; } … Destructor and other member functions … }; Ordinary constructors Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Copy Constructor A copy constructor must be specified separately from an ordinary constructor class V3 { private: double x, y, z; public: V3(double vx, double vy, double vz) { x = vx; y = vy; z = vz; return; } V3() {x = y = z = 0.0; return;} V3(const V3 &src) {x = src.x; y = src.y; z = src.z; } … Destructor and other member functions … }; (Uninteresting) Copy constructor Note difference in parameter passing Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Default Copy Constructor If you need a copy constructor in your program, but have not defined it yourself, the C++ compiler will create a default copy constructor Copies values of all data members of source object to corresponding members of receiver object Same as usual assignment Sometimes default copy constructors are not good enough More interesting user-defined copy constructors needed Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Another Copy Constructor [Ref AGRBook] class myString { public: char *cArray; int length; myString(const char initString[]) { … } // ordinary constructor ~myString() {delete [] cArray; return;} myString(const myString &source) : length(source.length) { // copy constructor cArray = new char[length+1]; if (cArray == NULL) { … Handle error appropriately … } else { for (int i = 0; i <= length; i++) { cArray[i] = (source.cArray)[i]; } return; } } … Other member functions … }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Summary Default constructors Copy constructors Importance in defining arrays Copy constructors Importance in creating a new object by copying an existing object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

An Interesting Copy Constructor [Ref AGRBook] class Queue{ private: int front, nWaiting, elements[100]; public: Queue() {front = nWaiting = 0; } // ordinary constructor Queue (const Queue &source) : // copy constructor front(source.front), nWaiting(source.nWaiting) { for (int i = front, j = 0; j < nWaiting; j++) { elements[i] = source.elements[i]; i = (i + 1) % 100; } … Other member functions … }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay