CONSTRUCTORS & 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

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.
Constructors & Destructors Review CS 308 – Data Structures.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 CSC241: Object Oriented Programming Lecture No 03.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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
Constructors and Destructors
Learning Objectives Pointers as dada members
Access Functions and Friend Functions
2 Chapter Classes & Objects.
Static data members Constructors and Destructors
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 ++
Concepts of Constructors and Its Types
Constructors & Destructors.
Chapter 5 Classes.
Constructor & Destructor
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture Constructors and Destructors
C++ Classes & Object Oriented Programming
Subject Name: Object Oriented Programming with C++
Constructor & Destructor
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
CONSTRUCTORS AND DESRUCTORS
Operators.
Constructor Spl member fn auto ini of object
Polymorphism Polymorphism
Constructors and destructors
Constructors and Destructors
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
More C++ Classes Systems Programming.
Object Oriented Programming (OOP) Lecture No. 12
SPL – PS3 C++ Classes.
Object Oriented Programming
Presentation transcript:

CONSTRUCTORS & DESTRUCTORS

Constructors A special member function having same name as that of its class which is used to initialize some valid values to the member variables.

Key points while defining constructor A constructor has same name as that of the class to which it belongs. A constructor is executed automatically whenever the object is created A constructor doesn`t have a return type, not even void We can declare more than one constructor in a class. These constructor differ in there parameter list. If you don’t provide a constructor of your own then the compiler generates a default constructor (expects no parameters and has an empty body). A constructor can preferably be used for initialization and not for input\output operations.

Contd.. Constructor should be declared in the public section of the class. If it is not declared in public section of the class then the whole class become private . By doing this the object of the class created from outside cannot invoke the constructor which is the first member function to be executed automatically.

Syntax class CLASSNAME { public: CLASSNAME([parameter list]); };

Example #include<iostream.h> #include<conio.h> class Rectangle { private: int length,breadth; public: Rectangle() Length=5,breadth=6; }

int area(int a,int b) { int a=(length*breadth); cout<<“area is”<<a; } }; void main() clrscr(); Rectangle r1; r1.area(); getch();

TYPES OF CONSTRUCTOR: 1.Default Constructor 2.Parameterized Constructor 3.Copy Constructor 4.Dynamic Constructor

Parameterized Constructor In order to initialize various data elements of different objects with different values when they are created. C++ permits us to achieve this objects by passing argument to the constructor function when the object are created . The constructor that can take arguments are called parameterized constructors

Parameterized constructor #include<iostream.h> class Rectangle { private: int length,breadth; public: Rectangle(int a,int b) length=a,breadth=b; }

int area() { int a=(length int area() { int a=(length*breadth); cout<<“area is”<<a; } }; void main() clrscr(); Rectangle r1(5,6); Rectangle r2(7,8); r1.area(); getch();

Multiple Constructors in a class class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle() { xpos = 0; ypos = 0; } rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; rectangle::rectangle(float h, float w) { height = h; width = w; }

Copy constructor A constructor is a constructor that creates a new object using an existing object of the same class And initializes each data member of newly created object with corresponding data member of existing object passed as argument. since it creates a copy of an existing object so it is called copy constructor.

Example Class counter { Int c; public: Counter(int a) //single parameter constructor C=a; } Counter(counter &ob) //copy constructor cout<<“copy constructor invoked”; C=ob.C;

Void show() { cout<<C; }; Void main() Clrscr(); Counter C1(10); Counter C2(C1);// call copy constructor C1.show(); C2.show(); getch(); }

#include<iostream #include<iostream.h> Class code { int id; Public: code() { } code(int a) { id = a; } code (code & x) { id = x. id; } void display(void) { cout<<id; } }; Int main() code A(100); code B(A); code C = A; code D; D = A; A.display(); B.display(); C.display(); D.display(); return 0; }

INITIALIZER LIST So far we have discussed constructor in which the initialization is performed in its body. Another alternate way is Initializer list Initializer list is placed between the parameter list and opening braces of the body of constructor. When the constructor is declared inside and defined outside the class using scope resolution then the member initialization list can only be specified within the constructor definition and not its declaration.

An list allows initialization of data members at the time of the creation which is more efficient as values are assigned before the constructor even starts to execute. rectangle(int a,int b):length(a),length(b){…}

STATIC DATA MEMBERS We have discussed that each object of a class hold its seprate copy of all the data members of the class which are not shared by different objects of same class. A static data members is a data member that gets memory only once for the whole class no matter how many objects of a class are created. This common copy is shared by all objects of its class.

class CLASSNAME { ………… static datatype DATAMEMER; ………………….. }; datatype CLASSNAME::DATAMEMBER=initial value ;

The static datamember are defined and decalred seprately. Because the normal data member gets there memory corresponding to their objects ,the static data members which are not associated with any object needs to be defined expilcitly outside the class.

Destructors Is a member function having same name as that of constructor but it is preceded by a tilde(~) symbol and is executed automatically when object of a class is destroyed

Key points while defining destructor A destructor has same name as that of the class to which it belongs preceded by tilde(~)sign. A destructor is executed automatically whenever the object is destroyed. A destructor doesn`t have a return type, not even void and no arguments There is only one destructor in class . If you don’t provide a destructor of your own then the compiler generates a default destructor A destructor can be used to deallocate memory for an object and declared in the public section.

A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing temporary variables ends (4) a delete operator is called 

Need for Destructors To de-initialize the objects when they are destroyed To clear memory space occupied by a data member.

syntax class CLASSNAME { ………………. public: ~CLASSNAME([parameter list]); };

Example #include<iostream.h> #include<conio.h> class counter { int id; public: counter(int i) Id=I; cout<<“contructor of object with id=”<<id; }

~counter() { cout<<“destructor with id=”<<id; } }; void main() counter c1(1); counter c2(2); counter c3(3); cout<<“\n end of main”; getch();

constructor of object with id=1 constructor of object with id=2 Output constructor of object with id=1 constructor of object with id=2 constructor of object with id=3 End of main destructor with id=3 destructor with id=2 destructor with id=1