SlideShare a Scribd company logo
1 of 16
CONSTRUCTOR
     AND
 DESTRUCTOR
1. Point out the errors, if any, in the following program.
       class date
       {
               private:
                       int day,month,year;
               date()
               {
                       day = 2;
                       month = 10;
                       year = 2008;
               }
       };

       void main()
       {
              date today;
       }
Error :
The default constructor is defined
under private section; so it is not
  accessible in function main( ).
2. Point out the errors, if any, in the following program.
        class value
        {
                private:
                        int i;
                        float f;
                public:
                        value()
                        {
                                 i = 0;
                                 f = 0.0;
                                 return 1;
                        }
        };
        void main()
        {
                value vi;
        }
Error :
The constructor should not return
   any value. Here constructor
   contains return statement.
3. Read the following code segment.
           #include <iostream.h>
           class sample
           {
                      int x, y;
                      public:
                         sample(int a = 10, int b = 20)
                         {
                                 x = a;
                                 y = b;
                        }
                        void show()
                        {
                                 cout << “n x = “ << x << “y = “ << y;
                         }
           };
           void main()
           {
                      sample s1;
                      sample s2(4);
                      sample s3 = sample(6,8);
                      s1.show();
                      s2.show();
                      s3.show();
           }
Explain the working of constructor for the creation of each object.
What will be the output of the above code.
b) x = 10, y=20
   x = 4, y=20
    x = 6, y=8
4. A class named employee is having the
following constructors.
     i) employee();
     ii) employee(int, float);
     iii) employee(employee &f);
A) Name the category to which each
   constructor belongs.
B) Write down the purpose of each.
C) Define the destructor for this class.
1. i) default constructor,
   ii) parameterized constructor,
  iii) copy constructor
2. short note about default constructor,
parameterized constructor and copy
constructor
3. ~employee( ) { }
5. The following code defines a class for finding out the area of a circle.
#include<iostream.h>
class circle
{
          float rad;
          public:
                    circle(float x)
                    {
                              rad = x;
                    }
                    void area()
                    {
                              cout << 3.14 * rad * rad;
                    }
};
void main()
{
          circle(5.5).area();
}
What is the purpose of the statement in the main function?
What is the output?
a)Through this statement, an
anonymous object (temporary instance)
is created and initialized with the value
5.5 and that object calls the member
function area( )
b) 94.985
6. While declaration, initialization is
possible with variables, arrays and
structures. What about of classes?
Justify your answer.


• But an object of a class cannot be initialized
  in similar fashion because the access labels
  restrict the accessibility of the data members.
7. A constructor is usually defined
in public section. Why?


• Generally a constructor is defined under the
  public section of a class so that its objects can
  be created in any function.
8. Is constructor definition
compulsory? Give justification to
support your answer.

• Yes, constructor definition is compulsory. A
  Constructor is defined like other member
  functions of a class. It can be defined either
  inside or outside the class specification.
9. List down the characteristics of
constructors.
• They should be declared in the public section.
• They are invoked automatically when the
  objects are created.
• They do not have return types, not even void
  and therefore, and they cannot return values.
• They cannot be inherited.
• Like other C++ functions, they can have default
  arguments.
• They cannot be virtual.
• We cannot refer to their addresses.
• They cannot be static.
10. List down the characteristics of
destructors.
• They are invoked automatically when the
  objects are destroyed.
• They obey the usual access rules that other
  member functions do.
• They cannot be inherited.
• No argument can be provided to a destructor,
  neither does it return any value.
• They cannot be virtual.
• We cannot refer to their addresses.
• They cannot be static.
• If there is no destructor in a class, a default
  destructor is generated by the compiler.

More Related Content

What's hot

Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsKanhaiya Saxena
 

What's hot (20)

Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objects
 

Similar to Constructor and destructor

More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Abu Saleh
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
Constructors.16
Constructors.16Constructors.16
Constructors.16myrajendra
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Abu Saleh
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4007laksh
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 

Similar to Constructor and destructor (20)

More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
Test Engine
Test EngineTest Engine
Test Engine
 
Test Engine
Test EngineTest Engine
Test Engine
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
E5
E5E5
E5
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Oops recap
Oops recapOops recap
Oops recap
 
class and objects
class and objectsclass and objects
class and objects
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
Constructor
ConstructorConstructor
Constructor
 

More from Selvin Josy Bai Somu (8)

Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Web technology
Web technologyWeb technology
Web technology
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Files in c++
Files in c++Files in c++
Files in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 

Constructor and destructor

  • 1. CONSTRUCTOR AND DESTRUCTOR
  • 2. 1. Point out the errors, if any, in the following program. class date { private: int day,month,year; date() { day = 2; month = 10; year = 2008; } }; void main() { date today; }
  • 3. Error : The default constructor is defined under private section; so it is not accessible in function main( ).
  • 4. 2. Point out the errors, if any, in the following program. class value { private: int i; float f; public: value() { i = 0; f = 0.0; return 1; } }; void main() { value vi; }
  • 5. Error : The constructor should not return any value. Here constructor contains return statement.
  • 6. 3. Read the following code segment. #include <iostream.h> class sample { int x, y; public: sample(int a = 10, int b = 20) { x = a; y = b; } void show() { cout << “n x = “ << x << “y = “ << y; } }; void main() { sample s1; sample s2(4); sample s3 = sample(6,8); s1.show(); s2.show(); s3.show(); } Explain the working of constructor for the creation of each object. What will be the output of the above code.
  • 7. b) x = 10, y=20 x = 4, y=20 x = 6, y=8
  • 8. 4. A class named employee is having the following constructors. i) employee(); ii) employee(int, float); iii) employee(employee &f); A) Name the category to which each constructor belongs. B) Write down the purpose of each. C) Define the destructor for this class.
  • 9. 1. i) default constructor, ii) parameterized constructor, iii) copy constructor 2. short note about default constructor, parameterized constructor and copy constructor 3. ~employee( ) { }
  • 10. 5. The following code defines a class for finding out the area of a circle. #include<iostream.h> class circle { float rad; public: circle(float x) { rad = x; } void area() { cout << 3.14 * rad * rad; } }; void main() { circle(5.5).area(); } What is the purpose of the statement in the main function? What is the output?
  • 11. a)Through this statement, an anonymous object (temporary instance) is created and initialized with the value 5.5 and that object calls the member function area( ) b) 94.985
  • 12. 6. While declaration, initialization is possible with variables, arrays and structures. What about of classes? Justify your answer. • But an object of a class cannot be initialized in similar fashion because the access labels restrict the accessibility of the data members.
  • 13. 7. A constructor is usually defined in public section. Why? • Generally a constructor is defined under the public section of a class so that its objects can be created in any function.
  • 14. 8. Is constructor definition compulsory? Give justification to support your answer. • Yes, constructor definition is compulsory. A Constructor is defined like other member functions of a class. It can be defined either inside or outside the class specification.
  • 15. 9. List down the characteristics of constructors. • They should be declared in the public section. • They are invoked automatically when the objects are created. • They do not have return types, not even void and therefore, and they cannot return values. • They cannot be inherited. • Like other C++ functions, they can have default arguments. • They cannot be virtual. • We cannot refer to their addresses. • They cannot be static.
  • 16. 10. List down the characteristics of destructors. • They are invoked automatically when the objects are destroyed. • They obey the usual access rules that other member functions do. • They cannot be inherited. • No argument can be provided to a destructor, neither does it return any value. • They cannot be virtual. • We cannot refer to their addresses. • They cannot be static. • If there is no destructor in a class, a default destructor is generated by the compiler.