Constructor & Destructor

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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
Chapter 10 Introduction to Classes
 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)
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.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 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.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CONSTRUCTOR AND DESTRUCTORS
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.
Chapter -6 Polymorphism
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
Class and Objects UNIT II.
Static data members Constructors and Destructors
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.
Programming with ANSI C ++
CONSTRUCTORS & DESTRUCTORS
Constructors & Destructors.
Constructor & Destructor
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture Constructors and Destructors
Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS212: Object Oriented Analysis and Design
CONSTRUCTOR A member function with the same name as its class is called Constructor and it is used to initialize the objects of that class type with.
Contents Introduction to Constructor Characteristics of Constructor
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
Constructors/Destructors Functions Revisited
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Constructor & Destructor By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot.

We are going to learn: What is a CONSTRUCTOR? Characteristics of Constructor Constructor: In Public & In Private Default Constructor Parameterized Constructor Constructor Overloading Constructor with Default Arguments Copy Constructor Member Initialization List (MIL) Order of Constructor Invocation Implementing Constructors Destructor Function

What is a CONSTRUCTOR? “Constructor is a ‘special’ member function used for initialization of objects (data members).” “A constructor is a special member function that is a member of a class and has same name as that class.” It is used to initialize the object of the class type with a legal initial value. A constructor is called whenever an object of its associated class is created.

A Constructor is declared and defined as follows: class number { int a, b ; public: number( void); // ** Constructor Function Declared ----- }; number :: number( ) // ** Constructor Function Defined { a=0; b=0; cout<<“Hello Students! See Object is created”; }

Declaration and Definition It can be defined either inside the class definition or outside the class definition class X { int i ; public: int j,k ; X() { i = j = k =0;} }; X::X() { I = j = k =0; }

Characteristics of Constructors Constructor Functions have same name as that of class name. They do not have return types, not even void May have parameters C++ has default constructors that are called whenever a class is declared even if no function with the same name exists They should be declared in public section. They are invoked automatically when objects are created. Constructors can not be virtual. They can not be inherited.

Constructor: In Public & In Private Generally a constructor should be defined under the public section of a class, so that its object can be created in any function

int main() X obj2; //invalid class X { int i ; X() { i = j = k =0;} public: int j,k ; void check(void); // member function }; void X :: check (void) { X obj1; //valid } int main() X obj2; //invalid

Default Constructor A constructor that accept no parameter is called the default constructor. If no such constructor is defined, then compiler supplies a default constructor. For Example X obj1 ; The default constructor for class X is X::X( ) The statement X obj1 ; invokes the default constructor of the compiler to create the object x.

C++ Parameterized Constructor class number { int a, b ; public: number( int x, int y); // Constructor Func. Declared ----- }; number :: number(int x, int y) // Constructor Func Defined { a=x; b=y; } number ob1; // ***** WRONG OBJECT CREATION number ob1( 0,100 ); // ********** * Implicit Call Known as shorthand method Easy to implement and looks better number ob1 = number( 0, 100 ); // ** Explicit Call

C++ Parameterized Constructor

C++ : Constructor Overloading class number { int a, b ; public: number( ){a=0; b=0; }// Constructor 1 number( int x, int y); // Constructor 2 ----- }; number :: number(int x, int y) // Constructor 2 Defined { a=x; b=y; }

Constructor with Default Arguments class number { int a, b ; public: number( int x, int y=5 ); // Constructor ----- }; number :: number(int x, int y) // Constructor Defined { a=x; b=y; } number ob1(10); number ob2(0 , 0 );

class A { int i,j; public: X(int x=10,int y=20); }; A::X(int x,int y) i=x; j = y; } int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } obj1 obj2 obj3 i=250 j=20 i=10 j=20 i=2 j=4

Explicit Constructor Automatic conversion adds to readability. But there may be times when you do not want this automatic conversion to take place. For this purpose, C++ defines the keyword explicit.

Dynamic Initialization In earlier cases, the object is initialized with constants. The values of constants are available at compiler time. i.e., the object initialization is done at compile time. We can use constructors to initialize objects at run time. The values to be passed to the constructors are made available at runtime and then the constructor is invoked while defining the object. We call this dynamic initialization.

Dynamic Initialization

Copy Constructor A constructor that accepts a reference of its own class is called a copy constructor. It is used to declare and initialize an object from another objects of same class. The process of initialization with the help of a copy constructor is known as copy initialization.

Copy Constructor class sample { int i, j; public: sample ( int a, int b) { i = a; j = b; } sample ( sample & s) i = s.i ; j = s.j ; void print(void) { cout<<i <<“ “<<j <<“\n”; } Above constructors may be used as follows sample s1 (10,12); //1st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called

Copy Constructor

Member Initialization List (MIL) MIL is the method for initialization of the members of a class using the constructor function. It provide the alternative for providing initializations outside the constructor body. Its Need : Readability Efficiency

Example of Constructor with Arguments class sum { private: int sum1,sum2; public: sum(int,int); print() {cout<<sum1<<“ “<<sum2<<endl;} }; void main () sum obj1 (10,20); //constructor is called at this time obj1.print(); return; }

What is the output?? Example of Constructor with Arguments sum::sum (int x, int y) { sum1=9; sum2= 10; } What is the output??

the answer 9 10 (not 10 20)

Order of Constructor Invocation The objects are constructed in the order they are defined. Consider the following statement Sample s1,s2,s3; // the order of construction is s1,s2,s3. But when a class containing objects of another class as its members. In such a case, the constructor for member objects are invoked first and then only, the constructor for containing class is invoked. For example

class C { private: A objA; B objB; public: C() cout<<“Constructing C” <<endl; }}; int main() clrscr(); C objC; getch(); } class A { public: A() cout<<“Constructing A” <<endl; }}; class B B() cout<<“Constructing B”

C++ : DESTRUCTOR FUNCTION A class can have another special member function called destructor, which is invoked when an object is destroyed. This function is reverse of the constructors. A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ number( ); Does not have a return type (not even void) Cannot have parameters Called automatically when the class object is destroyed De-allocates storage allocated to a class Should be public (or protected) A class cannot have more than one destructor.

C++ DESTRUCTOR FUNCTION public: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { //close (infile); //close (outfile); Cout<<bye; }

Who is Bjarne Stroustrup??

Thank You