Constructors and Destructors


How to use constructor in C++/Cpp
Constructors in C++:
l  A constructor is a member function of a class.
l  It is a special member function of a class whose name is same as the name of class.
l  It has no return type.
l  Constructors do not call explicitly.
l  It is invoked implicitly when an object of class is created.
l  An implicit constructor is generated by compiler whenever we do not write any constructor in class definition. This implicit constructor would be called during object creation.
l  If we define a constructor in the class then there will be no implicit constructor created by class.
l  Constructor should always be defined in public as we create objects outside the class definition. Although there are scenarios where we would like to define constructor in private.
l  Constructor solves the problem of initialization. We can initialize variables of primitive data type during creation of variable but this feature is optional.

For example: int x=3; //creation of variable x and initialize it with a value 3
Let us assume complex is a user defined data type.
complex c1=4; //error this is called problem of initialization during creation of object
l  Since static member functions are not associated with any particular object they can never access object’s non static variables. Although we can create an object of same class in static member function and can access object’s member from there.
 /*C++ program for implementing default constructor, one argument constructor, copy constructor and assignment operator overloading*/

//copy constructor and assignment op OL int *
#include<iostream>
using namespace std;

class A
{
private:
int *p;
int a;
public:
A(){cout<<"Default constr."<<endl;p= new int;*p=0;a=0;}

A(int aa){cout<<"1 arg constr."<<endl;p=new int; *p=aa; a=aa;}

A(const A& obj)
{
cout<<"Copy constr."<<endl;
a=obj.a;
p = new int;
*p = *obj.p;
}

A & operator=(const A& obj)
{
cout<<"Assignment."<<endl;
this->a = obj.a;
*this->p = *obj.p;
return *this;
}



void display()
{
cout<<"ptr ="<<p<<"\t";
cout<<"data="<<a<<"\n\n";
}


~A()
{
cout<<"destructor called."<<endl;
delete p;
}


};

int main()
{
A o1; o1.display();

A o2(10); o2.display();

A o3=o2; o3.display();

A o4; o4.display();

o4=o2; o4.display();



return 0;
}

/*
Default constr.
ptr =0xe486010 data=0

1 arg constr.
ptr =0xe486030 data=10

Copy constr.
ptr =0xe486050 data=10

Default constr.
ptr =0xe486070 data=0

Assignment.
ptr =0xe486070 data=10

destructor called.
destructor called.
destructor called.
destructor called.

*/

===============
#include<iostream>
#include<string.h>
using namespace std;

class sample
{
private:
char *name;
int data;
public:
sample(char p[],int d)
{
cout<<"\n Argumented Constructor called.\n";
name = new char[10];
strcpy(name , p);
data = d;
}
~sample(){
delete[] name;
}

sample(const sample &obj)
{
cout<<"\n Copy Constructor Called.\n";
data = obj.data;
//cout<<&obj.name<<"\n";
name = new char[10];
strcpy(name, obj.name);
}

sample & operator = (const sample & obj)
{
cout<<"\n operator overloading is called.\n";
strcpy(this->name, obj.name);
return *this;
}

void display()
{
cout<<"name ="<<name<<"\n";
cout<<"data="<<data<<"\n";
}


};

int main(){
sample d1("english winglish", 20); d1.display();
sample d2=d1; d2.display();

sample d3("Hello World", 20); d3.display();
d3 =d1; d3.display();

return 0;
}
/*

Argumented Constructor called.
name =english winglish
data=20

Copy Constructor Called.
name =english winglish
data=20

Argumented Constructor called.
name =Hello World
data=20

operator overloading is called.
name =english winglish
data=20
*/
              Coming Soon More Updates.
How to use various types of constructors in C++/CPP
      How to use various types of constructors in C++/CPP
      How to use various types of constructors in C++/CPP
         How to use various types of constructors in C++/CPP

No comments:

Post a Comment