Constructors

Published:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

CONSTRUCTORS:

“When a class object is created, its members can be initialized by that class' constructor function”. (Dietal, 2000)

When the object is created which is an instance of a class, the data members will have random values, the special member function which initializes those data members when the object is created are called constructors and it is the only way to initialize the member variables during object creation.

Constructors are automatically called when an object is created. They may have zero or more parameters. The Constructor with no parameters is known as default constructor also it can be defined as the Constructor with parameters which has default values. Also if user defined constructors are not present (i.e) if the explicit constructors are not present then the compiler will provide the default constructor but it does nothing.

Syntax:

Normal constructor:

Classname(arguments)

{

…..

}

Default constructor:

* Classname()

{

….

}

* Classname(Arguments with default values)

{

…..

}

SIMPLE EXAMPLE:

#include<iostream>

#include<string.h> // c string functions

using namespace std;

class Employee

{

public:

Employee(const char *n,int s) // Normal constructor

{

strcpy(name,n);

salary=s;

}

Employee() // Default constructor - no parameters

{

strcpy(name,"john");

salary=20000;

}

~Employee(){} //destructor

void print()

{

cout<<"Name :"<<name<<endl;

cout<<"Salary:"<<salary<<"pounds"<<endl;

}

private:

char name[30];

int salary;

};

void main()

{

Employee A("Robert",10000);

A.print();

Employee B;

B.print();

system("pause");

}

In this example the class Employee contains one normal constructor, one default constructor, one destructor(which has been explained in next topic) and one normal function. The name of the class and that of constructor should be same so the constructor name also ‘Employee'.

Where ‘Employee(const char *n,int s)' is the normal constructor (i.e) user defined constructor , it contains two arguments which are ‘const char *n' to initialize employee's name for an object of the class Employee and ‘int s' to initialize employee's salary for the same. In that program the name and salary for an employee(object) A has been initialized using this user defined constructor.

Where ‘Employee()' is the default constructor because it contains no arguments. In that program the name and salary for an employee(object) B has been initialized using this default constructor. And ‘void print()' is the function which just prints the name and salary.

* Constructor never returns a value(i.e) it has no return type and also no void. Let us see this through an example,

#include<iostream>

using namespace std;

class cricket

{

public:

cricket()

{

return wickets;

}

void display()

{

wickets = 10;

cout<<"total number of wickets :"<<wickets<<endl;

}

private:

int wickets;

};

void main()

{

cricket c();

cricket d;

d.display();

}

O/P error: cricket: constructor cannot return a value

In this example the default constructor ‘cricket()' returns integer value ‘wickets' so it results error.

* Multiple constructors can be present in one class because according to their parameters the objects of a class will access them automatically. Let us see this through an example,

#include<iostream>

using namespace std;

class cricket

{

public:

cricket(int wkt) // first constructor

{

wickets = wkt;

cout<<"Total number of wickets :"<<wickets<<endl;

}

cricket(const char *pl) //second constructor

{

strcpy(players,pl);

cout<<"Total number of players :"<<players<<endl;

}

private:

int wickets;

char players[10];

};

void main()

{

cricket c(10);

cricket d("Eleven");

}

O/P: Total number of wickets: 10

Total number of players: Eleven

In this example there are two constructor which are ‘cricket(int wkt)' and ‘cricket(const char *pl)' and these are having different types of parameters so the objects ‘c' & ‘d' properly accessed them.

* Constructor can be declared in public or private section but it must be declared in public not in private section because it cannot be directly accessed by main function if it was declared in private section(but we can access it using friend functions).We can declare it in private section only if it is used internally by the class. Let us see this through an example

#include<iostream>

using namespace std;

class cricket

{

public:

cricket(const char *pl)

{

strcpy(players,pl);

cout<<"Total number of players :"<<players<<endl;

}

private:

cricket(int wkt)

{

wickets = wkt;

cout<<"Total number of wickets :"<<wickets<<endl;

}

int wickets;

char players[5];

};

void main()

{

cricket c(10);

cricket d("ten");

}

O/P error: 'cricket::cricket' : cannot access private member declared in class 'cricket'

In this example, the constructor cricket(int wkt) is declared in private section so main function cant access it.

* Constructor can be declared in inside or outside the class definition. Let us see this through an example

#include<iostream>

using namespace std;

class cricket

{

public:

cricket(int wkt);

cricket(const char *pl) // constructor inside the class

{

strcpy(players,pl);

cout<<"Total number of players :"<<players<<endl;

}

private:

int wickets;

char players[5];

};

cricket::cricket(int wkt)//constructor outside the class

{

wickets = wkt;

cout<<"Total number of wickets :"<<wickets<<endl;

}

void main()

{

cricket c(10);

cricket d("ten");

}

* Constructor cant be a friend function

Writing Services

Essay Writing
Service

Find out how the very best essay writing service can help you accomplish more and achieve higher marks today.

Assignment Writing Service

From complicated assignments to tricky tasks, our experts can tackle virtually any question thrown at them.

Dissertation Writing Service

A dissertation (also known as a thesis or research project) is probably the most important piece of work for any student! From full dissertations to individual chapters, we’re on hand to support you.

Coursework Writing Service

Our expert qualified writers can help you get your coursework right first time, every time.

Dissertation Proposal Service

The first step to completing a dissertation is to create a proposal that talks about what you wish to do. Our experts can design suitable methodologies - perfect to help you get started with a dissertation.

Report Writing
Service

Reports for any audience. Perfectly structured, professionally written, and tailored to suit your exact requirements.

Essay Skeleton Answer Service

If you’re just looking for some help to get started on an essay, our outline service provides you with a perfect essay plan.

Marking & Proofreading Service

Not sure if your work is hitting the mark? Struggling to get feedback from your lecturer? Our premium marking service was created just for you - get the feedback you deserve now.

Exam Revision
Service

Exams can be one of the most stressful experiences you’ll ever have! Revision is key, and we’re here to help. With custom created revision notes and exam answers, you’ll never feel underprepared again.