SlideShare a Scribd company logo
1 of 24
Download to read offline
Constructors & Destructors
Pranali Chaudhari
Contents
 Constructor
 Types of Constructor
 Memory Allocation
 Destructor
Constructor
 A constructor is a special member function
whose task is to initialize the objects of its class.
 Constructor has the same name as that of
class.
 The constructor is invoked when the object of its
associated class is created.
 Constructor constructs the values of data
members of the class.
Constructor
 A constructor is declared and defined as follows:
Class integer
{
int m, n;
public :
integer(void); // constructor declared
…..
…..
};
integer :: integer(void) // constructor defined
{
m = 0;
n = 0;
}
Characteristics of Constructor
 The constructor have some special characteristics:
 They should be declared in the public section.
 They are invoked automatically when the objects are
created.
 They do not have return type.
 They cannot be inherited.
 Like other C++ functions, they can have default
arguments.
 Constructors cannot be virtual.
 They make implicit calls to the operators new and delete
when memory allocation is required.
Types of Constructors
 Default constructor.
 Parameterized constructor.
 Copy constructor.
 Dynamic constructor.
Default Constructor
 A constructor that accepts no parameters is
called the default constructor.
 The default constructor for class A is A::A().
 If no such constructor is defined, then the
compiler supplies a default constructor.
Parameterized Constructor
 The constructor that can take arguments are called
parameterized constructors.
class integer
{
int m, n;
public:
integer(int x, int y); // parameterized constructor
…..
…..
};
integer :: integer(int x, int y)
{
m = x;
n = y;
}
Copy Constructor
 A copy constructor can take as argument an object of
the same class to which it belongs.
 A copy constructor is used to declare and initialize an
object from another object.
integer I2(I1);
 Define the object I2 and at the same time initialize it
to the values of I1.
integer I2 = I1;
Multiple constructor in a class
class integer
{
int m, n;
public:
integer(){ m = 0; n = 0; } // constructor 1
integer(int a, int b) // constructor 2
{ m = a; n = b; }
integer( integer & i ) // constructor 3
{ m = i.m; n = i.n; }
};
Dynamic Constructor
 The constructors can also be used to allocate
memory while creating objects.
 Allocation of memory to objects at the time of their
construction is known as dynamic construction of
objects.
 The memory is allocated with the help of the new
operator.
Dynamic Constructor
class string
{
char *name;
int length;
public:
string()
{
length = 0;
name = new char [ length +1];
}
string (char *s)
{
length = strlen(s);
name = new char [ length + 1];
strcpy(name, s);
}
void display(void)
{ cout << name << “n” ; }
void join(string &a, string &b);
};
Dynamic Constructor
void string :: join(string &a, string &b)
{
length = a.length + b.length;
delete name;
name = new char [ length + 1];
strcpy(name, a.name);
strcat(name, b.name);
}
int main()
{
char *first = “Mathew”;
string name1(first), name2(“Joseph”), s1;
s1.join(name1, name2);
s1.display();
return 0;
}
Constructor with Default
Arguments
 Constructors can be defined with default
arguments.
 Eg: complex(float real, float imag=0);
 complex c (5.0);
 complex c(2.0, 3.0);
Dynamic Initialization of Objects
 Class objects can be initialized dynamically.
 The initial value of object is provided during
runtime.
 Dynamic initialization can be provided using
overloaded constructors.
 Example.
Destructor
 A destructor is used to destroy the objects
that have been created by a constructor.
 It is defined as:
~ integer() { }
 A destructor never takes any arguments nor
does it return any value.
Practice Example
 Create a class student with the data members
roll no, student name and percentage marks.
Create a default constructor to initialize each
member as zero. Demonstrate all the types of
constructors to initialized the objects. Display
all the objects.
Summary
 ______ are used to initialize variable and to
allocate memory.
 Constructor has the same name as that of
class. ( True / False )
 A constructor that takes arguments is known
as __________ .
 A _________ is used to declare and initialize
an object from another object.
 The memory is allocated with the help of the
_____ operator.
 Objects are destroyed by using ________.
Short Answer Questions
 How do we invoke a constructor function?
 The constructor is invoked whenever an object
of its associated class is created.
 List some of the special properties of the
constructor.
 Properties of constructors
Short Answer Questions
 What do you mean by dynamic initialization of
objects? Why do we need this?
 Dynamic initialization of objects means
providing the values to an object at runtime.
This provides the flexibility of using different
format of data at run time depending upon the
situation.
Short Answer Questions
 Explain the following statements:
time T2 (T1);
time T2 = T1;
T1 and T2 are objects of time class.
 Both the statements are demonstrating the
copy constructor. Here the object T2 of class
time is created and at the same time it is
initialized with the values of another object T1
of class time.
Short Answer Questions
 Explain the term constructor overloading.
 Constructor overloading means when more than
one constructor is defined in a class.
 What is default constructor? What happens if no
such constructor is defined?
 A constructor that accepts no parameters is called
the default constructor. If no such constructor is
defined, then the compiler supplies a default
constructor.
References
 Object Oriented Programming with C++ by E.
Balagurusamy.
END OF UNIT

More Related Content

What's hot

Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 

What's hot (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Access specifier
Access specifierAccess specifier
Access specifier
 
inheritance
inheritanceinheritance
inheritance
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Java string handling
Java string handlingJava string handling
Java string handling
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Java package
Java packageJava package
Java package
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 

Similar to Constructors destructors

Similar to Constructors destructors (20)

Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
7494609
74946097494609
7494609
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
C questions
C questionsC questions
C questions
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
C++
C++C++
C++
 

More from Pranali Chaudhari (9)

Exception handling
Exception handlingException handling
Exception handling
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Templates
TemplatesTemplates
Templates
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 

Recently uploaded

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

Constructors destructors

  • 2. Contents  Constructor  Types of Constructor  Memory Allocation  Destructor
  • 3. Constructor  A constructor is a special member function whose task is to initialize the objects of its class.  Constructor has the same name as that of class.  The constructor is invoked when the object of its associated class is created.  Constructor constructs the values of data members of the class.
  • 4. Constructor  A constructor is declared and defined as follows: Class integer { int m, n; public : integer(void); // constructor declared ….. ….. }; integer :: integer(void) // constructor defined { m = 0; n = 0; }
  • 5. Characteristics of Constructor  The constructor have some special characteristics:  They should be declared in the public section.  They are invoked automatically when the objects are created.  They do not have return type.  They cannot be inherited.  Like other C++ functions, they can have default arguments.  Constructors cannot be virtual.  They make implicit calls to the operators new and delete when memory allocation is required.
  • 6. Types of Constructors  Default constructor.  Parameterized constructor.  Copy constructor.  Dynamic constructor.
  • 7. Default Constructor  A constructor that accepts no parameters is called the default constructor.  The default constructor for class A is A::A().  If no such constructor is defined, then the compiler supplies a default constructor.
  • 8. Parameterized Constructor  The constructor that can take arguments are called parameterized constructors. class integer { int m, n; public: integer(int x, int y); // parameterized constructor ….. ….. }; integer :: integer(int x, int y) { m = x; n = y; }
  • 9. Copy Constructor  A copy constructor can take as argument an object of the same class to which it belongs.  A copy constructor is used to declare and initialize an object from another object. integer I2(I1);  Define the object I2 and at the same time initialize it to the values of I1. integer I2 = I1;
  • 10. Multiple constructor in a class class integer { int m, n; public: integer(){ m = 0; n = 0; } // constructor 1 integer(int a, int b) // constructor 2 { m = a; n = b; } integer( integer & i ) // constructor 3 { m = i.m; n = i.n; } };
  • 11. Dynamic Constructor  The constructors can also be used to allocate memory while creating objects.  Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.  The memory is allocated with the help of the new operator.
  • 12. Dynamic Constructor class string { char *name; int length; public: string() { length = 0; name = new char [ length +1]; } string (char *s) { length = strlen(s); name = new char [ length + 1]; strcpy(name, s); } void display(void) { cout << name << “n” ; } void join(string &a, string &b); };
  • 13. Dynamic Constructor void string :: join(string &a, string &b) { length = a.length + b.length; delete name; name = new char [ length + 1]; strcpy(name, a.name); strcat(name, b.name); } int main() { char *first = “Mathew”; string name1(first), name2(“Joseph”), s1; s1.join(name1, name2); s1.display(); return 0; }
  • 14. Constructor with Default Arguments  Constructors can be defined with default arguments.  Eg: complex(float real, float imag=0);  complex c (5.0);  complex c(2.0, 3.0);
  • 15. Dynamic Initialization of Objects  Class objects can be initialized dynamically.  The initial value of object is provided during runtime.  Dynamic initialization can be provided using overloaded constructors.  Example.
  • 16. Destructor  A destructor is used to destroy the objects that have been created by a constructor.  It is defined as: ~ integer() { }  A destructor never takes any arguments nor does it return any value.
  • 17. Practice Example  Create a class student with the data members roll no, student name and percentage marks. Create a default constructor to initialize each member as zero. Demonstrate all the types of constructors to initialized the objects. Display all the objects.
  • 18. Summary  ______ are used to initialize variable and to allocate memory.  Constructor has the same name as that of class. ( True / False )  A constructor that takes arguments is known as __________ .  A _________ is used to declare and initialize an object from another object.  The memory is allocated with the help of the _____ operator.  Objects are destroyed by using ________.
  • 19. Short Answer Questions  How do we invoke a constructor function?  The constructor is invoked whenever an object of its associated class is created.  List some of the special properties of the constructor.  Properties of constructors
  • 20. Short Answer Questions  What do you mean by dynamic initialization of objects? Why do we need this?  Dynamic initialization of objects means providing the values to an object at runtime. This provides the flexibility of using different format of data at run time depending upon the situation.
  • 21. Short Answer Questions  Explain the following statements: time T2 (T1); time T2 = T1; T1 and T2 are objects of time class.  Both the statements are demonstrating the copy constructor. Here the object T2 of class time is created and at the same time it is initialized with the values of another object T1 of class time.
  • 22. Short Answer Questions  Explain the term constructor overloading.  Constructor overloading means when more than one constructor is defined in a class.  What is default constructor? What happens if no such constructor is defined?  A constructor that accepts no parameters is called the default constructor. If no such constructor is defined, then the compiler supplies a default constructor.
  • 23. References  Object Oriented Programming with C++ by E. Balagurusamy.