Professional Documents
Culture Documents
Constructor & Destructor PDF
Constructor & Destructor PDF
Introduction
#include<iostream> int main(){
using namespace std; test ob;
class test ob.display();
{ }
int x;
public:
void set(){ OUTPUT
x=10;} 10
void display(){
cout<<x;
}
};
Properties
Declared as public
Automatically invoked
No return types
Cannot be inherited
Can have default arguments
Cannot be declared as virtual
Example
#include<iostream> int main(){
using namespace std; test ob;
ob.display();
class test{ }
int x;
public:
test(){
OUTPUT
x=10;
} 10
void display(){
cout<<x;
}
};
Parameterized Constructor
#include<iostream> int main(){
using namespace std; test ob(2);
class test ob.display();
{ }
int x;
public:
Implicit Call
test(int a){ OUTPUT
x=a;
2
}
void display(){
cout<<x;
}};
Parameterized Constructor
#include<iostream> int main(){
using namespace std; test ob=test(2);
class test ob.display();
{ }
int x;
public: Explicit Call
test(int a){ OUTPUt
x=a; 2
}
void display(){
cout<<x;
}};
Multiple Constructors
#include<iostream> test(int a,int b)
using namespace std; {
class test cout<<a<<"\t"<<b;
{ } OUTPUT
public: };
2
test() 2 3
{ int main(){ Hi
cout<<"Hi"; test ob= test(2);
} test ob1=test(2,3);
test(int a){ test ob3=test();
cout<<a; }
}
Multiple Constructors
#include<iostream> int main(){
using namespace std; test ob= test(2);
class test test ob1=test(2,3);
{ test ob3=test();
OUTPUT
public: }
test(int a){ Error
cout<<a;
}
test(int a,int b){
cout<<a<<"\t"<<b;
}};
Multiple Constructors
#include<iostream> test(float x, float y){
using namespace std; cout<<x<<"\t"<<y;
class test{ }
public: };
OUTPUT
test(){
cout<<"Hi"; int main(){
test.cpp:26:22: error: call of overloaded 'test(double, double)' is ambiguous
} test ob2=test(2.3,3.4); test ob= test(2); ^
test.cpp:17:1: note: candidate: test::test(float, float)
test(int a){ test(float x,float y) test ob1=test(2,3); ^~~~
cout<<a; test ob2=test(2.3,3.4);
} test.cpp:13:1: note: candidate:test
test::test(int,
ob3=test();int)
test(int a,int b)
test(int a,int b){ }
cout<<a<<"\t"<<b;
}
Example
#include<iostream> int main(){
using namespace std; test ob= test(2);
class test }
{ OUTPUT
public:
2 4
test(int a,int b=4){
cout<<a<<"\t"<<b;
}
};
Example
#include<iostream> int main(){
using namespace std; test ob= test(2);
class test }
{ OUTPUT
public: 2 4 10
test(int a,int b=4, int c=10){
cout<<a<<"\t"<<b<<"\t"<<c;
}
};
Example
#include<iostream> int main(){
using namespace std; test ob= test(2);
class test }
{ OUTPUT
public: Error
test(int b=4, int c=10,int a){
cout<<a<<"\t"<<b<<"\t"<<c;
}
};
Example
#include<iostream> int main(){
using namespace std; test ob1(2);
class test{ test ob2(ob1);
int x; }
public: OUTPUT
test(int a){
x=a; 2 2
cout<<x;
}
test(test & ob){
x=ob.x;
cout<<x;
}};
Example
#include<iostream> int main(){
using namespace std; test ob1(2);
class test{ ob1.display();
int x; test ob2(ob1);
public: ob2.display();
OUTPUT
test(int a){ }
x=a; 2 2
}
test(test & ob){
x=ob.x;
}
void display(){
cout<<x;
}};
Example
#include<iostream> int main(){
using namespace std; test ob1(2);
class test{ ob1.display();
int x; test ob2(ob1);
OUTPUT
public: ob2.display();
test(int a){ } 2 2
x=a;
}
void display(){
cout<<x;
}};
Example
#include<iostream> int main(){
using namespace std; int x;
class test{ cin>>x;
int x; test ob1(x); OUTPUT
public: ob1.display();
23
test(int a){ test ob2(ob1); 23 23
x=a; ob2.display();
} }
void display(){
cout<<x;
}};
Example
#include<iostream> ~test(){
using namespace std; nob--;
cout<<"\n"<<nob;
int nob=0; }};
OUTPUT
class test{ int main(){
int x; test ob1(5); 1
2
public: { 1
test(int a){ test ob2(10); 2
x=a; } 1
0
nob++; test ob3(20);
cout<<"\n"<<nob; }
}
#include<iostream> int test::i;
using namespace std;
class test{ int main(){
static int i; test ob,ob1; OUTPUT
public: void getdata() ob.setdata();
2 2
{ cout<<i; } ob1.setdata();
void setdata() ob.getdata();
{ i++; } ob1.getdata();
}; }
Static Data Member
A static member variable is initialized to 0 when the first object is created.
No other initialization is permitted.
Only one copy of the data member is created and is shared by all the objects.
It is visible only within the class, but its lifetime is the entire program.
Type and scope of each static member variable must be defined outside the
class definition as the static data members are stored separately rather than as
a part of an object.
Also referred to as class variables.
Code
#include<iostream> int test::i;
using namespace std; int main(){
class test{ test::disp();
static int i; test ob,ob1; OUTPUT
setdata() { i++; }
static void disp() { cout<<"Display
invoked"; setdata(); getdata();
}
};
Code
#include<iostream> int test::i;
using namespace std;
class test{ int main()
static int i; {
public: test::disp(); OUTPUT
void getdata() { cout<<i; } void } Display invoked 1
setdata() { i++; }
static void disp() {
test ob;
cout<<"Display invoked";
ob.setdata(); ob.getdata();
}};
Static Member Functions
A static member function can have access to only other static member
function or variables declared in the same class.
A static member function can be called using the class name instead of the
object name.