Constructors and destructors

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
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.
Chapter 12: Adding Functionality to Your Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
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.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
 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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
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.
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.
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.
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.
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.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Constructors and Destructors
Learning Objectives Pointers as dada members
Static data members Constructors and Destructors
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Constructors & Destructors.
Constructor & Destructor
Constructors & Destructors
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture Constructors and Destructors
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructor & Destructor
Introduction to Classes
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Inheritance Dr. Bhargavi Goswami Department of Computer Science
CONSTRUCTORS AND DESRUCTORS
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Pointers Dr. Bhargavi Goswami Department of Computer Science
Constructors and Destructors
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Java Programming Language
Chapter 9 Introduction To Classes
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
Constructors & Destructors
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Constructors and destructors Dr. Bhargavi Dept of CS CHRIST 9426669020

introduction Can we initialize the variables of objects when they are created? Similarly, when a variable of built-in type goes out of scope, can compiler automatically destroys the variable? Yes. Using: Constructors, for automatic initialization of objects. Destructors, for destroying objects when no more required.

Constructors Definition: special function which is invoked without explicit call whose task is to initialize objects of its class. When invoked? When object is created. Why called constructors? Bcoz it constructs values for data members. Explicit call statement is not required to call a constructor. Use? Convenient when number of obj is large.

Types of constructors Default constructor Parameterized constructor Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors Types of constructors Default constructor Parameterized constructor Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors

Characteristics of constructors They should be declared in public section. Invoked automatically at object creation. Have no return type. Thus, cannot return values. Cannot be inherited. Though, derived class can call base class constructors. May have default arguments. Cannot be virtual. We cannot refer to address of constructors An obj with constructor cannot be used as union Makes implicit call to ‘new’ and ‘delete’ operators for dynamic memory allocation

Default constructors If no constructor is defined, compiler calls default constructor without body. Default constructors are constructors without explicit definition and without arguments. Eg. A::A() Eg. classConstructorDefaulVal.cpp Eg. classConstructor.cpp Eg. privateConstructor.cpp

Parameterized constructors Practically we cannot initialize all the objects with same values. What if I want to initialize with different values to different objects? Is it possible? Yes. How? Parameterized constructors can take arguments that can be passed with individual values for each object. Definition: Constructors that can take arguments are called parameterized constructors.

Parameterized constructors Pl note that parameterized constructors require argument passing. Constructors can also be defined as inline. Constructor can accept a reference to its own class as parameter. We also call it copy constructor. Eg.6_1_ExampleOfParameterizedConstruncto r.cpp Eg. classParameterisedConstructor.cpp

overloading constructors Can we copy the values of first object into second object using constructor? Yes. Lets see how. See the example given below. Eg. 6_2_OverloadedConstructors.cpp Eg. contructor_Overloading.cpp

Constructors with default arguments Is it possible to define constructors with default arguments? Yes. How? Eg. complex C(float real, float imag=0); complex C(5.0); complex C(2.0,3.0); Eg. 6_2_OverloadedConstructors.cpp, we have already done. Eg. classConstructorDefaulVal.cpp, we have already done. Note: Its imp to distinguish between default constructors A::A() and default argument constructor A::A(int=0). They cannot be implemented together to avoid ambiguity.

Dynamic initialization of objects Can class objects be initialized dynamically? Means? During runtime provide the initialization values to objects. Is it possible? Yes. Advantage: Multiple formats are available for initialization using overloaded constructors. Provides flexibility of using different formats at runtime. Eg. 6_3_DynamicInitializationOfObject.cpp

Copy constructor We already did this during constructor overloading. Use? Initialize objects with values of another existing object. Eg. Integer i2 = i1; Integer i2(i1); Following statement is not copy constructor but, its legal and assignment operator overloading which we will see next chapter. i2 = i1; Copy constructor takes a reference to an obj of same class as itself as an argument. We cannot pass argument by value to copy constructor. When no copy constructor is defined, compiler supplies its own copy constructor. Eg. 6_4_CopyConstructor.cpp Eg. copyConstructorDestructor.cpp

Dynamic constructors Constructor can also allocate memory while creating objects. Use? Assign right amount of memory as per the requirement. Thus, optimum utilization of memory. How to do that? Using new operator. Eg. dynamicConstructor.cpp Eg. 6_5_ConstructorsWithNew.cpp

Constructing two dimensional array Eg. 6_6_ConstructingMatrixObjects.cpp

Const objects Can we have objects as const? Yes. Y? To avoid accidental modification of obj values. Eg. const matrix X(m,n); //const obj X We cannot modify m and n. Constant obj can call only const functions. High security implementation.

destructors Use? To destroy objects created by constructors. Preceded by tilde(~) sign. Eg. ~integer(){}; Never takes any argument. Never returns any value. Invoked implicitly by compiler during exit. Used to clean up storage not accessible. It is a part of good programming practice. New – delete, same, constructor – destructor. Objects get destroyed in reverse order of creation. Eg. 6_7_ImplmentationOfDestructors.cpp Eg.6_8_MemoryAllocationToAnObjectUsingDestrucor.c pp

END OF CHAPTER 6 THANK YOU