SlideShare a Scribd company logo
Constructor, Destructor
bY
SADi, SHANTu
The Class Constructor:
A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
Unlike normal functions, constructors have specific rules for how they must be named:
1) Constructors should always have the same name as the class (with the same capitalization)
2) Constructors have no return type (not even void)
A constructor that takes no parameters (or has all optional parameters) is called a default constructor.
Here is an example of a class that has a
default constructor:
class A
{
int x;
public:
A(); //Constructor
};
Constructors can be defined either inside the class definition or outside class definition using
class name and scope resolution :: operator.
class A
{
int x;
public:
A(); //Constructor
};
A::A() // Constructor definition
{
i=1;
}
Constructors are of three
types :
1. Default Constructor
2. Parametrized
Constructor
3. Copy COnstructor
Types of Constructors
Default Constructor
Default constructor is the
constructor which doesn't
take any argument. It has
no parameter
Syntax :
class_name ()
{ Constructor Definition }
Example :
class Cube
{
int side;
public:
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
}
Output : 10
A default constructor is so important for initialization of
object members, that even if we do not define a constructor
explicitly, the compiler will provide a default constructor
implicitly.
Parameterized
Constructor
These are the
constructors with
parameter. Using this
Constructor you can
provide different values
to data members of
different objects, by
passing the appropriate
values as argument
Example :
class Cube
{
int side;
public:
Cube(int x)
{
side=x;
}
};
int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
cout << c1.side;
cout << c2.side;
cout << c3.side;
}
OUTPUT : 10 20 30
Copy Constructor
These are special type of Constructors which takes an object
as argument, and is used to copy values of data members of
one object into other object.

More Related Content

PPT
Constructor & Destructor
PPTX
Constructor and Types of Constructors
PPT
Constructor and Destructor PPT
PDF
Constructor and Destructor
PPSX
Constructor and destructor
PPTX
Constructor and destructor
PPTX
constructor & destructor in cpp
PDF
Constructors and Destructors
Constructor & Destructor
Constructor and Types of Constructors
Constructor and Destructor PPT
Constructor and Destructor
Constructor and destructor
Constructor and destructor
constructor & destructor in cpp
Constructors and Destructors

What's hot (20)

PPTX
Constructor and Destructor in c++
PPTX
Constructor ppt
PPTX
Constructors and destructors
PPT
Oop lec 5-(class objects, constructor & destructor)
PPTX
Constructor and desturctor
PDF
04. constructor & destructor
PPT
Constructor
PPTX
Types of Constructor in C++
PPTX
constructors in java ppt
PPT
vb.net Constructor and destructor
PPT
Tutconstructordes
PPT
Constructor
PPTX
Constructors & destructors
PPTX
Constructor & destructor
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPTX
constructor and destructor
PPT
C++: Constructor, Copy Constructor and Assignment operator
PDF
Constructor & destructor
PDF
Constructors destructors
Constructor and Destructor in c++
Constructor ppt
Constructors and destructors
Oop lec 5-(class objects, constructor & destructor)
Constructor and desturctor
04. constructor & destructor
Constructor
Types of Constructor in C++
constructors in java ppt
vb.net Constructor and destructor
Tutconstructordes
Constructor
Constructors & destructors
Constructor & destructor
Oop Constructor Destructors Constructor Overloading lecture 2
constructor and destructor
C++: Constructor, Copy Constructor and Assignment operator
Constructor & destructor
Constructors destructors
Ad

Viewers also liked (15)

PDF
The State of Twitter: STL 2011
PDF
STL Algorithms In Action
PPT
Set Theory In C++
PPTX
Friend function & friend class
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PPT
standard template library(STL) in C++
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
PDF
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPT
Lecture5
The State of Twitter: STL 2011
STL Algorithms In Action
Set Theory In C++
Friend function & friend class
4. Recursion - Data Structures using C++ by Varsha Patil
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
8. Graph - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
standard template library(STL) in C++
5. Queue - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
Lecture5
Ad

Similar to C++ Constructor destructor (20)

DOCX
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
PPTX
Constructor & Destructor/sanjeet-1308143
PPTX
Constructors in C++.pptx
PPTX
DHARANI.pptx
PPTX
Constructors and Destructors
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
C++ Unit-III Lecture-3a-C++ Programming Concepts
PPTX
PPTX
C++Constructors
PPTX
object oriented programming with C++ Constructors_Destructors.pptx
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PDF
Constructor and Destructor.pdf
PPTX
An introduction to constructor
PPTX
Constructor and Destructor
PDF
Constructors and destructors
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPT
ConsTRUCTION AND DESTRUCTION
PDF
1.11 Constructors and Destructors....pdf
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
Constructor & Destructor/sanjeet-1308143
Constructors in C++.pptx
DHARANI.pptx
Constructors and Destructors
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++Constructors
object oriented programming with C++ Constructors_Destructors.pptx
constructocvbcvbcvbcvbr-Destructor (1).pptx
Constructor and Destructor.pdf
An introduction to constructor
Constructor and Destructor
Constructors and destructors
An introduction to Constructors and destructors in c++ .pptx
ConsTRUCTION AND DESTRUCTION
1.11 Constructors and Destructors....pdf

Recently uploaded (20)

PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
additive manufacturing of ss316l using mig welding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
composite construction of structures.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
R24 SURVEYING LAB MANUAL for civil enggi
additive manufacturing of ss316l using mig welding
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
Project quality management in manufacturing
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
composite construction of structures.pdf
UNIT 4 Total Quality Management .pptx
Digital Logic Computer Design lecture notes
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

C++ Constructor destructor

  • 2. The Class Constructor: A class constructor is a special member function of a class that is executed whenever we create new objects of that class. Unlike normal functions, constructors have specific rules for how they must be named: 1) Constructors should always have the same name as the class (with the same capitalization) 2) Constructors have no return type (not even void) A constructor that takes no parameters (or has all optional parameters) is called a default constructor.
  • 3. Here is an example of a class that has a default constructor: class A { int x; public: A(); //Constructor };
  • 4. Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator. class A { int x; public: A(); //Constructor }; A::A() // Constructor definition { i=1; }
  • 5. Constructors are of three types : 1. Default Constructor 2. Parametrized Constructor 3. Copy COnstructor Types of Constructors
  • 6. Default Constructor Default constructor is the constructor which doesn't take any argument. It has no parameter Syntax : class_name () { Constructor Definition } Example : class Cube { int side; public: Cube() { side=10; } }; int main() { Cube c; cout << c.side; } Output : 10
  • 7. A default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.
  • 8. Parameterized Constructor These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument Example : class Cube { int side; public: Cube(int x) { side=x; } }; int main() { Cube c1(10); Cube c2(20); Cube c3(30); cout << c1.side; cout << c2.side; cout << c3.side; } OUTPUT : 10 20 30
  • 9. Copy Constructor These are special type of Constructors which takes an object as argument, and is used to copy values of data members of one object into other object.