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

CPS 235 Object Oriented Programming Paradigm
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Classes & Data Abstraction
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
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.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
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:
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
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.
Pointer to an Object Can define a pointer to an object:
Data Structures and Algorithms
Procedural and Object-Oriented Programming
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
Object-Oriented Programming (OOP) Lecture No. 21
CISC181 Introduction to Computer Science Dr
Review: Two Programming Paradigms
Introduction to Classes
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Introduction to Classes
Learning Objectives Classes Constructors Principles of OOP
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
CS150 Introduction to Computer Science 1
Constructors and Other Tools
Constructors and Destructors
CS148 Introduction to Programming II
COP 3330 Object-oriented Programming in C++
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Method of Classes Chapter 7, page 155 Lecture /4/6.
CS410 – Software Engineering Lecture #5: C++ Basics III
Fall 2018, COMP 562 Poster Session
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
CS148 Introduction to Programming II
Classes Member Qualifiers
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: More on Constructors Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics Object-oriented programming with structures and classes Data members and member functions Accessing members and controlling access to members Constructor and destructor functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture Closer look at constructors Example usage in C++ programs 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

Recap: Constructors/Destructor of Class V3 class V3 { private: double x, y, z; double length() { … } public: V3 (double vx, double vy, double vz) { x = vx; y = vy; z = vz; return; } V3 () { x = y = z = 0.0; return; } ~V3() { if (length() == 0.0) { cout << “Zero vector!!!”; return; … Other member functions of V3 … }; Constructor functions Destructor function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recap: Invoking Member Functions Member functions of a class can be usually invoked explicitly on a receiver object of the same class class V3 { private: double x, y, z; double length() { … } public: V3 scale(double const factor) { … } void printLength() { … } … Other member functions … }; int main() { V3 a (1.0, 2.0, 3.0); V3 b = a.scale(4.0); b.printLength(); return 0; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recap: Invoking Member Functions Member functions of a class can be usually invoked explicitly on a receiver object of the same class class V3 { private: double x, y, z; double length() { … } public: V3 scale(double const factor) { … } void printLength() { … } … Other member functions … }; int main() { V3 a (1.0, 2.0, 3.0); V3 b = a.scale(4.0); b.printLength(); return 0; } Can we do the same with constructors and destructors? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Invoking Constructors/Destructors Explicitly Usually an error to call a destructor explicitly in a program OK to call a constructor explicitly in a program Explicit constructor invokation int main() { V3 a(1.0, 1.0, 1.0); V3 b = a.sum( V3(2.0, 2.0, 2.0) ); a.printLength(); b.printLength(); return 0; } Looks like normal function call Name of (constructor) function is name of class Creates temporary object and invokes constructor on it Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Invoking Constructors Explicitly [Ref. AGRBook] An interesting implementation of “sum” in class V3 class V3 { private: double x, y, z; public: … Other members, constructors, destructor … V3 sum (V3 const &b) { return V3(x+b.x, y+b.y, z+b.z); } }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Specifying Default Parameter Values C++ allows default values to be specified for parameters of constructors (and also for other member functions) With constructor definition V3(double vx = 0.0, double vy = 1.0, double vz = 2.0) { x = vx; y = vy; z = vz; return; } all of the following lead to correct constructor calls V3 a; V3 b (1.2); V3 c (1.2, 1.3); V3 d (1.2, 1.3, 1.4); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Specifying Default Parameter Values C++ allows default values to be specified for parameters of constructors (and also for other member functions) With constructor definition V3(double vx = 0.0, double vy = 1.0, double vz = 2.0) { x = vx; y = vy; z = vz; return; } all of the following lead to correct constructor calls V3 a; V3 b (1.2); V3 c (1.2, 1.3); V3 d (1.2, 1.3, 1.4); Equivalent to V3 a(0.0, 1.0, 2.0); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Specifying Default Parameter Values C++ allows default values to be specified for parameters of constructors (and also for other member functions) With constructor definition V3(double vx = 0.0, double vy = 1.0, double vz = 2.0) { x = vx; y = vy; z = vz; return; } all of the following lead to correct constructor calls V3 a; V3 b (1.2); V3 c (1.2, 1.3); V3 d (1.2, 1.3, 1.4); Equivalent to V3 b(1.2, 1.0, 2.0); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Specifying Default Parameter Values C++ allows default values to be specified for parameters of constructors (and also for other member functions) With constructor definition V3(double vx = 0.0, double vy = 1.0, double vz = 2.0) { x = vx; y = vy; z = vz; return; } all of the following lead to correct constructor calls V3 a; V3 b (1.2); V3 c (1.2, 1.3); V3 d (1.2, 1.3, 1.4); Equivalent to V3 c(1.2, 1.3, 2.0); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Specifying Default Parameter Values C++ allows default values to be specified for parameters of constructors (and also for other member functions) With constructor definition V3(double vx = 0.0, double vy = 1.0, double vz = 2.0) { x = vx; y = vy; z = vz; return; } all of the following lead to correct constructor calls V3 a; V3 b (1.2); V3 c (1.2, 1.3); V3 d (1.2, 1.3, 1.4); Equivalent to V3 d(1.2, 1.3, 1.4); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Initialization Lists Specifies how different data members of the receiver object are initialized before execution of the constructor begins class V3 { private: double x, y, z; public: V3(double vx, double vy, double vz) : x(vx), y(vy), z(vz) { … Rest of constructor code comes here … } … Other member functions of class V3 … }; Note the : Initialization List Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Can be any expression in vx, vy, vz Initialization Lists Specifies how different data members of the receiver object are initialized before execution of the constructor begins class V3 { private: double x, y, z; public: V3(double vx, double vy, double vz) : x(vx), y(vy), z(vz) { … Rest of constructor code comes here … } … Other member functions of class V3 … }; Note the : Initialization List Can be any expression in vx, vy, vz Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

An Interesting Example [Ref. AGRBook] class Point { private: double x, y; public: Point (double p, double q) {x = p; y = q; return;} }; class Disk { private: Point center; double radius; public: Disk(double x, double y, double r) : center(Point(x, y)), radius(r) { return; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Summary Closer look at constructor functions Invoking constructors explicitly Specifying default values of parameters Initialization lists Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay