Lecture Constructors and Destructors

Slides:



Advertisements
Similar presentations
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.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Classes Separating interface from implementation
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
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 and Type Conversions
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Object-Oriented Programming Using C++ CLASS 27.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
 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.
CONSTRUCTOR AND DESTRUCTORS
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
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 CSE 2341 Object Oriented Programming with C++ Note Set #5.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
 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.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
1 Class 19 Chapter 13 – Creating a class definition.
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
By Muhammad Waris Zargar
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 ++
CSC241: Object Oriented Programming
CONSTRUCTORS & DESTRUCTORS
Constructors & Destructors.
Object Lifetime and Dynamic Objects
Constructor & Destructor
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Polymorphism Lec
Dynamic Memory Allocation Reference Variables
Constructor & Destructor
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Object Oriented Programming (OOP) Lecture No. 9
CONSTRUCTORS AND DESRUCTORS
Constructor Spl member fn auto ini of object
Constructors and destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
Constructors/Destructors Functions Revisited
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes: A Deeper Look, Part 1
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
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
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Lecture 15-17 Constructors and Destructors

Contents Constructor Overloading Dynamic Initialization of objects Dynamic Constructors Destructor

Constructor Overloading Constructor can be overloaded in similar way as function overloading. . Overloaded constructors have the same name (name of the class) but different number (or types or both) of arguments. Depending upon the number and type of arguments passed, specific constructor is called.

Example of Constructor Overloading #include<iostream> using namespace std; class example { private: int a,b; public: example(); //Default Constructor example(int, int); //Parameterized Constructor example(example &); //Copy Constructor void display(); }; example::example() cout<<“Constructor is called”; } example::example(int x, int y) { a=x; b=y; } example::example(example &p) a=p.a; b=p.b; void example::display() cout<<a<<endl<<b; int main() example e1; example e2(4, 5); example e3(e2); e3.display(); return 0;

Dynamic Initialization of Objects Class objects can be initialized dynamically also i.e. the initial value of an object can be provided during run time. Advantage: Various initialization formats can be provided using overloaded constructors. Provides the flexibility of using different format of data at run time depending upon the situation.

Example #include<iostream> using namespace std; class factorial /*Program to find factorial of a number using constructor*/ #include<iostream> using namespace std; class factorial { int n; public: factorial(int); void display(); }; factorial::factorial(int number) n=number; } void factorial::display() { int fact=1; if(n==0) cout<<"\n factorial=1"; else for(int i=1; i<=n; i++) fact=fact *i; } cout<<"\n factorial="<<fact; int main() int x; cout<<"\n enter the number to find its factorial"; cin>>x; factorial obj(x); //dynamic initialization of object obj.display(); return 0;

Dynamic Constructor The constructor can also be used to allocate memory while creating objects. This enables the system to allocate the right amount of memory for each object when the objects are not of the same size. Allocation of memory to objects at time of their construction is known as dynamic construction of objects. The memory is allocated with the help of new operator.

Example 1 OUTPUT: The value of object's pointer is:100 #include<iostream> using namespace std; class test { int *ptr; public: test(); test(int); void display(); }; test::test() ptr=new int; *ptr=100; } test::test(int t) *ptr=t; void test::display() { cout<<"The value of object's pointer is:"<<*ptr<<endl; } int main() test obj; test obj1(40); obj.display(); obj1.display(); return 0; OUTPUT: The value of object's pointer is:100 The value of object's pointer is:40

Example 2 OUTPUT: Welcome to C++ World void example::display() { #include<iostream> using namespace std; class example { char *name; int length; public: example(); example(char *); void display(); }; example::example() length = 0; name = new char[length+1]; } example::example(char *e) length = strlen(e); name = new char[ length+1]; strcpy( name,e); void example::display() { cout<<name<<endl; } int main() char *a= “Welcome to"; example e1(a), e2(“C++"), e3(“World"); e1.display(); e2.display(); e3.display(); return 0; OUTPUT: Welcome to C++ World

Introduction to Destructors A destructor is a special member function of class which is used to destroy the objects that have been created by a constructor. The destructor is called automatically by the compiler when the object goes out of scope. Like a constructor, the destructor is a member function whose name is the same as class name but is preceded by a tilde ~ sign. class A { public: ~A(); //Destructor declaration };

Introduction to Destructors (Cont..) Destructor never takes any argument. Destructors does not return any value. Destructor cannot be overloaded. Whenever new is used to allocate memory in the constructors, delete should be used to free that memory. Note: Objects are destroyed in reverse order of their creation.

Example OUTPUT: a=10 Object is destroyed #include<iostream> using namespace std; class ABC { int a; public: ABC(int); //Constructor void display(); ~ABC(); //Destructor }; ABC::ABC(int x) a=x; } void ABC::display() cout<<"a="<<a; ABC::~ABC() //Definition of Destructor { cout<<"\nObject is destroyed"; } int main() ABC obj1(10); obj1.display(); return 0; Destructor is automatically called by the compiler once the object goes out of scope. OUTPUT: a=10 Object is destroyed

Thank You