 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.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
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];
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Classes Separating interface from implementation
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Inheritance: Extending classes 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.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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)
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
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.
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.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
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.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
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
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
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.
CONSTRUCTORS & DESTRUCTORS
Constructor & Destructor
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Oriented Analysis and Design
Constructor & Destructor
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Multiple Base Classes Inheritance
Destructor CSCE 121 J. Michael Moore.
Inheritance:Concept of Re-usability
Destructor CSCE 121.
Constructors/Destructors Functions Revisited
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
Constructors & Destructors
Presentation transcript:

 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 name.  The constructor is invoked whenever an object of its associated class is created.  It is called constructor because it constructs the values of data members of the class.

class add { int m, n ; public : add (void) ; }; add :: add (void) { m = 0; n = 0; }  When a class contains a constructor, it is guaranteed that an object created by the class will be initialized automatically.  add a ;  Not only creates the object a of type add but also initializes its data members m and n to zero.

 A destructor is used to destroy the objects that have been created by a constructor.  Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde. eg: ~ integer ( ) { }

 It is a good practice to declare destructors in a program since it releases memory space for further use.  Whenever new is used to allocate memory in the constructor, we should use delete to free that memory.

 The C++ classes can be reused in many ways.  Once a class has been tested, it can be adapted by other programs to their requirements.  The Mechanism of deriving a new class from an old one is called INHERITANCE.  The old class is referred as BASE class.  The new class is referred as DERIVED class.

 There are 5 Forms of INHERITANCE:  Single Inheritance  Multiple Inheritance  Hierarchical Inheritance  Multilevel Inheritance  Hybrid Inheritance

Multiple Inheritance Hybrid Inheritance Multi Level Inheritance Hierarchical InheritanceSingle Inheritance

Class derived: public base { Public: derived() Cout<<“Constructing derived”; } ~derived() { Cout<<“Destructing derived”; } };

Void main() { derived D; Return(0); }

#include //header file #include class base//declaraction of class { protected: int i;//members of class public: base(int X)//parametrised conctructor

{ i=X; Cout<<“\n\nConstructing base”; } ~base()//destructor { Cout<<“\n\n Destructing base”; } };//termination of class class derived: public base//derived from base class {

int j; public: //passed along to base //passing parameters to base class derived(int X, int Y):base(Y) { j=X; Cout<<“Constructing derived\n\n”; } ~derived() { Cout<<“\n\n Destructing derived”; }

void show() { cout<<“i= \n “<<i<<“j= \n “<<j<<endl; } };//termination of class void main() { derived ob(3,4);//object is created clrscr(); ob.show();//function call getch(); }