Академический Документы
Профессиональный Документы
Культура Документы
Destructor
C++
Constructor: Definition
A constructor is a special member function
which is used to initialize the objects of a class.
Note:-
A constructor is distinct from other member functions
because its name is same as the name of the class.
It is the first member function executed
automatically when an object is created.
The only restriction that applies to constructor is that
it must not have a return type (not even void).
It cannot be inherited, though a derived class can call
the base class constructor.
General syntax of constructor
class classname
{
-------------
-------------
-------------
classname( parameter list);//constructor declaration
-----------------
};
//constructor definition
class name::classname(parameter list)
{
--------
}
EXAMPLE
Example:
class Bank
{
private:
int x,y;
---------
----------
public:
Bank( );
};
Bank :: Bank( )
{
x=0;
y=0;
}
void main()
{
Bank B;
}
When an object B is created ,constructor will be called automatically and it will initialize the
data members x=0 and y=0
NEED FOR A CONSTRUCTOR
A Constructor is used to initialize the
objects of the class being created. So, it
is needed whenever:
1. When you want to initialise the data member
of the class with some default values.
2. When you want to initialise the data
members of the class with arguments
passed to the constructor.
3. When you want to carry out any work when
an object is created.
Where to define
constructor?
Generally constructor will be defined under
public section, which can be available to non
members also.
But it can also be defined under private or
protected.
A private or protected constructor is not available to
the non-member functions.
i.e. With a private or protected constructor, you cannot
create an object of the same class in a non-member
function.
Types of constructors
There are three types of
constructors
a) Non-parameterized or Default
Constructor
b)Constructors with default
parameters
c) Parameterized Constructor
d)Copy Constructors
Default constructor:
A constructor that accepts no parameter is
called the default constructor.
With a default constructor, objects are
created just the same way as variables of
other data types are created.
If a class has no explicit constructor
defined, the compiler will supply a default
constructor.
This implicitly declared default constructor
is an inline public member of its class.
When are default constructors
used?
Should be used when you want to
create objects without having to type
the initial objects every time with pre
specified initial values
Should be used if you want to create
array of objects of your class type:
You cant create an array of objects
unless your class has a default
constructor (implicitly or explicitly
defined).
class student{
private:
char name[50];
char gender;
int age;
float marks;
public:
student();
};
student:: student()
{
gets(name,Donald);
sex=M;
age=34;
marks=90;
}
main(){
student s; //default constructor invoked
}
Constructor with default arguments