Saturday, August 8, 2009

CONSTRUTERS AND DESTRUCTORS

Constructors and destructors:
we have seen so far a few examples of classes being implement .In all the case we have used member functions to provide intial values to the private member variables.
For exam:
x.gedata(100,299,25)
x.getdata() .
All these functions call statements are used with the appropriate objects that have already been created.These functions cannot be used to intialize the member variablesat the time of creation of their objects.
once of the aim of c++ is to create user defined data types such as class,that behave very much similar to the built in types.This means the we should be able ti intialize a class type variable(object) when it is declared much the same way as intialization of an ordiinary variable.
for exp;
int m=20;
c++ provides a special member function called constructor which enables an object to initialize itself when it is created.Constructors are the special member functions whose name is same name as the class name and this memmber function gets invoked automtically whenever the object of its associated class is created.It is called as constructor because it constructts the data members of the class.
The Constructor Functions have some special characterstics
1.They should be declared in public section.
2.They ae invoked automatically when the objects are created
3. They do not have return types they cannot return values.
Constructors are of various Types.
1. A constructor that accepts no parameters is called paameterzed
2.The `constructors that can take arguments are called a parameterized 3.constructors.This type of functions allows to intialize the data elements with different values.
Constructors with default Values
It is possible ot define the constructors with default values which are called as the constuctors with default arguments.
4.Copy constuctor:-A copy constructor are those which are used to declare as well as intiialize an object from another object.

No comments:

Post a Comment