Constructors & Destructors

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
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];
Classes Separating interface from implementation
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CONSTRUCTOR AND DESTRUCTORS
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
1 Introduction to Object Oriented Programming Chapter 10.
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.
Constructors and Destructors
Learning Objectives Pointers as dada members
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.
Review What is an object? What is a class?
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
Constructors & Destructors.
Constructor & Destructor
Class Operations Pointer and References with class types
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Constructor & Destructor
Operator Overloading
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
NAME 436.
Classes: A Deeper Look, Part 1
Constructors and destructors
Chapter 9 Introduction To Classes
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Constructors & Destructors Chapter 3 Constructors & Destructors

Constructors A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator. class A { int i; public: A(); //Constructor declared }; A::A() // Constructor definition { i=1; }

Types of Constructors Constructors are of three types : Default Constructor Parameterized Constructor Copy Constructor

5 Default constructor Default constructor is the constructor which doesn't take any argument. If the programmer does not specify the constructor in the program then compiler provides the default constructor automatically. Default constructor get called automatically when objects of class get created. We don’t need to call default constructor explicitly. Like using object name and (.) operator

Parameterized Constructor These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument. This constructor is can’t be called using object name and (.) operator. We have to call this constructor when object is created & by specifying appropriate parameters

Copy constructor The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: Initialize one object from another of the same type. copy an object to pass it as an argument to a function.

Multiple constructor in class/constructor overloading C++ permits us to use all these constructor in the same class. Example class Integer { int m,n; public: integer(); // default constructor integer(int a,int b); //parameterized constructor integer(integer); // copy constructor }

When more than one constructor function is defined in the class it is known as Constructor overloading

Constructor with default argument It is possible to define constructor with default argument. Eg. complex(int a,int b=0); When we call that constructor it is not necessary to pass two argument to the constructor. It will automatically consider default argument. If you have specified the default argument and you are passing required parameter to constructor( two value) then it will overwrite that default argument.

Destructor A destructor is used to destroy the objects that have been created by a constructor. Like a constructor the destructor is a member function whose name is same as the class name but is preceded by a tilde sing (~). Eg. ~student(); Destructor never takes any argument nor does it return any value It will be involved implicitly by the compiler when control get exit from the program. It clans up storage that is no longer accessible.