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

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.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Pointers OVERVIEW.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
1 CSC241: Object Oriented Programming Lecture No 03.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Introduction to Object Oriented Programming Chapter 10.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Learners Support Publications Constructors and Destructors.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Data Structures and Algorithms
Procedural and Object-Oriented Programming
Data Structures and Algorithms
Pointers and Dynamic Arrays
The C++ programming language
Static data members Constructors and Destructors
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
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 ++
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
CISC181 Introduction to Computer Science Dr
CS410 – Software Engineering Lecture #11: C++ Basics IV
Introduction to Classes
CS Computer Science IB: Object Oriented Programming
Student Book An Introduction
COMP 2710 Software Construction Pointers
Pointers Revisited What is variable address, name, value?
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
group work #hifiTeam
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Contents Introduction to Constructor Characteristics of Constructor
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Constructors and Destructors
CS148 Introduction to Programming II
Introduction to Classes and Objects
Computing and Statistical Data Analysis Lecture 6
CS410 – Software Engineering Lecture #5: C++ Basics III
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
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: Constructor and Destructor Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics Structures and classes Data members and member functions Accessing members Access control of members public and private members Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture Special member functions Constructor functions Destructor functions 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 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recap: Member Functions and Their Usage A class can have public or private member functions class V3 { // 3-dimensional vector with printLength() private: double x, y, z; public: … Other member functions … void printLength() { cout << length() << endl; return; } private: double length() {return sqrt(x*x + y*y + z*z);} }; int main() { V3 a, * ptr; … Some code here … a.printLength(); ptr = new V3; if (ptr == NULL) return -1; ptr->printLength(); delete ptr; return 0; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Two Special Member Functions of Every Class Constructor: Invoked automatically when an object of the class is allocated Convenient way to initialize data members Just like any other member function Accepts optional input parameters Can be used to perform tasks other than initialization too Destructor: Invoked automatically when an object of the class is de-allocated Convenient way to do book-keeping/cleaning-up before de- allocating object Accepts no parameters Can be used to perform other tasks before de-allocating object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Example Constructor of Class V3 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; } … Other member functions of V3 … }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Example Constructor of Class V3 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; } … Other member functions of V3 … }; A member function No return type Same name as that of class (i.e. V3) Optional input parameters Mostly used for initialization Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Example Constructor of Class V3 Another constructor of class V3 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; } … Other member functions of V3 … }; Typed list of parameters different from that of the previous constructor Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Multiple Constructors of Same Class A class can have multiple constructors as long as each one has a distinct list of parameter types V3 (double vx, double vy, double vz) and V3() When allocating an object of the class, the types of parameters passed to the constructor determine which constructor is invoked V3 myObj1; V3 *myObj2 = new V3(1.0, 2.0. 3.0); Allocated object serves as the receiver object for the constructor call Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Note the “public” declaration Usage of Constructors int main() { V3 a (0.0, 0.0, 0.0); V3 b; V3 *p, *q; … Some code here … p = new V3 (1.0, 2.0, 3.0); q = new V3; delete p; delete q; return 0; } 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; } … Other member functions of V3 … }; Note the “public” declaration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Note the “public” declaration Usage of Constructors int main() { V3 a (0.0, 0.0, 0.0); V3 b; V3 *p, *q; … Some code here … p = new V3 (1.0, 2.0, 3.0); q = new V3; delete p; delete q; return 0; } 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; } … Other member functions of V3 … }; Note the “public” declaration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Usage of Constructors int main() { V3 a (0.0, 0.0, 0.0); V3 b; V3 *p, *q; … Some code here … p = new V3 (1.0, 2.0, 3.0); q = new V3; delete p; delete q; return 0; } 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; } … Other member functions of V3 … }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Usage of Constructors int main() { V3 a (0.0, 0.0, 0.0); V3 b; V3 *p, *q; … Some code here … p = new V3 (1.0, 2.0, 3.0); q = new V3; delete p; delete q; return 0; } 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; } … Other member functions of V3 … }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Two Special Member Functions of Every Class Constructor: Invoked automatically when an object of the class is allocated Convenient way to initialize data members Just like any other member function Accepts optional input parameters Can be used to perform tasks other than initialization Destructor: Invoked automatically when an object of the class is de-allocated Convenient way to do book-keeping/cleaning-up before de- allocating object Accepts no parameters Can be used to perform other tasks before de-allocating object Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Example Destructor of Class V3 class V3 { private: double x, y, z; double length() { … } public: … Constructors of class V3 … ~V3() { if (length() == 0.0) {cout << “Zero vector!!! ” << endl;} return; } … Other member functions of class V3 … }; Destructor of class V3 A member function No return type Name: ~ followed by name of class (i.e. ~V3) No input parameters Mostly used for book-keeping/clean-up before de-allocation of objects Multiple destructors of same class not allowed in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Example Destructor of Class V3 class V3 { private: double x, y, z; double length() { … } public: … Constructors of class V3 … ~V3() { if (length() == 0.0) {cout << “Zero vector!!! ” << endl;} return; } … Other member functions of class V3 … }; int main() { V3 a (1.0, 2.0, 3.0); { V3 b; a = b; } V3 *p = new V3(1.0, 1.0, 1.0); a = *p; delete p; return 0; Note the “public” declaration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Summary Constructor and destructor functions of classes Simple usage of above special member functions More complex usage coming later … Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay