SlideShare a Scribd company logo
MSc, Department of Bioinformatics, KSAWU, Vijayapura 2
Karnataka State Women's University
VIJAYAPUR
INTRODUCTION
Constructor is the special type of member function in
C++ class, which are automatically invoked when an
object is being created.
It is special because its name is same as the class name.
Class A
{
public:
int x;
public:
A(); //Constructor
};
Constructer can be defined either inside the class
definition or outside class definition using class name
and the scope of resolution :: operator
Class A
{
int number;
public:
A(); // Constructor declared
};
A::A() // Constructor definition
{
number=1;
}
1. Default Constructors or Zero argument constructor
2. Parameterized Constructors
3. Copy Constructor
“Default constructor is the constructor which doesn’t take
any argument.”
Or
“A constructer that accepts no parameter is known as default
constructer”
Syntax
Student
{
Student()
{
}
};
Example :
Class Cube
{
public :
int side ;
public :
Cube ()
{
side 10;
}
};
Int main ()
{
Cube c;
cout <<“nValue is : “ << c. side;
Output :
Value is : 10
…………………………………………...
Process excited after o.o148 seconds with
return value 0
Press any key to continue . . .
 “A constructer that receives arguments / parameters , is called
parameterized constructer “
 Construct with parameter is called parameterized constructer
 All the constructor executes at the time of object creation
 When we will create parameterized constructor then we have to pass
argument value at the time of object creation of that class
Syntax
Class Student
{
Student (int a , int b ) // parameterized constructor
{
// Statements
}
};
EXAMPLE
Class student
{
public :
int Roll ;
String Name;
float Marks;
puclic :
Student (int r, string nm,float m)
{
Roll = r;
Name = nm;
Marks = m;
}
Parameterized
Constructor
with three
parameters
Void Display ()
{
cout<<“n Roll is : “<<Roll;
cout<<“n Name is : “<<Name;
cout<<“n Marks is : “:Marks;
}
}; // end of class
Int main()
{
Student S( 2, “Justin”, 90);
S.Display(); //Displaying Student
Details
return 0;
}
OUTPUT
Roll is : 2
Name is : Justin
Marks is : 90
……………………………………………………….
Process excited after o.o1708 seconds with return value 0
Press any key to continue . . .
 These are special type of constructors which takes an object as
argument and is used to copy values of data member of one object
into other object
 Initialization of an object through another object is called copy
constructor .
 In other words, copying the value of one object into another object is
called copy constructor.
Syntax
Class Class_Name {
Constructor_name(class_name & obj_name)
{
}
};
Class Student
{
Int Roll;
string Name;
float Marks;
Public:
Student(int r, string nm, float m)
{
Roll = r;
Name = nm;
Marks = m;
}
Student (student & S)
{
Roll = S.Roll;
Name = S. Name;
Marks = S. Marks;
}
constructor 1 :
Parameterize
Constructor
Constructor 2:
Copy
Constructor
EXAMPLE
Void Display ()
{
cout << “n Roll:” <<Roll;
cout << “n Name:” << Name;
cout << “n Marks:”<<Marks;
}
};
Int main ()
{
Student S1(2, “Justin”,90);
Student S2 (S1); // Statement 1
cout <<“n Values in objects S1”;
S1. Display();
cout <<“n.......................”;
cout<<“n Values in object S2”;
S2.Display();
OUTPUT
Value in object S1
Roll : 2
Name : Justin
Marks : 90
……………………………………………….
Value in object S2
Roll : 2
Name : Justin
Marks : 90
……………………………………………….
Process excited after o.o4381 seconds with return value 0
Press any key to continue . . .
To initialize data member of class:
In the constructor member function
(which will be declared by the programmer) we can initialize the
default vales to the data members and they can be used further for
processing.
To allocate memory for data member:
Constructor can also be used to declare
run time memory (dynamic memory for the data members).
Constructor is used for:
@geeks for geeks
Object-Oriented Programming with ANSI and
Turbo C++ by Ashok Kamthane
Publisher: Pearson India Release Date: July
2006 ISBN: 9788131703830
REFERENCES :
C++Constructors

More Related Content

What's hot

Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
aleenaguen
 
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
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Tut Constructor
Tut ConstructorTut Constructor
Constructors
ConstructorsConstructors
Constructors
Mirza Hussain
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
ShaishavShah8
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
suraj pandey
 
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
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
Jani Harsh
 

What's hot (20)

Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor 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
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructors
ConstructorsConstructors
Constructors
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor
ConstructorConstructor
Constructor
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
 
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
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 

Similar to C++Constructors

Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
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
 
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
Akshaya Parida
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 
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
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructorDeepak Singh
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
Praveen M Jigajinni
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 

Similar to C++Constructors (20)

Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
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
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
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
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 

More from Nusrat Gulbarga

NMR .pdf
NMR .pdfNMR .pdf
NMR .pdf
Nusrat Gulbarga
 
multiomics-ebook.pdf
multiomics-ebook.pdfmultiomics-ebook.pdf
multiomics-ebook.pdf
Nusrat Gulbarga
 
How Genomics & Data analysis are intertwined each other (1).pdf
How Genomics & Data analysis are intertwined each other  (1).pdfHow Genomics & Data analysis are intertwined each other  (1).pdf
How Genomics & Data analysis are intertwined each other (1).pdf
Nusrat Gulbarga
 
Newtons law of motion ~ II sem ~ m sc bioinformatics
Newtons law of motion ~  II sem ~ m sc  bioinformaticsNewtons law of motion ~  II sem ~ m sc  bioinformatics
Newtons law of motion ~ II sem ~ m sc bioinformatics
Nusrat Gulbarga
 
Chemo-informatics sem 4 MSc Bioinformatics
Chemo-informatics sem 4 MSc  BioinformaticsChemo-informatics sem 4 MSc  Bioinformatics
Chemo-informatics sem 4 MSc Bioinformatics
Nusrat Gulbarga
 
Genomes, omics and its importance, general features III semester
Genomes, omics and its importance, general features   III semesterGenomes, omics and its importance, general features   III semester
Genomes, omics and its importance, general features III semester
Nusrat Gulbarga
 
Architecture of prokaryotic and eukaryotic cells and tissues
Architecture of prokaryotic and eukaryotic cells and tissuesArchitecture of prokaryotic and eukaryotic cells and tissues
Architecture of prokaryotic and eukaryotic cells and tissues
Nusrat Gulbarga
 
Proteomics 123
Proteomics 123Proteomics 123
Proteomics 123
Nusrat Gulbarga
 
Water the unusual properties of water
Water   the unusual properties of waterWater   the unusual properties of water
Water the unusual properties of water
Nusrat Gulbarga
 
JUST SAY CHEESE
JUST SAY CHEESEJUST SAY CHEESE
JUST SAY CHEESE
Nusrat Gulbarga
 
Generation of computer
Generation of computerGeneration of computer
Generation of computer
Nusrat Gulbarga
 
Unit 1 cell biology
Unit 1 cell biologyUnit 1 cell biology
Unit 1 cell biology
Nusrat Gulbarga
 
Mutation
MutationMutation
Mutation
Nusrat Gulbarga
 
Cell signaling
Cell signalingCell signaling
Cell signaling
Nusrat Gulbarga
 
Necrosis
NecrosisNecrosis
Necrosis
Nusrat Gulbarga
 
Biophysics thermodynamics
Biophysics  thermodynamicsBiophysics  thermodynamics
Biophysics thermodynamics
Nusrat Gulbarga
 
Translation in Pro and Eu karyotes
Translation in Pro  and Eu karyotesTranslation in Pro  and Eu karyotes
Translation in Pro and Eu karyotes
Nusrat Gulbarga
 
DATABASE ADMINSTRATION
DATABASE ADMINSTRATION DATABASE ADMINSTRATION
DATABASE ADMINSTRATION
Nusrat Gulbarga
 
123 pathology of_the_endocrine_system_i_final_
123 pathology of_the_endocrine_system_i_final_123 pathology of_the_endocrine_system_i_final_
123 pathology of_the_endocrine_system_i_final_
Nusrat Gulbarga
 
Apoptosis
ApoptosisApoptosis
Apoptosis
Nusrat Gulbarga
 

More from Nusrat Gulbarga (20)

NMR .pdf
NMR .pdfNMR .pdf
NMR .pdf
 
multiomics-ebook.pdf
multiomics-ebook.pdfmultiomics-ebook.pdf
multiomics-ebook.pdf
 
How Genomics & Data analysis are intertwined each other (1).pdf
How Genomics & Data analysis are intertwined each other  (1).pdfHow Genomics & Data analysis are intertwined each other  (1).pdf
How Genomics & Data analysis are intertwined each other (1).pdf
 
Newtons law of motion ~ II sem ~ m sc bioinformatics
Newtons law of motion ~  II sem ~ m sc  bioinformaticsNewtons law of motion ~  II sem ~ m sc  bioinformatics
Newtons law of motion ~ II sem ~ m sc bioinformatics
 
Chemo-informatics sem 4 MSc Bioinformatics
Chemo-informatics sem 4 MSc  BioinformaticsChemo-informatics sem 4 MSc  Bioinformatics
Chemo-informatics sem 4 MSc Bioinformatics
 
Genomes, omics and its importance, general features III semester
Genomes, omics and its importance, general features   III semesterGenomes, omics and its importance, general features   III semester
Genomes, omics and its importance, general features III semester
 
Architecture of prokaryotic and eukaryotic cells and tissues
Architecture of prokaryotic and eukaryotic cells and tissuesArchitecture of prokaryotic and eukaryotic cells and tissues
Architecture of prokaryotic and eukaryotic cells and tissues
 
Proteomics 123
Proteomics 123Proteomics 123
Proteomics 123
 
Water the unusual properties of water
Water   the unusual properties of waterWater   the unusual properties of water
Water the unusual properties of water
 
JUST SAY CHEESE
JUST SAY CHEESEJUST SAY CHEESE
JUST SAY CHEESE
 
Generation of computer
Generation of computerGeneration of computer
Generation of computer
 
Unit 1 cell biology
Unit 1 cell biologyUnit 1 cell biology
Unit 1 cell biology
 
Mutation
MutationMutation
Mutation
 
Cell signaling
Cell signalingCell signaling
Cell signaling
 
Necrosis
NecrosisNecrosis
Necrosis
 
Biophysics thermodynamics
Biophysics  thermodynamicsBiophysics  thermodynamics
Biophysics thermodynamics
 
Translation in Pro and Eu karyotes
Translation in Pro  and Eu karyotesTranslation in Pro  and Eu karyotes
Translation in Pro and Eu karyotes
 
DATABASE ADMINSTRATION
DATABASE ADMINSTRATION DATABASE ADMINSTRATION
DATABASE ADMINSTRATION
 
123 pathology of_the_endocrine_system_i_final_
123 pathology of_the_endocrine_system_i_final_123 pathology of_the_endocrine_system_i_final_
123 pathology of_the_endocrine_system_i_final_
 
Apoptosis
ApoptosisApoptosis
Apoptosis
 

Recently uploaded

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 

Recently uploaded (20)

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 

C++Constructors

  • 1.
  • 2. MSc, Department of Bioinformatics, KSAWU, Vijayapura 2 Karnataka State Women's University VIJAYAPUR
  • 3.
  • 4. INTRODUCTION Constructor is the special type of member function in C++ class, which are automatically invoked when an object is being created. It is special because its name is same as the class name. Class A { public: int x; public: A(); //Constructor };
  • 5. Constructer can be defined either inside the class definition or outside class definition using class name and the scope of resolution :: operator Class A { int number; public: A(); // Constructor declared }; A::A() // Constructor definition { number=1; }
  • 6.
  • 7. 1. Default Constructors or Zero argument constructor 2. Parameterized Constructors 3. Copy Constructor
  • 8. “Default constructor is the constructor which doesn’t take any argument.” Or “A constructer that accepts no parameter is known as default constructer” Syntax Student { Student() { } };
  • 9. Example : Class Cube { public : int side ; public : Cube () { side 10; } }; Int main () { Cube c; cout <<“nValue is : “ << c. side;
  • 10. Output : Value is : 10 …………………………………………... Process excited after o.o148 seconds with return value 0 Press any key to continue . . .
  • 11.  “A constructer that receives arguments / parameters , is called parameterized constructer “  Construct with parameter is called parameterized constructer  All the constructor executes at the time of object creation  When we will create parameterized constructor then we have to pass argument value at the time of object creation of that class Syntax Class Student { Student (int a , int b ) // parameterized constructor { // Statements } };
  • 12. EXAMPLE Class student { public : int Roll ; String Name; float Marks; puclic : Student (int r, string nm,float m) { Roll = r; Name = nm; Marks = m; } Parameterized Constructor with three parameters
  • 13. Void Display () { cout<<“n Roll is : “<<Roll; cout<<“n Name is : “<<Name; cout<<“n Marks is : “:Marks; } }; // end of class Int main() { Student S( 2, “Justin”, 90); S.Display(); //Displaying Student Details return 0; }
  • 14. OUTPUT Roll is : 2 Name is : Justin Marks is : 90 ………………………………………………………. Process excited after o.o1708 seconds with return value 0 Press any key to continue . . .
  • 15.  These are special type of constructors which takes an object as argument and is used to copy values of data member of one object into other object  Initialization of an object through another object is called copy constructor .  In other words, copying the value of one object into another object is called copy constructor. Syntax Class Class_Name { Constructor_name(class_name & obj_name) { } };
  • 16. Class Student { Int Roll; string Name; float Marks; Public: Student(int r, string nm, float m) { Roll = r; Name = nm; Marks = m; } Student (student & S) { Roll = S.Roll; Name = S. Name; Marks = S. Marks; } constructor 1 : Parameterize Constructor Constructor 2: Copy Constructor
  • 17. EXAMPLE Void Display () { cout << “n Roll:” <<Roll; cout << “n Name:” << Name; cout << “n Marks:”<<Marks; } }; Int main () { Student S1(2, “Justin”,90); Student S2 (S1); // Statement 1 cout <<“n Values in objects S1”; S1. Display(); cout <<“n.......................”; cout<<“n Values in object S2”; S2.Display();
  • 18. OUTPUT Value in object S1 Roll : 2 Name : Justin Marks : 90 ………………………………………………. Value in object S2 Roll : 2 Name : Justin Marks : 90 ………………………………………………. Process excited after o.o4381 seconds with return value 0 Press any key to continue . . .
  • 19. To initialize data member of class: In the constructor member function (which will be declared by the programmer) we can initialize the default vales to the data members and they can be used further for processing. To allocate memory for data member: Constructor can also be used to declare run time memory (dynamic memory for the data members). Constructor is used for:
  • 20. @geeks for geeks Object-Oriented Programming with ANSI and Turbo C++ by Ashok Kamthane Publisher: Pearson India Release Date: July 2006 ISBN: 9788131703830 REFERENCES :

Editor's Notes

  1. Side= 10 is intilized at the int side by the instance variable
  2. In this example, Statement 1 is creating an object S2 and passing another object S1 as parameter to the constructor 2. Constructor 2 will take the reference of object S1 passed by the statement 1 and copy all the values of object S1 to data members associated to the object S2.