Constructor and Destructor in C++ are special member functions that are automatically called by the compiler.
Constructors initialize a newly created object and are called when the object is created. Destructors destroy objects and release memory and are called when the object goes out of scope. There are different types of constructors like default, parameterized, and copy constructors that allow initializing objects in different ways. Destructors do not have arguments or return values and are declared with a tilde symbol preceding the class name.
2. Constructor and Destructor
Constructor and Destructor in C++ Constructor and
Destructor are the special member functions of the
class which are created by the C++ compiler or can be
defined by the user. Constructor is used to initialize
the object of the class while destructor is called by
the compiler when the object is destroyed.
3. Constructor
A constructor is a special type of member function that is called automatically
when an object is created.
It is used to initialize the data members of new object generally.
The return type of the constructor is the class types.
For example,
class LNCT
{
public:
// create a constructor
LNCT()
{
// code
}
};
4. Here, the function LNCT() is a constructor of the
class LNCT. Notice that the constructor
has the same name as the class,
does not have a return type, and
is public
To create a constructor, use the same name as
the class, followed by parentheses ():
In class-based object-oriented programming, The
constructor is a special method that is used to
initialize a newly created object and is called just
after the memory is allocated for the object.
5.
6.
7.
8.
9.
10. class MyClass // The class
{
public: // Access specifier
MyClass() // Constructor
{
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Note: The constructor has the same name as the class, it is always public, and it does not have
any return value.
OUTPUT :
Hello World!
11. Types of constructors in C++
Default Constructor.
Parameterized Constructor.
Copy Constructor.
12.
13. Default constructor:
A constructor without any arguments or with the
default value for every argument is said to be the
default constructor. They are used to create
objects, which do not have any specific initial
value. If no constructors are explicitly declared in
the class, a default constructor is provided
automatically.
14. #include <bits/stdc++.h>
using namespace std;
class construct
{
public:
int x, y;
construct() // Default Constructor with default values
{
x = 5; y = 10;
}
};
int main()
{
// Default constructor called automatically
// whenever the object is created
construct z;
cout << "x is: " << z.x << "n" << "y is: " << z.y;
return 0;
}
15.
16. Parameterized constructor:
A constructor which takes arguments as input is
called a parameterized constructor. It is used to
provide different values to distinct objects. When
we call the constructor (by creating an object of
the class), we pass parameters to the constructor,
which will set the value of the corresponding
attributes to the same
17. #include <bits/stdc++.h>
using namespace std;
class values
{
private: int a, b;
public: // Parameterized Constructor
values(int x, int y)
{
a = x; b = y;
}
int getA()
{
return a; }
int getB()
{
return b;
} };
int main()
{
// Constructor called
values v(5, 10); // values assigned by constructor
cout << "v1.x = " << v.getA() << "n" << "v2.y = " << v.getB();
return 0; }
v1.x = 5
v2.y = 10
18. Copy constructor
: A copy constructor is a member function that
initializes an object using another object of the
same class. A copy constructor initializes an
object by copying the member values from an
object of the same type. If your class members
are all simple types such as normal values, the
compiler-generated copy constructor is sufficient
and you don’t need to define your own.
A Copy constructor is an overloaded constructor
used to declare and initialize an object from
another object.
19. copy constructor
A constructor that is used to copy or initialize the
value of one object into another object is called
copy constructor .
SYNTAX:- class_name (class_name &ref)
{
// code to executed
}
20. Copy Constructor is of two types:
Default Copy constructor: The compiler defines
the default copy constructor. If the user defines
no copy constructor, compiler supplies its
constructor.
User Defined constructor: The programmer
defines the user-defined constructor.
21. #include <iostream>
using namespace std;
class A
{
int a,b;
public:
A(int x, int y)
{
a=x; b=y;
}
A(A &m)
{
a=m.a;
b=m.b;
}
void show ()
{
cout<< a<<b;
}};
int main()
{
A ob(10,20);
A ob1=ob;
ob.show();
ob1.show();
return 0;
}
22. Syntax Of User-defined Copy Constructor:
Class_name(const class_name &ol
d_object);
class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
23. #include <iostream>
using namespace std;
class A
{
public:
int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
}
};
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1); // Calling the copy constructor.
cout<<a2.x;
return 0;
}
24. #include<iostream>
using namespace std;
class Point {
private: int x, y;
public:
Point(int x1, int y1)
{
x = x1; y = y1;
} // Copy constructor
Point(const Point & p1)
{
x = p1.x; y = p1.y;
}
int getX()
{
return x;
}
int getY()
{
return y; } };
int main()
{
Point p1(10, 15);
// Normal constructor is called here Point
p2 = p1; // Copy constructor is called here //
Let us access values assigned by
constructors
cout << "p1.x = " << p1.getX() << ", p1.y =
" << p1.getY();
cout << "np2.x = " << p2.getX() << ", p2.y
= " << p2.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
25. • Write the program to calculate the area of circle
using Default Constructor?
•Write a program to exchange of variables values
using parametric constructor?
• Write a program to calculate the factorial a
giving number using default and parametric
constructor?
•Write a program to calculate the biggest value in
two non using copy and parametric ?
Exercise
26. What are destructors in C++?
A destructor is a member function that is invoked automatically when the
object goes out of scope or is explicitly destroyed by a call to delete .
A destructor has the same name as the class,
preceded by a tilde ( ~ ).
For example,
the destructor for class String is declared: ~String() .
If you do not define a destructor, the compiler will provide a default one;
for many classes this is sufficient. You only need to define a custom
destructor when the class stores handles to system resources that need
to be released, or pointers that own the memory they point to.
~ExampleClass()
{
--------
----------
}
27. Declaring destructors
Destructors are functions with the same name as the class
but preceded by a tilde (~)
Several rules govern the declaration of destructors.
Destructors:
Do not accept arguments.
Do not return a value (or void).
Cannot be declared as const, volatile, or static. However,
they can be invoked for the destruction of objects declared
as const, volatile, or static.
Can be declared as virtual. Using virtual destructors, you
can destroy objects without knowing their type — the
correct destructor for the object is invoked using the virtual
function mechanism. Note that destructors can also be
declared as pure virtual functions for abstract classes.
28. Using destructors
Destructors are called when one of the following events occurs:
A local (automatic) object with block scope goes out of scope.
An object allocated using the new operator is explicitly
deallocated using delete.
The lifetime of a temporary object ends.
A program ends and global or static objects exist.
The destructor is explicitly called using the destructor function's
fully qualified name.
Destructors can freely call class member functions and access
class member data.
There are two restrictions on the use of destructors:
You cannot take its address.
Derived classes do not inherit the destructor of their base class.
29. class class_name{
private: // private members
public: // declaring destructor
~class_name()
{
// destructor body
}
};
30. #include <iostream>
using namespace std;
class test
{
public:
test()
{
int n=10;
cout<<n;
}
~test()
{
cout<<"Destructor";
}
};
int main()
{
test t,t1,t2;
return 0;
}