We
initialize the built in data type during declaration like int a=5
We also
initialize array during declaration int a[ ]={1,2,3,4};
Char name[
]=”Sachin”;
We also
initialize structure like given below
#include<iostream.h>
#include<conio.h>
struct
player
{
int id,age;
char
name[20];
};
void main()
{
player
p={7,32,"Dhoni"};
cout<<p.id<<"-"<<p.name<<"-"<<p.age<<endl;
getch();
}
Here is the
question how we can initialize the class?
Solution is
constructor
Before
learning how to define constructor we should know more about Feature of
constructer
Features of constructor
1.
It has no return data type
2.
It is also like a member function
3.
Name of class and constructor is always same
4.
It is implicitly called
5.
It can be overloaded
6.
It can’t be inherited but derived class can call
base class constructor
7.
It follows the access rule
8.
When object is declared constructor function
will be invoke according to matching parameter
Defining constructor
class student
{
int age;
public:
student() // constructor
{
}
void get();
void show();
};
Here we have defined one constructor we can see
that it is defined like function, name of constructor and class is same, no
return type even void
When we declare object student s constructor will
be invoked. Here we can see that no value is passed in constructor so no value
will be passed in object
We can also define more than one constructor in a
class. As we know that it can be overloaded. When more than one constructor is
defined constructor will be called according to matching of arguments.
Ex.
class student
{
int age;
public:
student()
// constructor 1
{
}
Student(int a) // constructor 2
{
age=a;
}
void get();
void show();
};
If we declare object student s constructor 1 will
be invoked as its argument is matching, similarly when we declare object like
student s(5);
Constructor 2 will be invoked
Destructor:-
as we have discussed constructor is used to initialize the object. If we
declare so many objects in a program then each object will occupy memory. If
some constructor is not in use memory occupied by it must be released. C++ compiler has one interesting features
which release the memory occupied by an object when its life time is over
Destructor is a member function which destroy the
object/release the memory occupied by object when its life time is over
It can be define like
~student()
{
}
We use ~(Tild) sign to define destructor
Features
of destructor is almost same like constructor. Main difference between
constructor and destructor is that constructor is used to initialize the object
while destructor is used to destroy the object. only one destructor can be
defined for class but destructor can't be overloaded.
Features of destructor
1.
It has no return data type
2.
It is also like a member function
3.
Name of class and destructor is always same
4.
It is implicitly called when life time of object
is over.
5.
It can’t be inherited but derived class can call
base class constructor
6.
It follows the access rule
7.
When object is declared destructor function
will be invoke according to matching parameter
Types of constructor
1.
Default constructor
2.
Parametrized constructor
3.
Copy constructor
1.
Default constructor
Student()
{
}
Above
define constructor is called default constructor which does not have any arguments
When
we declare object
student
s;
default
constructor will be invoked
student
()
{
Age
=15;
}
This
is also example of default constructor it also not having argument it will also
called when student s ; object will be declared and it will initialize the age
of student with 10.
Note:- student() { } and student() { age =15} can’t
be define in same class as it is not
satisfying
the overloading rules.
2.
Parametrized constructor:
student(int a)
{
age=a;
}
For calling this constructor we
have to declare object with matchin argument like
Student s(5);
3. Copy constructor :- copy constructor
is used to copy one object into another object.
If we talk about built in data type we assign value of one variable into
other like
int a =5;
this
type of initialization is called explicit assignment
there
is another method of assignment int a(5);
we
also assign object of structure like student s1,s2;
s1=s2
is
there any method to assign one class object into another object of same class. It
is possible with the help of copy constructor
copy
constructor always takes one argument of class object in call by reference mode
Example
student(student &s)
example of copy constructor
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll,marks;
char name[10];
public:
student()
{
cout<<"\n default constructor" ;
}
student(int r, int m, char n[])
{
cout<<"\n parametrized constructor";
roll=r;
strcpy(name ,n);
marks =m;
}
student(student &s)
{
cout<<"\n copy constructor\n";
roll= s.roll;
strcpy(name,s.name);
marks=s.marks;
}
~student(){
cout<<"\n destructor\n";
}
void get()
{
cout<<"\n enter roll" ;
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter marks\n";
cin>>marks;
}
void show()
{
cout<<roll<<"\t"<<name<<"\t"<<marks<<endl ;
}
};
void main()
{
{
student s(5,10,"Rahul"); // parametrized constructor is called
student m ; // default constructor is called
m.get();
student n(m); // copy constructor is called
m.show();
s.show();
n.show();
getch();
}
getch();
}
example of copy constructor
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll,marks;
char name[10];
public:
student()
{
cout<<"\n default constructor" ;
}
student(int r, int m, char n[])
{
cout<<"\n parametrized constructor";
roll=r;
strcpy(name ,n);
marks =m;
}
student(student &s)
{
cout<<"\n copy constructor\n";
roll= s.roll;
strcpy(name,s.name);
marks=s.marks;
}
~student(){
cout<<"\n destructor\n";
}
void get()
{
cout<<"\n enter roll" ;
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter marks\n";
cin>>marks;
}
void show()
{
cout<<roll<<"\t"<<name<<"\t"<<marks<<endl ;
}
};
void main()
{
{
student s(5,10,"Rahul"); // parametrized constructor is called
student m ; // default constructor is called
m.get();
student n(m); // copy constructor is called
m.show();
s.show();
n.show();
getch();
}
getch();
}
No comments:
Post a Comment