Constructors and Destructors

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.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter 7: User-Defined Functions II
Review of C++ Programming Part II Sheng-Fang Huang.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
OPERATOR OVERLOADING Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
More C++ Features True object initialisation
Chapter 9 Classes: A Deeper Look, Part I Part II.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Programming with ANSI C ++
Chapter 7: User-Defined Functions II
Operator Overloading; String and Array Objects
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.
CISC181 Introduction to Computer Science Dr
Concepts of Constructors and Its Types
CS410 – Software Engineering Lecture #11: C++ Basics IV
C++, OBJECT ORIENTED PROGRAMMING
Constructor & Destructor
Constructors & Destructors
Pointers Revisited What is variable address, name, value?
This pointer, Dynamic memory allocation, Constructors and Destructor
OPERATOR OVERLOADING.
Introduction to Classes
Member functions and Member data in C++ classes
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Operator Overloading; String and Array Objects
Object Oriented Programming (OOP) Lecture No. 9
Constant pointers and pointers to constants
Operator Overloading; String and Array Objects
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
Classes, Constructors, etc., in C++
9-10 Classes: A Deeper Look.
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes: A Deeper Look, Part 1
A Deeper Look at Classes
DYNAMIC MEMORY MANAGEMENT
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS148 Introduction to Programming II
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Constructors and Destructors

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, does not return any thing and not even void The prototype is - <class name> (<param list>) It guarantees the initialization of the data members of the class Domain constraints on the values of data members can also be implemented via constructors

The compiler embeds a call to the constructor for each object when it is created class A { int x; public : void setx (const int = 0); int getx ; } ; // end of A.h # include”A.h” void main() { A A1 ; } //end of Amain.cpp

The statements are transformed as follows A A1 ; //memory allocated for the object A1.A(); as A(&A1);// constructor called implicitly Similarly, the constructor is called for each object that is created dynamically in the heap by the new operator A * Aptr; Aptr = new A; as 2 statements Aptr = new A; Aptr -> A(); as A(Aptr) ; //constructor called implicitly by the compiler Constructors do not allocate memory for objects but are the member functions that are called after memory has been allocated for the object The compiler prototypes and defines constructor for us with out any statements in the definition

class A { ….. public: A(); // prototype inserted by compiler ….}; A :: A() { } //empty defn. inserted by compiler The compiler defines the constructor in order to resolve the call to the constructor that it compulsorily places for the object being created Explicit call to the constructor for an existing object is forbidden A1.A(); // not legal C++ code If we define constructor, the compiler does not define, but embeds implicit calls to the constructor

Being a non-static member, constructor 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. Hence members of the invoking object can be accessed from with in the definition of the constructor class A { int x; public : A() ; void setx (const int = 0); int getx ; } ; // end of A.h

# include”A.h” #include<iostream.h> A :: A() { cout << “constructor of class A called\n”; } // definitions of rest of functions of class A // end of A.cpp void main() { A A1 ; cout << “ End of Program \n “; } //end of Amain.cpp Output : constructor of class A called End of Program

For the class Distance, we would like to set the values of ‘iFeet’ and ‘fInches’ to 0 and 0.0 { public: Distance (); //our own constuctor //rest of class Distance }; //end of Distance.h #include “Distance.h” Distance :: Distance ( ) { iFeet =0; fInches = 0.0 ; } //definitions of rest of functions of class Distance // end of Distance .cpp

#include “Distance.h” #include<iostream.h> void main() { Distance d1; //constructor called cout<<d1.getFeet()<<””<<d1.getInches()<<endl; } //end of DistTest.cpp Output 0 0.0 There is a guaranteed initialization of data members of the objects of the class Distance The constructor that does not take any arguments – default / zero-argument constructor We define a class instead of character arrays

The class ‘String’ will have two private data members – (i) a character pointer pointing at dynamically allocated block of memory that contains the actual character array (ii) a long unsigned integer containing the length of this array class String { char * cStr; long unsigned int len; public : String(); // prototype of the constructor // rest of the class String }; // end of String.h

Suppose s1 is the object of class String 101 27 cStr 101 3 a b c \0 len s1 Suppose s1 is the object of class String We want to implement the following two conditions (i)cStr should point either to the dynamically allocated block of memory exclusively allocated for it or NULL (ii) There should be no memory leaks When the object of the class is created, cStr should initially set to NULL and len to zero

#include “String.h” String :: String() // definition of the constructor { cStr = NULL ; len = 0 ; } // definitions of rest of functions of class String // end of String.cpp Parameterized constructors Constructors take arguments and can, therefore be overloaded public : Distance(); //part of Distance.h Distance(int, float); // parameterized constructor

Distance :: Distance ( ) { iFeet =0; fInches = 0.0 ; } Distance :: Distance ( int p, float q) { iFeet =p; setInches(q) ; } //function definitions void main() //object created in stack { Distance d1(1, 1.1); //parameterized constructor called cout<<d1.getFeet()<<“ ”<<d1.getInches(); } Output: 1 1.1

void main() //object created in heap { Distance * dPtr; dPtr = new Distance d1(1, 1.1); //parameterized constructor called cout<<dPtr->getFeet()<<“ ”<<d1->getInches(); } Output: 1 1.1 If only the parameterized constructor is provided with out zero-argument one, the compiler will not provide the default constructor. On compiling Distance d1; // ERROR: no matching constructor Hence zero-argument constructor is a must

The formal arguments of the parameterized constructor can be assigned default values But, in that case zero-argument constructor should not be provided, because an ambiguity error will arise when we attempt to create an object with out passing any values for the constructor Distance ( int =0, float = 0.0 ) ; // default values If we write, Distance d1; // ambiguity error Parameterized constructor for class ‘String’ String s1(“abc”); //constructor would handle these char * cPtr=“abc” ; String s1(cPtr) ; //statements char cArr=“abc” ; String s1(Arr) ;