SlideShare une entreprise Scribd logo
1  sur  29
Copy Constructor
It is used to declare and initialized an object from
another object.
It takes reference to an object of the same class as
itself as an arguments.
Example:
integer I1;
integer I2(I1);
Sample Program
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(integer &x)
{
m=x.m; n=x.n;
}
integer()
{
m=100; n=100;
}
void display()
{
cout<<"m= "<<m<<"
and n= "<<n<<endl;
}
};
void main()
{
clrscr();
integer int1;
int1.display();
integer int2(int1);
int2.display();
getch();
}
OUTPUT
In C++, We can have more than one
constructor in a class with same name,
as long as each has a different list of
arguments?
When is user defined copy constructor needed?
• Default copy constructor for each class which does a
member wise copy between objects
• Define our own copy constructor only if an object has
pointers or any run time allocation of resource like file
handle, a network connection..etc.
Default constructor does only shallow copy.
Copy constructor vs Assignment Operator
MyClass t1, t2;
MyClass t3 = t1; // ----> (1)
t2 = t1; // -----> (2)
Copy constructor is called when a new object is created from an
existing object, as a copy of the existing object.
Assignment operator is called when an already initialized object
is assigned a new value from another existing object.
In the above example (1) calls copy constrictor and
(2) calls assignment operator.
Can we make copy constructor private?
class A
{
private:
int x;
A() { x=7; cout<<“Constructor called”; }
public:
void show()
{
A();
cout<< “X is”<<x;
}
};
main()
{
A *a;
a->show();
}
Dynamic Constructors
• The constructor can also be used to allocate memory
while creating objects.
• To allocate the right amount of memory for each
object.
• Allocation of memory to objects at the time of their
construction is known as dynamic construction of
objects.
Example
int number()
{
for(int h=0;h<s;h++)
{
int n;
cout<<"enter number";
cin>>n;
return n;
}
}
void input()
{
for(int h=0;h<s;h++)
{
cout<<"enter number";
cin>>x[h];
}
}
class num
{
private:
int *x,s;
public:
num()
{
s=number();
x=new int[s]; }
void sum()
{
int adi=0;
for(int h=0;h<s;h++)
adi+=x[h];
cout<<"Sum is"<<adi;
}
};
main()
{
num n1;
n1.input();
n1.sum();
}
Dynamic Initialization of Objects
class construct
{
public:
float area;
construct()
{
area = 0;
}
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
int x,y;
construct o;
cout<<“Enter variable values”;
cin>>x>>y;
construct o2(x,y);
o.disp();
o2.disp();
return 0;
}
Overloading Constructor
Constructor overloading is the process of defining
more than one constructor in the same class.
C++ permits us to use multiple constructor in the
same class.
Sample Program
class construct
{
public:
float area;
// Constructor with no parameters
construct()
{
area = 0;
}
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 0;
}
class Demo
{
private:
int X,Y;
public:
Demo()
{
X = 0; Y = 0;
}
Demo(int x, int y=20)
{
X = x; Y = y;
}
void putValues()
{
cout << endl << "Value of X : " << X;
cout << endl << "Value of Y : " << Y
}
};
int main()
{
Demo d1= Demo(10);
cout << endl <<"D1 Value Are : ";
d1.putValues();
Demo d2= Demo(30,40);
cout << endl <<"D2 Value Are : ";
d2.putValues();
return 0;
}
Constructors with default arguments
Const member functions in C++
A function becomes const when const keyword is used
in function’s declaration.
The idea of const functions is not allow them to
modify the object on which they are called
Local Classes
A class declared inside a function becomes local to that
function and is called Local Class in C++.
void fun()
{
class Test // local to fun
{
/* members of Test class */
};
}
int main()
{
return 0;
}
Interesting facts of local class
1) A local class type name can only be used in
the enclosing function.
2) All the methods of Local classes must be
defined inside the class only
• A Local class cannot contain static data members.
It may contain static functions though
• Member methods of local class can only access
static and enum variables of the enclosing
function.
• Local classes can access global types, variables
and functions. Also, local classes can access other
local classes of same function.
Pointer to Members
A::* denotes pointer of member of class A
&A::m Address of m member of class A
Cont……
Pointer to member function
return_type (class_name::*ptr_name) (argument_type) =
&class_name::function_name ;
#include<iostream>
using namespace std;
class Data
{ public:
void f () { cout<<"hi"; }
};
int main()
{
Data d;
void (Data::*fp1) () = &Data::f;
(d.*fp1)();
}
void (Data::*fp1) () = &Data::f;
Destructor
It is used to destroy the objects created by the
constructor.
It is called for the class object whenever it passes the
scope in the program.
Whenever new is used in the constructor to allocate
the memory delete should be used in the destructor
to free the memory for future use.
Characteristics of Destructor
It has same name as the class name but is preceded
by tilde (~) sign.
It has no return type and do not take any arguments.
It can not be overloaded.
It is called whenever the object get out of its scope.
Sample Program
#include<iostream.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
cout<<"Default
Constructor is
called"<<endl;
}
integer(int x, int y)
{
m=x;
n=y;
cout<<"Parameterize
d Constructor is
called"<<endl;
}
~integer()
{
cout<<"Object is
detroyed"<<endl;
}
void display()
{
cout<<"m=
"<<m<<" and n=
"<<n<<endl;
}
};
void main()
{
clrscr();
{
integer int1;
int1.display();
}
{
integer int2(2,5);
int2.display();
}
}
OUTPUT

Contenu connexe

Tendances

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
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overviewgourav kottawar
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Sumant Tambe
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsDudy Ali
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++Patrick Viafore
 
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
 

Tendances (20)

Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
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
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Compile time polymorphism
Compile time polymorphismCompile time polymorphism
Compile time polymorphism
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Templates
TemplatesTemplates
Templates
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
C++11
C++11C++11
C++11
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++
 
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
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
 

Similaire à Constructor in c++

Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#singhadarsh
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpvoegtu
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharphmanjarawala
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpvoegtu
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.pptinstaface
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxurvashipundir04
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3mohamedsamyali
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011Deepak Singh
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 

Similaire à Constructor in c++ (20)

Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
class and objects
class and objectsclass and objects
class and objects
 
Bc0037
Bc0037Bc0037
Bc0037
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 

Dernier

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 

Dernier (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Constructor in c++

  • 1. Copy Constructor It is used to declare and initialized an object from another object. It takes reference to an object of the same class as itself as an arguments. Example: integer I1; integer I2(I1);
  • 2. Sample Program #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(integer &x) { m=x.m; n=x.n; } integer() { m=100; n=100; } void display() { cout<<"m= "<<m<<" and n= "<<n<<endl; } };
  • 3. void main() { clrscr(); integer int1; int1.display(); integer int2(int1); int2.display(); getch(); }
  • 5. In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments?
  • 6. When is user defined copy constructor needed? • Default copy constructor for each class which does a member wise copy between objects • Define our own copy constructor only if an object has pointers or any run time allocation of resource like file handle, a network connection..etc.
  • 7. Default constructor does only shallow copy.
  • 8.
  • 9. Copy constructor vs Assignment Operator MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. Assignment operator is called when an already initialized object is assigned a new value from another existing object. In the above example (1) calls copy constrictor and (2) calls assignment operator.
  • 10. Can we make copy constructor private? class A { private: int x; A() { x=7; cout<<“Constructor called”; } public: void show() { A(); cout<< “X is”<<x; } }; main() { A *a; a->show(); }
  • 11. Dynamic Constructors • The constructor can also be used to allocate memory while creating objects. • To allocate the right amount of memory for each object. • Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.
  • 12. Example int number() { for(int h=0;h<s;h++) { int n; cout<<"enter number"; cin>>n; return n; } } void input() { for(int h=0;h<s;h++) { cout<<"enter number"; cin>>x[h]; } } class num { private: int *x,s; public: num() { s=number(); x=new int[s]; } void sum() { int adi=0; for(int h=0;h<s;h++) adi+=x[h]; cout<<"Sum is"<<adi; } }; main() { num n1; n1.input(); n1.sum(); }
  • 13. Dynamic Initialization of Objects class construct { public: float area; construct() { area = 0; } construct(int a, int b) { area = a * b; } void disp() { cout<< area<< endl; } }; int main() { int x,y; construct o; cout<<“Enter variable values”; cin>>x>>y; construct o2(x,y); o.disp(); o2.disp(); return 0; }
  • 14. Overloading Constructor Constructor overloading is the process of defining more than one constructor in the same class. C++ permits us to use multiple constructor in the same class.
  • 15. Sample Program class construct { public: float area; // Constructor with no parameters construct() { area = 0; } // Constructor with two parameters construct(int a, int b) { area = a * b; } void disp() { cout<< area<< endl; } }; int main() { // Constructor Overloading // with two different constructors construct o; construct o2( 10, 20); o.disp(); o2.disp(); return 0; }
  • 16. class Demo { private: int X,Y; public: Demo() { X = 0; Y = 0; } Demo(int x, int y=20) { X = x; Y = y; } void putValues() { cout << endl << "Value of X : " << X; cout << endl << "Value of Y : " << Y } }; int main() { Demo d1= Demo(10); cout << endl <<"D1 Value Are : "; d1.putValues(); Demo d2= Demo(30,40); cout << endl <<"D2 Value Are : "; d2.putValues(); return 0; } Constructors with default arguments
  • 17. Const member functions in C++ A function becomes const when const keyword is used in function’s declaration. The idea of const functions is not allow them to modify the object on which they are called
  • 18. Local Classes A class declared inside a function becomes local to that function and is called Local Class in C++. void fun() { class Test // local to fun { /* members of Test class */ }; } int main() { return 0; }
  • 19. Interesting facts of local class 1) A local class type name can only be used in the enclosing function.
  • 20. 2) All the methods of Local classes must be defined inside the class only
  • 21. • A Local class cannot contain static data members. It may contain static functions though • Member methods of local class can only access static and enum variables of the enclosing function. • Local classes can access global types, variables and functions. Also, local classes can access other local classes of same function.
  • 22. Pointer to Members A::* denotes pointer of member of class A &A::m Address of m member of class A
  • 24. Pointer to member function return_type (class_name::*ptr_name) (argument_type) = &class_name::function_name ; #include<iostream> using namespace std; class Data { public: void f () { cout<<"hi"; } }; int main() { Data d; void (Data::*fp1) () = &Data::f; (d.*fp1)(); } void (Data::*fp1) () = &Data::f;
  • 25. Destructor It is used to destroy the objects created by the constructor. It is called for the class object whenever it passes the scope in the program. Whenever new is used in the constructor to allocate the memory delete should be used in the destructor to free the memory for future use.
  • 26. Characteristics of Destructor It has same name as the class name but is preceded by tilde (~) sign. It has no return type and do not take any arguments. It can not be overloaded. It is called whenever the object get out of its scope.
  • 27. Sample Program #include<iostream.h> class integer { int m,n; public: integer() { m=0; n=0; cout<<"Default Constructor is called"<<endl; } integer(int x, int y) { m=x; n=y; cout<<"Parameterize d Constructor is called"<<endl; } ~integer() { cout<<"Object is detroyed"<<endl; }
  • 28. void display() { cout<<"m= "<<m<<" and n= "<<n<<endl; } }; void main() { clrscr(); { integer int1; int1.display(); } { integer int2(2,5); int2.display(); } }