CONSTRUCTOR AND DESTRUCTORS

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 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
 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.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and 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.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
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:
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter -6 Polymorphism
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Constructors and Destructors
Operator Overloading Ritika Sharma.
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.
CONSTRUCTORS & DESTRUCTORS
Operator Overloading.
Constructors & Destructors.
Constructor & Destructor
Constructors & Destructors
Chapter 15: Overloading and Templates
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Oriented Analysis and Design
Constructor & Destructor
Operator Overloading
Contents Introduction to Constructor Characteristics of Constructor
Constructor Spl member fn auto ini of object
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Chapter 6 Polymorphism.
Constructors & Destructors
Presentation transcript:

CONSTRUCTOR AND DESTRUCTORS Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor :   Constructor is a special member function which enables an object to initialize itself when it is created. Important characteristics of constructor are It should be defined in public section only. It has same name as the name of the class. It is invoked automatically when an object of its associated class is created. Constructor does not return any value. We cannot refer to their address. An object with a constructor ( or destructor) cannot be used member of a union. Like other c++ functions, they can have default arguments.

Syntax for declaration of constructor class_name { public: class_name( parameter list) function body //constructor function } ~ class_name( parameter list) function body // destructor function }; Types of constructors    The three basic types of constructor available in c++ are : Default constructor. Parameterized constructor. Copy constructor.

Default constructor :- A constructor that accepts no parameters is called default constructor. Example :- Class integer { int m,n; Public: integer() //default constructor m=10; n=10; } }; When a class contains a constructor like the one defined above, it is clear that an object created by the class will be initialized automatically. The declaration Integer int1; // object int1 is created Note only creates the object int1 but also initialized its data member m and n to 10.

Parameterized constructor :- In default constructor every objects are initialized with the same values but in many cases, it may be necessary to initialize the various data elements of different object with different values when they are permits us to achieve this objective by passing argument to the construct function. When the objects are created, the constructors that can take arguments are called parameterized constructors. E.g. Class integer { int m,n; Public: integer(int x, int y) // parameterized constructor m=x; n=y; } }; When a constructor has been parameterized, the object of declaration statement as integer int1; integer int1=integer(0,100); This statement creates an integer objects int1 & passes the values 0 and 100 to it. integer int1(0,100);

Constructor with default arguments :- Constructor with default argument means an argument value is specified in constructor declaration which is used if the corresponding actual parameter value is omitted when constructor is invoked. While declaring the function the arguments without default values are placed first and those with default values are placed later as the first or middle arguments can not be omitted. The default arguments are specified in function prototype if function is defined later else those are given in function definition. Syntax for default argument : return_type function_name(data_type parameter_name = constant_value); Example : int test(int a, int b = 5); // function test is having argument b with default value.

Multiple constructors (constructor overloading) :- More than one constructor can also be created in a class, then it is known as multiple constructors in a class or constructor overloading. Example :- Class integer { int m,n; Public: Integer() // default constructor m=10; n=10; } integer(int x) // parameterized constructor m=x; n=x; integer(int x,int y,int z=100) // constructor with default argument n=y+z; };

Copy constructor :- A constructor can accept a reference to its own class as a parameter which is called as a copy constructor. Therefore the following constructor is valid Class Time { …… Public: Time(Time&); //copy constructor }; A copy constructor can be used to declare and initialized an object of another object. For example, the statement Time T2(T1); Would define the object T2 and at the same time initialize it to the values of T1. The process of initializing through a copy constructor is known as copy initialization. But the statement T2=T1; Will not invoke the copy constructor. However , if T1 & T2 are objects, this statement is legal and simply assigns the values of T1 to T2 member by member.

//Example of copy constructor #include<iostream.h> #include<conio.h> class sample { int number; public: sample(int x) number=x; } sample(sample &m) // copy constructor number=m.number; void display() cout << "\n a = " << number; }; main() clrscr(); sample a(40); sample b(a); sample c=a; cout << "\n The data for a object \n"; a.display(); cout << "\n The data for b object \n"; b.display(); cout << "\n The data for c object \n"; c.display(); getch();

Destructor function :- It is a special function just opposite constructor. The important characteristics of destructor are :- 1) Destructor function also has the same name as the class but preceded by a tilde (~). 2) When an object is out of scope destructor function is called automatically. 3) The destructor has no arguments and also does not return any value. Syntax for declaration of destructor class class_name { public: class_name( parameter list) function body //constructor function } ~ class_name( parameter list) function body // destructor function };

Example of Destructor #include<iostream.h> #include<conio.h> class sample { int a,b; public: sample() a=100; b=200; } ~sample() cout << "\n OBJECT IS DESTROYED "; void display() cout << "\n a = " << a << "\n b = " << b; }; main() clrscr(); sample x; x.display(); sample y; y.display(); cout << "\n The y object is destroyed"; sample z; z.display(); cout << "\n the z object is destroyed "; getch();

Operator overloading :- Overloading unary and binary operators. : It allows to extend functionality of an existing operator to operate on user defined data types also. The operation of operator when the operands are of user defined data types depends upon operator overloading done in program. Unary operator overloading : Overloading of operators operating on single operand. There should be no arguments to the operator function. Binary operator overloading : Overloading of operators operating on two operands. There should be one parameter to the operator function. Following operator can’t be overloaded :-   1) :: Scope resolution operator. 2) Sizeof operator 3) ?: conditional (ternary) operator 4) * pointer to class member operator 5) . dot operator

Rules for overloading operators :- Syntax for declaration of operator function :- For declaration of the operator overloading return_ datatype operator operator_to_overload(arguments); e.g. int operator + (test t); // operator function to overload '+' operator To declare a class distance to store distance in feet and inches. Inches will increment by 1 if ++ operator is used with the object i.e. ++ operator is overloaded. Rules for overloading operators :- 1. Only existing operator can be overloaded. new operator cannot be created. 2. The overloaded operator must have at least one operand that is of user define type. 3. We cannot change the basic meaning of an operator. that is cannot redefine the plus(+) operator to subtract one value from the other. 4. Overloaded operator follow the syntax rules of original operator.