Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Selamat Idul Fitri 1430 H Taqabalallahu minna waminkum Taqabbal ya karim… Mohon maaf lahir dan batin.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
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.
C++ Classes & Data Abstraction
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
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.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
 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)
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
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.
 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.
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 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
1.Which of the following statements is correct?
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.
Chapter -6 Polymorphism
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
93 4/11/98 CSE 143 Class Constructors [Sections ]
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
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.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Constructors and Destructors
Static data members Constructors and Destructors
Programming with ANSI C ++
Class A { public : Int x; A()
Constructors & Destructors.
Constructor & Destructor
Constructors & Destructors
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Lecture Constructors and Destructors
Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27.
Object Oriented Analysis and Design
Constructor & Destructor
CONSTRUCTOR A member function with the same name as its class is called Constructor and it is used to initialize the objects of that class type with.
Contents Introduction to Constructor Characteristics of Constructor
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
Introduction of Programming
CIS 199 Final Review.
Constructors and destructors
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
C++ Polymorphism Reference and pointer implicit type casting
Constructors & Destructors
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor o Overloaded Constructor -Multiple Constructor in a class -Constructor with default argument o Destructor o Difference between constructor & destructor. o Conclusion

Concept of constructor  In C++, a constructor is a ‘special’ member function whose task is to initialize the objects of it’s class when it is created.  It is special because it’s name is the same as the class name.  It is called constructor because it constructs the value of data members of the class.  The constructor is invoked whenever an object of it’s associated class is created.

 They should be declared in the public section.  They are invoked automatically when the objects are created.  Do not have return types.  Cannot be virtual.  Cannot be inherited through a derived class can call the base class constructor. Characteristics of Constructor

#include class stud { int roll_no; char name[20]; public: stud(); void show() { cout<<“roll_no”<<roll_no<<endl; cout<<“name”<<name<<endl; } }; stude::stud() { cout<<“enter roll_no”<<endl; cin>>roll_no; cout<<“enter name”<<endl; cin>>name; } void main() { stud s; clrscr(); s.show(); getch(); } Program to explain concept of constructor

Types of constructor Constructor1.Default2.Parameterized3.Copy

1.Default Constructor  A constructor that accepts no parameters is called the default constructor.  If no such constructor is defined, then the compiler supplies a default constructor.  E.g. #include class integer { int x; public: integer(); }; integer::integer() { x= 10; } void main() { integer int 1; getch(); }

 The constructors that can take arguments are as shown bellow: class integer { int m,n; public: integer(intx,inty);//parameterized constructor }; integer::integer(int x,int y) { m=x; n=y; } Parameterized constructor

 By calling the constructor explicitly integer int1=integer(0,100);//explicit call This statement creates an integer object int1 and passes the values 0 and 100 to it.  By calling the constructor implicitly integer int1(0,100);//implicit call. Calling to Constructor

The constructor functions can also be defined as inline functions, e.g. class integer { int m,n; public: integer(int x,int y) { m=x; n=y; } }; e.g. class a { public: a(a); }; it is illegal class a { public: a(a&); }; is valid

- However a constructor can accept a reference to it’s own class as a parameter, in such cases, the constructor is called the copy constructor. Syntax: class integer { int x; public: integer(integer &i) { x=i.x; } }; Copy Constructor

# include class integer { int x; public: integer(int a) { x=a; } integer(integer &i) { x=i.x; } void show() { cout<<“x=”<<x<<endl; } }; void main() { integer i1(100); integer i2(i1); integer i3=i1 integer i4; i4=i1; // it is not copy constr cout<<“i1”; i1.show(); cout<<“i2”; i2.show(); cout<<“i3”; i3.show(); getch(); } Program to explain copy constructor

-We are used two kinds of constructors, they are: 1.integer(); //no arguments 2.Integer(int,int); //two arguments -e.g class integer { int m,n; public: integer() // constructor 1 { m=0; n=0; } integer(int a,int b) //constructor 2 { m=a; n=b; } integer(integer &i) //constructor3 { m=i.m; n=i.n; } }; Overloaded Constructors: Multiple Constructors in a class

Program shows the use of overloaded constructors #include class integer { int m,n; public: integer() { m=0; n=0; } integer(int a,int b) { m=a; n=b; } integer(integer &x) { m=x.m; n=x.n; } }; void main() { integer i1; integer i2(30,60); integer i3(i2); getch(); }

Constructors with default arguments -It is possible to define constructors with default arguments. -For example, the constructor complex() can be declared as follows: complex(float teal, float imag=0); -e.g. program #include class student { int roll_no; float marks; public: student(int x=10;float y=40) { roll_no=x; marks=y; } void display() { cout<<“roll no”<<roll_no<<endl; cout<<“marks”<<marks<<endl; } }; void main() { student s1; student s2(1,60); clrscr(); cout<<“output using default constructor argumets:\n”; s1.display(); cout<<“output without default constructor arguments:\n”; s2.display(); getch(); }

1.Constructor is invoked automatically as soon as the object of that class is created this concept is also known as automatic initialization of objects. 2.A key benefit of using a constructor is that it guarantees that the object will go through proper initialization before being used. 3.When a user instantiates an object, that object’s constructor is called and must return before the user can perform any other work with that object. 4.The main use and advantage of constructors is to initialize objects. Advantages of constructor

-A destructor, as the name implies, 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 the same as the class name but is precede by a tilde ( ~ ). -E.g. ~integer() { } -Destructor never take any arguments. -The object is destroyed when end of scope of program. Destructors

ConstructorDestructor 1.Constructor is a special member function whose task is to initialize the object’s of it’s class. 1.Destructor is used to destroy objects that have been created by a constructor. 2. It’s name is same as class name. 2. it’s name is same as class name but is preceded by tilde. 3.Constructor is invoked when the object of it’s associated class is created. 3.It is invoked implicitly by the compiler upon exit of the program to clean up the storage that is no longer accessible. 4.Constructor can take arguments. 4. Destructor does not take any arguments. 5.e.g. student(int a,int b)5. e.g. ~student() Difference Between Constructor & Destructor