SlideShare a Scribd company logo
Constructor and Destructor in C++
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.
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
}
};
 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.
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!
Types of constructors in C++
 Default Constructor.
 Parameterized Constructor.
 Copy Constructor.
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.
#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;
}
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
#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
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.
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
}
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.
#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;
}
Syntax Of User-defined Copy Constructor:
Class_name(const class_name &ol
d_object);
class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
#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;
}
#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
• 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
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()
{
--------
----------
}
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.
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.
class class_name{
private: // private members
public: // declaring destructor
~class_name()
{
// destructor body
}
};
#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;
}

More Related Content

What's hot

classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Burhan Ahmed
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
tanmaymodi4
 

What's hot (20)

classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Control statements
Control statementsControl statements
Control statements
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Friend functions
Friend functions Friend functions
Friend functions
 
Interface
InterfaceInterface
Interface
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 

Similar to Constructors in C++.pptx

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
रमन सनौरिया
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
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
Lovely Professional University
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
Somnath Kulkarni
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
Akshaya Parida
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
RAJ KUMAR
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
C++
C++C++
C++
C++C++

Similar to Constructors in C++.pptx (20)

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
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
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
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
constructors and destructorsconstructors and destructors
constructors and destructors
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
C++
C++C++
C++
 
C++
C++C++
C++
 

Recently uploaded

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

Constructors in C++.pptx

  • 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; }