Constructor & Destructor

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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 15: Operator Overloading
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
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.
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.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
 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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
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.
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
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
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.
Programming with ANSI C ++
Function Overloading.
Constructors & Destructors.
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Constructor & Destructor
Operator Overloading
CS212: Object Oriented Analysis and Design
Contents Introduction to Constructor Characteristics of Constructor
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Java Programming Language
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Chapter 6 Polymorphism.
Constructors and destructors
C++ Class Members Class Definition class Name { public: constructor(s)
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
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Presentation transcript:

Constructor & Destructor Chapter No 3 Constructor & Destructor Constructor:- Constructor is a special member function whose task is to initialize the object Constructor Declaration:- class Class_Name { public: Constructor _Name( ); }; Constructor Declaration:- Class _Name :: Constructor _Name( ) { - - - - - - - - - - - - } Constructor Call :- void main( ) { Constructor _Name ( ); }

Characteristics of Constructor :- Constructor is a special member function because it has same name as class name It cannot have written Data Type It Declare in Public Section Only It call automatically when object is created It cannot be inherited It can have Default argument It cannot refer there address Type of constructor :- Default constructor Parameterized constructor Copy constructor Dynamic constructor

Default constructor :- A constructor that accept no parameters is called the Default Constructer The default constructor for emp is emp : : emp ( ) If no such a constructer is defined then the compiler supplies a default constructor Constructor Declaration:- class Class_Name { public: Constructor _Name( ); }; Constructor Declaration:- Class _Name :: Constructor _Name( ) { - - - - - - - - - - - - } Constructor Call :- void main( ) { Constructor _Name ( ); }

Parameterized constructor :- The constructor that can take argument are called Parameterized Constructer The constructor integer ( ) may be modified to take argument as shown below Constructor Declaration:- class integer { int m,n; public: integer (int x,int y) - - - - - - - - - - - - - }; Constructor Definition:- integer : : integer( int x,int y ) { m=x; n=y; } Constructor Call :- integer int ( 10,20 );

Copy constructor:- A Constructer references to it’s own class as a parameter which is called as copy constructor Constructor Declaration:- class time { public: time( time Q ); }; Constructor Definition:- time : : time( time Q) - - - - - - - - - - - - } Constructor Call :- time T ( п );

Constructor Calling:- Dynamic constructor :- Allocation of memory to object at the time of construction is known as dynamic construction Constructor Calling:- Constructor can be called by using two type 1) Explicitly Calling :- Syntax:- class_Name object_Name = Class_Name ( Parameter_List) e.g. test T = test (20,35) ; 2) Implicitly Calling :- Syntax:- class_Name object_Name( Parameter_List ) e.g. test T (20,35)

Multiple Construction in a Class Multiple Construction used in the same class is known as Multiple Construction. integer( ); integer( int, int ); integer(integer & i); class integer { int m, n; public : integer ( ) { m=0; n=0; } integer (int a, int b) m=a; n=b; } integer (integer & i) { m= i.m; n=i.n; };

Characteristics of Destructor :- Destructor is used to destroy the object that has been created by constructor. The Destructor is also a special member function whose name is same as the class name but it processed by a tild operator. e.g. ~ integer ( ); Characteristics of Destructor :- A Destructor can not pass any argument A Destructor can not return any value therefore it does not have any return type It will be invoked implicitly by the compiler when exit from program to cleanup storage that is no longer accessible Whenever a new is used to allocate memory in the constructor we should use delete to free from memory

Function Overloading:- The overloading means use of the same thing for different purpose C++ also permits the overloading of function. Declaring Function:- void add ( ); int add ( ); void add (int a); void add (int a, int b); void add (int a, float b); int add (float a, float b); Function Calling:- add ( ); p= add ( ); add (10 ); add ( 10,20); add ( 10,20.5); add ( 10.5,20.5);

Function Selection invoke Following Steps:- The compiler first tries to find an exact match in which the type of actual argument are same. If exact match is not find the compiler use the integral promotions to the actual argument such as char to int. When either of them fails the compiler tries to use the built in conversation to the uses the function whose match is unique. If all of the steps fail then the compiler will try the user define conversation in combination will integral promotion & built in conversions to find a unique match.

Operator Overloading C++ permits to add two variable of user define types with same syntax is applied to the basic type This means that C++ has the ability to provide the operator with a special meaning for data type. The mechanism of giving such special meaning to an operator is known as operator overloading. Operator overloading provide a flexible option for the creation of new definition for most of new definition's for most C++ Operator.

Rules for operator Overloading Only exciting operator can be overloaded new operator cannot be created We cannot change the basic meaning of the operator e.g. + cannot use for subtraction Overloaded operator follows the rules of the original operator The overloaded operator must have atlist one operand this is user define time There are some operator that cannot be overloaded they are as follows Size of Size operator . Class member access operator .* Class member access operator : : Scope resulation operator ? : Conditional operator We cannot use friend function to overload certain operator which is given as follows = Assignment ( ) Function Call [ ] Subscripting > Class member access Unary operator overloaded by means of member function take no explicite argument & no return explicite value but this overloaded by means of friend function take one reference argument

Rules for operator Overloading Binary operator overloading through a member function take one explicite & those which are overloaded through friend function take two argument When using binary operator overloaded through a member function the left hand operand must be a object or relevant class Binary arithmetic operator such as +, -, *, /. Must explicitly written a value they must not attempt change their own ourgument