Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.

Slides:



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

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.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Structures/Classes CS 308 – Data Structures. What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure.
Constructors & Destructors Review CS 308 – Data Structures.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
C++ Review (3) Structs, Classes, Data Abstraction.
 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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
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.
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
1 CSC241: Object Oriented Programming Lecture No 05.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
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.
CONSTRUCTORS & DESTRUCTORS
Concepts of Constructors and Its Types
Constructors & Destructors.
Constructor & Destructor
Constructors & Destructors
Class: Special Topics Copy Constructors Static members Friends this
group work #hifiTeam
Classes Access Specifiers
Introduction to Classes
Contents Introduction to Constructor Characteristics of Constructor
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Constructor Spl member fn auto ini of object
Constructors and destructors
Constructors and Destructors
Introduction to Classes and Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Constructors And Destructors

2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used for Initialization -- Same Name as the Class Name Does not Return a Value Does not Return a Value Cannot be Virtual and Static Cannot be Virtual and Static Implicitly Invoked when Objects are Created or Copied Implicitly Invoked when Objects are Created or Copied Can have Arguments Can have Arguments Cannot be Explicitly called Cannot be Explicitly called Multiple Constructors are Allowed Multiple Constructors are Allowed Default Constructor -- No Arguments -- Explicit or Implicit Default Constructor -- No Arguments -- Explicit or Implicit Copy Constructor -- Explicit or Implicit Copy Constructor -- Explicit or Implicit

3 class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; rectangle::rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }

4 A constructor is called automatically whenever a new instance of a class is created. A constructor is called automatically whenever a new instance of a class is created. You must supply the arguments to the constructor when a new instance is created. You must supply the arguments to the constructor when a new instance is created. If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body). If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).

5 Cont. void main() { rectangle rc(3.0, 2.0); rectangle rc(3.0, 2.0); rc.posn(100, 100); rc.posn(100, 100); rc.draw(); rc.draw(); rc.move(50, 50); rc.move(50, 50); rc.draw(); rc.draw();} Note: attempting to initialize a data member of a class explicitly in the class definition is a syntax error. Note: attempting to initialize a data member of a class explicitly in the class definition is a syntax error.

6 Overloading constructors You can have more than one constructor in a class, as long as each has a different list of arguments. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

7 Cont. rectangle::rectangle(){ height = 10; height = 10; width = 10; width = 10; xpos = 0; xpos = 0; ypos = 0; ypos = 0;} void main() { rectangle rc1(3.0, 2.0); rectangle rc1(3.0, 2.0); rectangle rc2(); rectangle rc2(); rc1.draw(); rc1.draw(); rc2.draw(); rc2.draw();}

8 What is a copy constructor? It is a member function which initializes an object using another object of the same class. It is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: A copy constructor has the following general function prototype: class_name (const class_name&);

9 class rectangle { private: private: float height; float height; float width; float width; int xpos; int xpos; int ypos; int ypos; public: public: rectangle(float, float); // constructor rectangle(float, float); // constructor rectangle(const rectangle&); // copy constructor rectangle(const rectangle&); // copy constructor void draw(); // draw member function void draw(); // draw member function void posn(int, int); // position member function void posn(int, int); // position member function void move(int, int); // move member function void move(int, int); // move member function};

10 rectangle::rectangle(const rectangle& old_rc) { height = old_rc.height; height = old_rc.height; width = old_rc.width; width = old_rc.width; xpos = old_rc.xpos; xpos = old_rc.xpos; ypos = old_rc.ypos; ypos = old_rc.ypos;} void main() { rectangle rc1(3.0, 2.0); // use constructor rectangle rc1(3.0, 2.0); // use constructor rectangle rc2(rc1); // use copy constructor rectangle rc2(rc1); // use copy constructor rectangle rc3 = rc1; // alternative syntax for rectangle rc3 = rc1; // alternative syntax for // copy constructor // copy constructor C++ statements; C++ statements;}

11 Destructor Member Function used for Clean-up -- Same Name as the Class Name with a ~ One Single Destructor Special Invoked When an Object Goes Out of Scope Can be Explicitly Called -- Unusual Cannot Accept Parameters and Does Not Return A Value Cannot be Declared static, const or volatile Can be virtual

12 Class string { private: private: char *s; char *s; int size; int size; public: public: string(char *); // constructor string(char *); // constructor ~string(); // destructor ~string(); // destructor}; string::string(char *c) { size = strlen(c); size = strlen(c); s = new char[size+1]; s = new char[size+1]; strcpy(s,c); strcpy(s,c);} string::~string(){ }

13 If you do not specify a destructor, the compiler generates a default destructor for you. When a class contains a pointer to memory you allocate, it is your responsibility to release the memory before the class instance is destroyed.

14 // date.h /* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor }; // date.h /* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor }; // date.cpp #include “date.h” //Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} //Destructor date::~date() {delete[] (string_date);} // date.cpp #include “date.h” //Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} //Destructor date::~date() {delete[] (string_date);} // client.cpp #include “date.h” main(){ // "today" is an object of "date". Constructor is invoked. date today(“ March 10, 2012 "); } // client.cpp #include “date.h” main(){ // "today" is an object of "date". Constructor is invoked. date today(“ March 10, 2012 "); }

15