class constructor and desturctor function
TRANSCRIPT
-
7/31/2019 Class Constructor and Desturctor Function
1/27
Class Constructor & Destructor
Function
02/02/11 & 03/02/11
-
7/31/2019 Class Constructor and Desturctor Function
2/27
Initialization of Data in Class
Class car
{
private :
int x, y ;
public :
car ()
{
x = 10;y = 20;
}
car (int i )
{
x = i ;
y = i + 50;}
car (int i, int j )
{
x = i ;
y = j ;
}};
main ()
{
car c ;
car m(70);
car k( 5, 10);
.
.
.
.}
CONSTRUCTOR
Default Constructor
Parameterized
Constructor
10 20
-
7/31/2019 Class Constructor and Desturctor Function
3/27
Class Constructor
A Constructor function is a public function andhas same name as that of the class.
General Characteristics of constructor function:
(i) Constructor has same name as that of the class.
(ii) It is declared as a public function.
(iii) It may be defined inside the class or outside
the the class .If it is defined outside the class its
prototype must be declared in the class body.
-
7/31/2019 Class Constructor and Desturctor Function
4/27
Contd..
(iv) It does not return any value, nor it is of type
void .So no type is written for it.
(v) The constructor is automatically calledwhenever an object is created.
-
7/31/2019 Class Constructor and Desturctor Function
5/27
Example
#includeusing namespace std;
class Cubicle
{private :
int x, y, z;
public :
Cubicle( int, int, int ) ;
int volume () { return (x*y*z); }
};
-
7/31/2019 Class Constructor and Desturctor Function
6/27
Contd..Cubicle :: Cubicle(int a, int b, int c)
{ x = a, y = b, z = c ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
7/27
Contd..
The expected output is given below:
constructor called
constructor calledconstructor called
Volume of cube1 = 27
Volume of cube2 = 120Volume of cube3 = 40
-
7/31/2019 Class Constructor and Desturctor Function
8/27
Types of Constructors
A program may have more than one constructor
functions provided their arguments are
different.
Different types of Constructors are :
(i) Constructor with default values
(ii) Constructor with parameters(iii) Copy constructor
-
7/31/2019 Class Constructor and Desturctor Function
9/27
Example
#includeusing namespace std;
class Cubicle
{
private :int x, y, z;
public :
Cubicle() { x = 3, y = 4, z = 2 ;}
Cubicle( int a) { x = a, y = 2, z = 3 ;cout
-
7/31/2019 Class Constructor and Desturctor Function
10/27
Contd..
Cubicle( int m, int k) { x = 3, y = m, z = k ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
11/27
Contd..int main()
{
Cubicle cube2(3);
{Cubicle cube1 ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
12/27
Destructor Functions
A destructor function removes the object from
the computer memory after its relevance is over.
For local object the destructor is called at the end
of the block enclosed by the pair of braces { }wherein the object is created and for a static
object is called at the end of main() function.
The destructor function also has the same nameas class but it is preceded by the tilde symbol (~)
and has no parameters.
-
7/31/2019 Class Constructor and Desturctor Function
13/27
Contd..
#include
using namespace std;
class Cubicle
{
private :int x, y, z;
public :
Cubicle() { x = 3, y = 4, z = 2 ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
14/27
Contd..
Cubicle ( Cubicle &cubeA)
{ x = cubeA.x, y = cubeA.y, z = cubeA.z ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
15/27
int main()
{Cubicle cube2(3);
{Cubicle cube1 ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
16/27
Expected Output..
Constructor with one parameter used.
Volume of cube1 = 24
Destructor used to remove object.
Volume of cube2 = 18
Constructor with two parameters used.
Volume of cube3 = 60
Destructor used to remove object.
Volume of cube4 = 18
Destructor called to remove object.
Destructor called to remove object.
-
7/31/2019 Class Constructor and Desturctor Function
17/27
Accessing private function members of
a class
#include
using namespace std;
class Cuboid
{
private :
int surface_area();
int volume();
int x, y, z ;
public :
Cuboid(int L, int W, int H) { x = L; y = W ; z = H ; }
} ;
-
7/31/2019 Class Constructor and Desturctor Function
18/27
Cond..
int Cuboid :: surface_area(){ return 2*(x*y + y*z + z*x ); }
int Cuboid :: volume ()
{ return x*y*z ; }
int main ()
{
Cuboid C1(5,6,4) ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
19/27
Expected output
error: cannot access private member declared in class
Cuboid
error: cannot access private member declared in classCuboid
-
7/31/2019 Class Constructor and Desturctor Function
20/27
Accessing private function member
through a public member function
#include
using namespace std;
class Cuboid
{
private :
int surface_area();
int volume();
int x, y, z ;
public :
Cuboid(int L, int W, int H) { x = L; y = W ; z = H ; }
-
7/31/2019 Class Constructor and Desturctor Function
21/27
Contd..
int Surface_area () { return surface_area (); }
int Volume () { return volume() ; }
};int Cuboid :: surface_area()
{ return 2*(x*y + y*z + z*x ); }
int Cuboid :: volume (){ return x*y*z ; }
-
7/31/2019 Class Constructor and Desturctor Function
22/27
Contd..
int main(){
Cuboid C1(5,6,4) ;
cout
-
7/31/2019 Class Constructor and Desturctor Function
23/27
Expected output
Volume of cuboid C1 120
Surface area of C1 = 148
-
7/31/2019 Class Constructor and Desturctor Function
24/27
Local Classes
A class may be declared inside a function, in
which it is called local class.
The scope of a local class is up to the function
definition.
-
7/31/2019 Class Constructor and Desturctor Function
25/27
Classes local to a function
#include
using namespace std ;
int Function ()
{ class X
{ private :
int x ;
public :
X ( int a ) { x = a ; }
int getx() { return x ; }
} x_object(5);
-
7/31/2019 Class Constructor and Desturctor Function
26/27
Contd..
class Y
{
private :
int y ;
public :
Y(int b ) { y = b ;}
int gety () { return y ; }
} y_object (10) ;
return (x_object.getx() * (y_object.gety() ) ) ;
}
-
7/31/2019 Class Constructor and Desturctor Function
27/27
Contd..
void main ()
{
cout