CPP
Constructor
& Destructor
Fill in the blanks
1. Creating on Object is called Constructor
2. Destructor means Deleting an
object
3. Write a Symbol of Destructor ~
4. Class name same as function name is
called Constructor
5. How many Constructor presents in CPP 5
6. Constructor with no parameter is
called Default Constructor
7. Constructor with argument are called Parameterized
Constructor
8. Constructor with object reference as
its argument is called Copy Constructor
9. Write a symbol of Copy Constructor &
10.
Allocating memory while creating an objects is called Dynamic Constructor
11.
Constructor overloading means function name same , argument different
Answer the following Question
(4 marks)
1.Write
Default constructor Program
class student{
private:
int rlno;
float mark;
public:
student(){} /*default constructor*/
void get()
{
cout<<"get rlno and mark";
cin>>rlno>>mark;
}
void put()
{
cout<<"rlno="<<rlno;
cout<<"mark="<<mark;
}
};
void main()
{
student
s; /*calling
default constructor*/
}
2.Write a
Program for Destructor
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){}
student(int r,float m)
{
rlno=r;
mark=m;
}
void put()
{
cout<<"Rlno="<<rlno<<endl;
cout<<"Mark="<<mark;
}
~student()
{
cout<<"destructor called";
}
};
void main()
{
student
k(120,78);
clrscr();
k.put();
getch();
}
3.Write a
Program for Copy Constructor
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){}
student(int r,float m)
{
rlno=r;
mark=m;
}
student(student
&t)
{
rlno=t.rlno;
mark=t.mark;
}
void put()
{
cout<<"rlno="<<rlno<<endl;
cout<<"mark="<<mark<<endl;
}
};
void main()
{
student
s;
student
k(120,78);
student
R(k);
clrscr();
R.put();
getch();
}
4. Write a
Program
for Parametrized Constructor
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){} /* default constructor*/
student(int r,float m) /*parameterized constructor*/
{
rlno=r;
mark=m;
}
void put()
{
cout<<"rlno="<<rlno<<endl;
cout<<"mark="<<mark;
}
};
void main()
{
student
s;
student
k(120,78);
clrscr();
k.put();
getch();
}
Answer the following Question
(10 marks)
1.What is
Constructor and Destructor?Write its types.Write a Program for Default
Constructor, Parameterized Constructor, Copy Constructor, Constructor
Overloding Program
Constructor
Creating an object is called Constructor
Destructor
Deleting an object is called Destructor
TYPES OF
CONSTRUCTOR
· default constructor
· parameterized
constructor
· copy constructor
· dynamic constructor
· constructor over
loading
Default and Parameterized
Constructor
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){} /* default constructor*/
student(int r,float m) /*parameterized constructor*/
{
rlno=r;
mark=m;
}
void put()
{
cout<<"rlno="<<rlno<<endl;
cout<<"mark="<<mark;
}
};
void main()
{
student
s;
student
k(120,78);
clrscr();
k.put();
getch();
}
Copy
Constructor
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){}
student(int r,float m)
{
rlno=r;
mark=m;
}
student(student
&t)
{
rlno=t.rlno;
mark=t.mark;
}
void put()
{
cout<<"rlno="<<rlno<<endl;
cout<<"mark="<<mark<<endl;
}
};
void main()
{
student
s;
student
k(120,78);
student
R(k);
clrscr();
R.put();
getch();
}
Constructor
Overloading
#include<iostream.h>
#include<conio.h>
class student{
private:
int rlno;
float mark;
public:
student(){}
student(int r,float m)
{
rlno=r;
mark=m;
}
void put()
{
cout<<"Rlno="<<rlno<<endl;
cout<<"Mark="<<mark<<endl;
}
};
void main()
{
student
s;
student
k(120,78);
clrscr();
k.put();
getch();
}
No comments:
Post a Comment