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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 14: Overloading and Templates
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.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Chapter 13: Overloading and Templates
Chapter 7: User-Defined Functions II
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
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 ++
Chapter 10: Void Functions
CISC181 Introduction to Computer Science Dr
Constructors & Destructors.
Constructor & Destructor
Class Operations Pointer and References with class types
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Introduction to Classes
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Constructors and Destructors
The Destructor and the Assignment Operator
Chapter 7: User-Defined Functions II
9-10 Classes: A Deeper Look.
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

Chapter 4 Constructors and Destructors

Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating a parameterized constructor for the class String Explicit constructors Copy constructor Destructors The philosophy of OOPS

Constructors The constructor gets called automatically for each object that has just got created. It appears as member function of each class, whether it is defined or not. It has the same name as that of the class. It may or may not take parameters. It does not return anything (not even void). The prototype of a constructor is ( ); Constructors fulfill the need for a function that guarantees initialization of member data of a class. Domain constraints on the values of data members can also be implemented via constructors. The constructor gets called automatically for each object when it is created. Example1: class A { /* class definition*/ }; void main() { A A1; }

Constructors Constructors do not actually allocate memory for objects. They are member functions that are called for each object immediately after memory has been allocated for the object. Example 2: class A { int x; public: void setx(int); int getx(); }; void main() { A A1; // object declared } The statement in main is transformed to A A1; // 4 bytes of memory is allocated for the object A1.A();// constructor is called implicitly by the compiler It is forbidden to call the constructor explicitly for an existing object.

Constructor Example 3: 1. Before (without constructor): class A { /* class definition*/ }; 2. After (with constructor): class A { public: A(); //prototype inserted implicitly by the compiler }; A::A() { // empty definition inserted implicitly by the compiler }

The Zero-Argument Constructor The constructor is a non-static member function. It is called for an object. It, therefore, takes the this pointer as a leading formal argument. The address of the invoking object is passed as a leading parameter to the constructor call. This means that the members of the invoking object can be accessed from within the definition of the constructor. The constructor gets called for each object when the object is created.

The Zero-Argument Constructor Example 4: class A { int x; public: A(); //our own constructor void setx(int); int getx(); }; A::A() { cout<< constructor of class A called\t; } void main() { A A1; cout<<program ends\n; } Output : constructor of class A called program ends The constructor that does not take any arguments and is called the zero- argument constructor.

The Zero-Argument Constructor A user-defined constructor implements domain constraints on the data members of a class. Example 5: class distance { int inch; int feet; public: distance(); //our own constructor /* rest of the class definition*/ }; distance::distance() { inch=0; feet=0; } void main() { distance d1; cout<<d1.getfeet()<<\t<<d1.getinch(); } Output : 00

The Zero-Argument Constructor The constructor provided by default by the compiler also does not take any arguments. The terms zero-argument constructor and default constructor are used interchangeably. Running example of class String It will have two data members. Both these data members will be private. The first data member will be a character pointer. It will point at a dynamically allocated block of memory that contains the actual character array. The other data member will be a long unsigned integer that will contain the length of this character array.

The Zero-Argument Constructor Example 6

The Zero-Argument Constructor Suppose S1 is an object of the class String and the string abc has been assigned to it. The address of the first byte of the memory block containing the string is stored in the cStr portion of S cStr len S1 Figure: Memory layout of an object of the class String The following two conditions should be implemented on all objects of the class String. cStr should either point at a dynamically allocated block of memory exclusively allocated for it (i.e., no other pointer should point at the block of memory being pointed at by cStr) or cStr should be NULL abc\0

The Zero-Argument Constructor When an object of the class String is created, the cStr portion of the object should be initially set to NULL (and len should be set to 0) The prototype and the definition of the constructor are as shown in example 7 Example 7: class String { char *Cstr; unsigned int len; public: String(); //our own constructor /* rest of the class definition*/ }; String::String() { Cstr=NULL; len=0; } void main() { String S1; }

Parameterized Constructors Constructors take arguments and can, therefore, be overloaded. A user-defined parameterized constructor can also be called by creating an object in the heap. The parameterized constructor is prototyped and defined just like any other member function except for the fact that it does not return any value. If the parameterized constructor is provided and the zero-argument constructor is not provided, the compiler will not provide the default constructor. Default values given to parameters of a parameterized constructor make the zero- argument constructor unnecessary. Example 8: class distance { int inch; int feet; public: distance( int=0, int=0); //default values given /* rest of the class definition*/ }; distance D1; // assigns inch and feet of D1 with 0, so need of zero argument constructor

Parameterized Constructors Here, a default value is assigned for the argument of the parameterized constructor. The constructor would handle the following statements: 1. String s1(abc); OR 2. char * cPtr = abc; String s1(cPtr); OR 3. char cArr[10] = abc; String s1(cArr); In each of these statements, we are essentially passing the base address of the memory block in which the string itself is stored to the constructor.

Parameterized Constructors Example 9 class String { char *Cstr; unsigned int len; public: String(); //our own constructor String (char * P); char * getstring() { return Cstr;} /* rest of the class definition*/ }; String::String() { Cstr=NULL; len=0; } String::String( char *str) { len= strlen(str); Cstr=new char[len+1]; strcpy(Cstr,str) } void main() { String S1; }

Parameterized Constructors The three scenarios depicting the assigning of an array to an object of the class String are:

Copy Constructor The copy constructor is a special type of parameterized constructor which copies one object to another. It is called when an object is created and equated to an existing object at the same time. The copy constructor is called for the object being created. The pre existing object is passed as a parameter to it. The copy constructor member-wise copies the object passed as a parameter to it into the object for which it is called. If the copy constructor for a class is not defined, the compiler defines it for us. But in either case, a call is embedded to it under the following three circumstances: 1.When an object is created and simultaneously equated to another existing object, the copy constructor is called for the object being created. The object to which this object was equated is passed as a parameter to the copy constructor. Example 10: A A1; A A2=A1; //copy constructor called or A A2(A1) or A *ptr = new A(A1);

Copy Constructor 2. When an object is created as a non-reference formal argument of a function. The copy constructor is called for the argument object. The object passed as a parameter to the function is passed as a parameter to the copy constructor. Example 11: void abc(A); A A1; abc(A1);//copy constructor called void abc(A A2) { // definition of abc() }

Copy Constructor 3. When an object is created and simultaneously equated to a call to a function that returns an object. The copy constructor is called for the object that is equated to the function call. The object returned from the function is passed as a parameter to the constructor. Example 12: A abc() { A A1; //remaining definition of abc return A1; } A A2=abc();// //copy constructor called

Copy Constructor Default copy constructor is defined by the compiler. Here the formal parameter is a reference, due to this no separate memory is allocated The prototype and the definition of the default copy constructor defined by the compiler are as follows: Example 13: Class A { public: A (A&); //default copy constructor }; A::A(A & Aobj) { *this=Aobj; } – Now the statement: A A2=A1; is converted as follows: A A2; A2.A(A1);//copy constructor is called for A2

Copy Constructor Example 14 class String { char *Cstr; unsigned int len; public: String( String &); //our own copy constructor /* rest of the class definition*/ }; String::String(String &ss) { if (ss.CStr==NULL) {Cstr=NULL; len=0; } else { len= strlen(ss.str); Cstr=new char[len+1]; //dynamically allocate a separate memory block and copy strcpy(Cstr, ss.Cstr); } } void main() { String S1(abc); String s2=s1; }

The destructor This function is the opposite of the constructor in the sense that it is invoked when an object ceases to exist. The definition of a destructor must obey the following rules: The destructor has the same name as the class but its name is prefixed by a tilde(~). The destructor has no arguments and no a return value. Destructors cannot be overloaded. The destructor for the class Person is thus declared as follows: class Person { public: Person(); // constructor ~Person(); // destructor };

The destructor The position of the constructor(s) and destructor in the class definition is dictated by following convention: First the constructors are declared, then the destructor, and only then other members are declared. The main task of a destructor is to make sure that memory allocated by the object (e.g., by its constructor) is properly deleted when the object goes out of scope.

Implementation of Destructors int count=0; class alpha { public: alpha() { count++; cout<< no of objects created<<count; } ~alpha() { cout<< no of objects destroyed<<count; count--; } };

void main() { cout<< enter main alpha A1,A2,A3,A4; { cout<<Enter block1 alpha A5; } Cout<<Re enter main; }

Output Enter main No of obj created Enter block1 No of objects created 5 No of objects destroyed 5 Re enter main No of objects destroyed

The destructor Consider the String class: { …………… String s1(abc); …………… } The memory allocated for object s1 itself gets deallocated when this block finishes execution. But when s1.Cstr was pointing at a memory block that was dynamically allocated in the heap area. After s1 gets destroyed, this memory block remains allocated as a locked up lost resource. The allocated memory, could be deallocated using delete operator.

Example 14 class String { char *Cstr; unsigned int len; public: ~String( ); //our own destructor /* rest of the class definition*/ }; String::~String() { if (Cstr!=NULL) delete [] Cstr; //if memory exists destroy it }

The Philosophy of OOPS One of the aims in OOPS is to abolish the use of fundamental data types. Classes can contain huge amounts of functionality (member functions) that free the application programmer from the worry of taking precautions against bugs. The class String is one such data type. By adding some more relevant functions, we can conveniently use objects of the class String. Example 15: void string ::addchar (char); // function to add a character This function appends a character to the string at which the pointer inside the invoking object points. String s1(abc); As a result of above statement, the pointer inside s1 points to a memory block of 4 bytes (including null character) Now, if we write: s1.addchar(d); // add a character to the string 1.another block of 5 bytes should get allocated 2.The string pointed by s1.ctr copied to new block 3.Character d should get appended to the string 4.The NULL character get further appended 5.The memory block pointed by S1.cstr should be deallocated 6.S1.cstr should be made to point at this new memory block

The following code can be used for adding a character to a stretchable string in the Object oriented way. One possible way of using this function is by using a loop to obtain a string from the user, which can be of any length: while (1) { ch=getche(); if (ch==\n) break; s1.addchar(ch); } Here user keeps adding character to the string, the allocated memory keeps getting stretched in a manner that is transparent to the application programmer. Such an affect is impossible with POP (char arrays) The Philosophy of OOPS

One can add a function that will replace the string associated with an object with the string that we pass to it. Consider the following prototype: Example 16: void string::setstring(char *); Suppose the following statements are executed: string s1(abc); s1.setstring(xyz); //replace abc by def The following set of events should take place: 1.a block of 4 bytes should be dynamically allocated to accommodate the string xyz 2.The string xyz should get written in that memory block with null character appended. 3.S1.cstr should be made to point at this new block of memory 4.The block of memory at which s1.cstr was previously pointing should be deallocated to prevent memory leak. With the normal parameterized constructor, there is no guarantee that the cstr is pointing at the dynamically allocated block of memory. The Philosophy of OOPS

There can be a function that will change the value of a character at a particular position in the string at which the pointer of the invoking object points. Moreover, there can be a function that reads the value from a particular position in the string at which the pointer of the invoking object points. These functions can have built-in checks to prevent values from being written to or read from bytes that are beyond the memory block allocated. Such a check is not built into character arrays. The application programmer has to put in extra efforts on his/her own to prevent the program from exceeding the bounds of the array. After we have added all such functions to the class String, we will get a new data type string that will be safe, efficient, and convenient to use.

The Philosophy of OOPS Suitably defined constructors and destructors have a vital role to play in the creation of such data types. Together they ensure the following: Data is never in an invalid state and domain constraints on the values of data members are never violated (constructor) There are no memory leaks (the destructor frees up unwanted memory). There are no run-time errors (no two calls to the destructor try to free up the same block of memory). After such data types have been defined, new data types can be created that extend the definitions of existing data types. They contain the definition of the existing data types and at the same time add more specialized features on their own. This facility of defining new data types by making use of existing data types is known as inheritance.