Constructors and destructors

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin.
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.
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.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
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];
1 Lab Session-5 CSIT221 Spring 2003 Default and Parameterized Constructors Destructors Programming Exercise for building a template based class (demo required)
Classes Separating interface from implementation
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
 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.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
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.
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes - Intermediate
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
Learners Support Publications Constructors and Destructors.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
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.
Programming with ANSI C ++
Command line arguments
Constructors & Destructors.
Constructor & Destructor
Class Operations Pointer and References with class types
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture Constructors and Destructors
Constructor & Destructor
Basic C++ What’s a declaration? What’s a definition?
Contents Introduction to Constructor Characteristics of Constructor
Foundational Data Structures
Chapter 4 (part 2).
Constructors and destructors
Constructors and Destructors
Essential Class Operations
CS148 Introduction to Programming II
COP 3330 Object-oriented Programming in C++
NAME 436.
Chapter 6 Polymorphism.
Essential Class Operations
CS148 Introduction to Programming II
Objects as Function Arguments
Constructors & Destructors
Classes Class Data Members & Initializers
SPL – PS3 C++ Classes.
Presentation transcript:

Constructors and destructors

constructors A constructor is a special member function whose task is to initialize the objects of its class. Class integer { Int m,n; Public: Integer(void); ………… ……….. }; Integer::integer(void) m=0;n=0; }

Parameterized constructors The constructors that can take arguments are called parameterized constructors. Class integer { int m,n; public: Integer(int x,int y); …… }; Integer::integer(int x, int y) m=x;n=y; }

Multiple constructors in a class In the first case,the constrector it self supplies the date values and no values are passed by the calling program. In the second case,the function call passes the appropriate values from main(). Class int { int m,n; public: Int() {m=0; n=0;} int(int a, int b) {m=a;n=b;} Int(int & i) {m=I; n=I,n;} };

Constructors with default arguments The actual parameter,when specified,overrides the default value

Dynamic initialization of objects This provieds the flexibility of using different format of date at run time depending upon the situasion

Copy constructor A copy constructor is used to declare and initialize an object from another object Integer (integer &i);