Chapter: Programming and Data structures : Object Oriented Programming Fundamentals

Constructor

The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.

CONSTRUCTOR:

 

The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.

 

General Syntax of Constructor

 

A constructor is a special member function that takes the same name as the class name. The default constructor for a class X has the form

 

X::X()

 

In the above example, the arguments are optional. The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the “new” operator.

 

There are several forms in which a constructor can take its shape namely:

 

 

Default Constructor:

 

This constructor has no arguments in it. The default Constructor is also called as the no argument constructor.

 

For example:

 

class Exforsys

 

{

 

private:

 

int a,b;

 

public:

 

Exforsys();

 

...

 

};

 

 

Exforsys :: Exforsys()

 

{

 

a=0;

 

b=0;

 

}

 

 

Parameterized Constructor:

 

A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example:

 

#include <iostream>

class Line

{

 

public:

 

void setLength( double len );

double getLength( void );

Line(double len); // This is the constructor

 

 

private:

 

double length;

 

};

 

 

// Member functions definitions including constructor

Line::Line( double len)

 

{

 

cout << "Object is being created, length = " << len << endl;

length = len;

 

 

}

 

void Line::setLength( double len )

 

{

 

length = len;

 

}

 

double Line::getLength( void )

 

{

 

return length;

 

}

 

// Main function for the program

int main( )

 

{

 

Line line(10.0);

 

// get initially set length.

 

cout << "Length of line : " << line.getLength() <<endl;

 

// set line length again

 

line.setLength(6.0);

 

cout << "Length of line : " << line.getLength() <<endl;

 

 

return 0;

 

}

 

When the above code is compiled and executed, it produces the following result: Object is being created, length = 10

 

Length of line : 10

Length of line : 6

 

 

Copy constructor:

 

This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

 

For example to invoke a copy constructor the programmer writes: Exforsys e3(e2);

 

Or

Exforsys e3=e2;

 

Both the above formats can be sued to invoke a copy constructor.

 

 

For Example:

 

#include <iostream>

using namespace std;

class Exforsys

{

 

private: int a; public:

 

Exforsys()

 

{

 

}

 

Exforsys(int w)

 

{

 

a=w;

 

}

 

Exforsys(Exforsys& e)

 

{

 

a=e.a;

 

cout << " Example of Copy Constructor";

 

}

 

void result()

 

{

 

cout<< a;

 

}

 

};

 

void main()

 

{

 

Exforsys e1(50);

 

Exforsys e3(e1);

 

cout<< "ne3=";e3.result();

 

}

 

Destructors

 

Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory.

 

Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

 

 

General Syntax of Destructors

 

~ classname();

 

The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

 

Some important points about destructors:

·        Destructors take the same name as the class name.

 

·        Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.

 

·        The Destructor does not take any argument which means that destructors cannot be overloaded.

 

·        No return type is specified for destructors.

 

 

For example:

 

class Exforsys

 

{

 

private:

 

...

 

public:

 

Exforsys()

 

{

 

}

 

~ Exforsys()

 

{

 

}

 

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Fundamentals : Constructor |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.