Constructor
A constructor is a member function which invoked automatically when an object declared for the class.A constructor having same name as the class name haing no return type.
For example:
If a class name is emplyee,then it's constuctor name is also employee.
Difference between constructor and destructor in C++ in hindi pdf |
//This program illustrate a constructor
#include<iostream.h>
#include<conio.h>
class student
{
Private:
int rn;
char sname[15];
double pmark;
public:
student() //constructor
{
cout<<"Enter the rollnumber":;
con>>rn;
cout<<"Enter student name:";
cin>>sname;
cout<<"Enter percentage mark:";
cin>>pmark;
}
void showdata()
{
cout<<"Roll number-<<rn<<endl;
cout<<"student name-"<<sname<<endl;
cout<<"percentsage mark-"<<pmark<<endl;
}
};
void main()
{
clrscr(0
student s1;'
s1.showdata();
getch(0;
}
Types of constructor
Constructor are of 3 types.
- Default constructor
- Parameterized constructor
- Copy constructor
A)Default constructor
Default constructor is a constuctor is supply a default value or a dummy value to the calling function,when we design the defalt constructor,within it only a single data member. can decalre object for the class,then that constant value will be supplied into the calling function.
//This program illustrate the default constructor
#include<iostream.h>
#include<conio.h>
class counter
{
private:
int n;
public:
counter()//default constructor
{
n=5;
}
void increament()
{
n++;
}
int showcount(0
{
return;
}
};
void main()
{
clrscr();
counter s1;
cout<<"Before increament the value of c1=""<<c1.showcount<<endl;
c1.increament();
cout<<"After increament the value of c1="<<c1.showcount<<endl;
getch();
}
out put:
Before increment the value of c1=5
After the increment the value of c1=6
B)Parameterized constructor
The constructor having argument is called parameterized constructor,when we design the paerameterized constructor within it the data member of the class must be assigned by the constuctor argument.'
syntax:
Class objectname(value1,value2.....);
Wap to interchange two variable value using parameterized constructor.
#include<iostream.h>
#include<conio.h>
class number
[
private:
int a;
int b;
public:
number(intx,inty) //parameterized constructor
{
a=x;
b=y;
}
void swap ()
{
a=a+b;
b=a-b;
a=a-b;
}
void show()
{
cout<<"value of a="<<a<<endl;
cout<<"value of b="<<b<<endl;
}
};
void main()
clrscr();
number n1(10,20);
cout<<"The original value"<<endl;
cout<<".-------------------
-"<<endl;
n1.show();n1.swap();
cout<<"After interchange"<<endl;
cout<<---------
-
----
-"<<endl;
n1.show();
getch();
}
out put:
The original value
Value of a=10
value of b=20
After interchange
value of a=20
value of b=10
C)Copy constructor
The copy of data value of an object into into another constructor is called copy constructor.
or
Making two object data values are equal is called copy constructor.
Syntax:
Classname newobjectname=existing objectname;
//This program illusterat ethe copy constructor
#include<iostream.h>
#include<conio.h>
#include<string.h.
class student
{
private:
int rn;
char sname[20];
float pmark;
public:
void setdata(int roll,char,stdname,flout pm){
rn=roll;
string(sname,stdname);
pmark=pm
}
void showdata(0
{
cout<<"Roll number-"<<rn<<endl;
cout<<"student name-"<<sname<<endl;
cout<,"percentage-"<<pmark<<endl;
}
};
voif main();
student s1;
s1.setdata(1,"Anil kumar swain",825);
student s2=s1;//copy constructor
cout<<"Data on 1st student"<<endl;
cout<<"------------------------"<<endl;
s1.showdata();
getch();
}
Destructor
A destructor is a member fuction whix is invoked automatically when object is destroyed.when program ends,then the object of the program will be destroyed.That means the destructor member function will be invked after termination of a program.Like constructor destructor having same name but begin with the character riled(~).The destructor can't be parameterized.The destructor does't need any ,memoryspace which means it's realse the sufficient amout of memory space.
//This program illustrate the destuctor
#include<iostream.h>
#include<conio.h>
class emplyee
{
private:
int eno;
char ename;
float sal;
public:
void getdata()
{
cout<<"Enter the employee number-:";
cin>.eno;
cout<<'Enter the employye name:";
cin>>ename;
cout<<"Enter the salary amount-";
cin>>sal;
}
~emplyee
{
cout<<"Employee number-"<<eno<<endl;
cout<<"Employee name-"<<ename<<endl;
cout<,"salary amout"<<sal<<endl;
}
};
clrscr();
employee e1;
e1.setdata();
}
- Run the program
- input the data value
- To see the result press Alt+F5
To know more about the destructor follow the link below
Click here
Email id:akashlkumarswain14@gmail.com
No comments:
Post a Comment
Thanks for join with us.