Sie sind auf Seite 1von 27

Class Constructor & Destructor Function

02/02/11 & 03/02/11

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; } }; 10 20

main () { car c ; car m(70); car CONSTRUCTOR k( 5, 10); . . Default Constructor . . } Parameterized Constructor

Class Constructor
A Constructor function is a public function and has 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.

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 called whenever an object is created.

Example
#include<iostream> using namespace std; class Cubicle { private : int x, y, z; public : Cubicle( int, int, int ) ; int volume () { return (x*y*z); } };

Cubicle :: Cubicle(int a, int b, int c) { x = a, y = b, z = c ; cout<<Constructor called<<endl ; } int main() { Cubicle cube1(3,3,3); Cubicle cube1(4,5,6); Cubicle cube1(2,4,5); cout<< Volume of cube1 = <<cube1.volume()<<\n; cout<< Volume of cube2 = <<cube2.volume()<<\n ; cout<< Volume of cube3 = <<cube3.volume()<<\n ; return 0 ; }

Contd..

Contd..
The expected output is given below: constructor called constructor called constructor called Volume of cube1 = 27 Volume of cube2 = 120 Volume of cube3 = 40

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

Example
#include<iostream> using 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<< Constructor with one parameter used ;}

Contd..
Cubicle( int m, int k) { x = 3, y = m, z = k ; cout<< Constructor with one parameter used ;} Cubicle ( Cubicle &cube2) { x = cube2.x, y = cube2.y, z = cube2.z ; cout <<\nCopy constructor called. ;} int volume() { return (x*y*z) ; } };

Contd..
int main() { Cubicle cube2(3); {Cubicle cube1 ; cout<< Volume of cube1<<cube1.volume()<<\n;} cout<<Volume of cube2 =<<cube2.volume()<<\n; { Cubicle cube3(4,5) ; cout<< Volume of cube3 = <<cube3.volume()<<\n ; } { Cubicle cube4 ; cube4 = cube2 ; cout<< Volume of cube4 = <<cube4.volume()<<\n ; return 0 ; }

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 name as class but it is preceded by the tilde symbol (~) and has no parameters.

Contd..
#include<iostream> using namespace std; class Cubicle { private : int x, y, z; public : Cubicle() { x = 3, y = 4, z = 2 ; cout<<Default constructor is called ;} ~Cubicle () { cout<<Destructor is called to remove object.\n Cubicle( int a, int b, int c) { x = a, y = b, z = c ; cout<<Parametric Constructor called ;}

Contd..
Cubicle ( Cubicle &cubeA) { x = cubeA.x, y = cubeA.y, z = cubeA.z ; cout <<\nCopy constructor called. ;} int volume() { return (x*y*z) ; } };

int main() {Cubicle cube2(3); {Cubicle cube1 ; cout<< Volume of cube1<<cube1.volume()<<\n;} Cubicle cube2(6,4,5) ; cout<<Volume of cube2 =<<cube2.volume()<<\n; { Cubicle cube3(cube2) ; cout<< Volume of cube3 = <<cube3.volume()<<\n ;} return 0 ; }

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.

Accessing private function members of a class


#include<iostream> 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 ; } };

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 << Volume of cuboid C1<<C1.volume()<< \n; cout << surface area of C1 = <<C1.surface_area()<<\n ; return 0 ; }

Expected output
error: cannot access private member declared in class Cuboid error: cannot access private member declared in class Cuboid

Accessing private function member through a public member function


#include<iostream> 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 ; }

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 ; }

Contd..
int main() { Cuboid C1(5,6,4) ; cout << Volume of cuboid C1 << C1.Volume() << \n ; cout << Surface area of C1 = << C1.Surface_area() << \n ; return 0 ; }

Expected output
Volume of cuboid C1 120 Surface area of C1 = 148

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.

Classes local to a function


#include<iostream> using namespace std ; int Function () { class X { private : int x ; public : X ( int a ) { x = a ; } int getx() { return x ; } } x_object(5);

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() ) ) ; }

Contd..
void main () { cout << Function () << endl ; }

Das könnte Ihnen auch gefallen