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.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
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];
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
C++ Review (3) Structs, Classes, Data Abstraction.
Memory Management Issues, Solutions, and Examples.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
 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)
More C++ Features True object initialisation
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CONSTRUCTOR AND DESTRUCTORS
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
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 ++
CONSTRUCTORS & DESTRUCTORS
Constructor & Destructor
Class Operations Pointer and References with class types
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Indirection.
Destructor CSCE 121.
Review Chapter 10 PPT for full coverage.
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
SPL – PS3 C++ Classes.
Presentation transcript:

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 name. Constructor function cannot invoke using class object. They invoked automatically when an object is created. Constructor fun does not having any return type, they are mainly used to initialized the class variables. Constructor funs are defined in the public section of the class

3 constructor Syntax: class name (argument list) { constructor body; } Eg: Class s4 { int a,b; public: s4 ()// constructor fun {a=b=0;} }; Main() { s4 s;// constructor fun invoked automatically when an object created }

4 constructor Constructors are 7 type 1)Default constructor 2)Parameterized constructor 3)Multiple constructor 4)Default argument constructor 5)Dynamic initialization of object 6)Dynamic constructor 7)Copy constructor

5 constructor Default constructor The constructor fun does not having parameters Class p {int a,b; Public: P() {a=b=10;} }; Q: Fibonacci series

6 constructor Parameterized constructor The constructor fun which having parameters class pp { int a,b; Public: pp( int x,int y) {a=x;b=y;} };

7 constructor Multiple constructor (constructor overloading) One class contains more than one constructor function Default constructor and parameterized constructor in one class class pp { int a,b; Public: pp() // default constructor {a=b=10;} pp( int x,int y)// parameterized constructor {a=x;b=y;} };

8 constructor Default argument constructor Constructor fun with default arguments class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} }; A class does not allow to contain both default constructor and default argument constructor, it leads to an ambiguity error

9 constructor Dynamic initialization of object Class object initialized dynamically ie initial values are provide during run time, by reading values from main prog class pp { int a,b; Public: pp( int x,int y) {a=x;b=y;} };

10 constructor Dynamic constructor The memory allocation of the object at run time using the memory management operator new New operator is used to allocate memory space at run time Here 2 bytes of memory is allocated and address is return to the pointer variable p

11 constructor memory management operator This syntax is used to allocate memory space and assign initial value This syntax is used to allocate an array of memory size

12 constructor memory management operator delete Delete operator is used to destroy or release the memory space allocated by new operator

13 constructor Dynamic constructor The memory space for each class variables are allocated using the new operator class p { char *s; public: P()// dynamic constructor {s=new char[20]; } };

14 constructor Copy constructor copy constructor is used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. The argument of copy constructor is the address of class object class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} pp( pp &ob) // copy constructor {a=ob.a; b=ob.b;} };

15 Destructor Like Constructors, destructors are also special member functions in C++, whose name is same as class name, use to destroy or release allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are invoked automatically like constructors, at end of a program or end of a block. General Syntax of destructors ~ classname();

16 Destructor Destructor does not have return type and argument, they are used to destroy the allocated memory of an object

17 constructor