Code About Us Tutorial


  Basic Classes
    What is a Class?
    Class Functions
    Creating Class Objects
    Constructors
    Destructors


   
 

Home > Basic Classes > Constructors

November 28, 2009 10:05 pm

   

Constructors

Now we are going to discuss class constructors and destructors. A constructor is a member function that is called on automatically once an object is declared. The object constructor and destructor are always there they are just hidden. By default the constructor and destructor take no parameters but you can change that, and I’ll show you how. The class’s destructor is called upon when an object is destroyed. Here is the syntax of a constructor:

 

 
ClassesName(){};

Its very simple, say you have a class called Employee. The Employee’s constructor would look like this:

 

 
Employee(){};

In this constructor you can treat it as if it was any regular function. I’ll give you an example.

 

 
class Employee
{
public:
	Employee(int age,int years);		//constructor
	void setAge(int age){ itsAge = age;}
	int getAge() { return itsAge;}
	void setYears(int years) { itsYears = years;}
	int getYears() { return itsYears;}
private:
	int itsAge;
	int itsYears;
};

Employee::Employee(int age, int years) { itsAge = age; itsYears = years; cout <<"Constructor calledn"; }

In the above syntax I have highlighted the constructor declaration so you can see where I added it in the body. I just wrote the name of the class which was Employee then I added two parameters to the constructor, int age and int years. I used this code:

 

 
Employee(int age,int years);

This creates the prototype, and now I have to define the constructor. The other functions are already defined because I have a body on them, but for the constructor I need to initialize the variables. The following code defines the constructor:

 

 
Employee::Employee(int age, int years)
{
	itsAge = age;
	itsYears = years;
	cout <<"Constructor calledn";
}

To define a function in a class, first you write the classes Name followed by two colons, then the function name, for instance:

If you did not want to write the function int getAge() { return itsAge;} all on one line you could have written it on a separate line, directly underneath the class,

 

 
Int Employee::getAge()
{
return itsAge;
}

This would have the same effect as the function above, I prefer to write it all on one line if there is not to much to write on a single line. Another reason I like writing it all on one line is because it makes the code more compact.

Let us return to explaining how constructors actually work. In the above code, to create an employee object and automatically set its Age and its years working in the business, all you would have to do is the following:

 

 
Employee Cory(16,4);

This would create an object of the Employee class. It would also set itsAge to 16 and itsYears working in the business to 4.

Here is a full program that expresses the idea.

 

 
//Employee.h

class Employee { public: Employee(int age,int years); void setAge(int age){ itsAge = age;} int getAge() { return itsAge;} void setYears(int years) { itsYears = years;} int getYears() { return itsYears;} private: int itsAge; int itsYears; };

Employee::Employee(int age, int years) { itsAge = age; itsYears = years; cout <<"Constructor calledn"; }

The above file is the file that just stores the class I created.

 

 
//Employee.cpp
#include <iostream.h>
#include "Employee.h"

int main() { cout <<"Creating Employee....nn"; Employee Cory(16,4); cout <<"Getting Employee's agenn"; cout <<"Employee Age: " << Cory.getAge() << "nn";

cout <<"Getting Employee's working year....nn"; cout <<"Years Working: " << Cory.getYears() << endl;

return 0; }

You can create multiple objects and each object will be created with the assigned parameters. If I wanted to make another employee and change the age and the years working I would write the following:

 

 
Employee Momad(18,7);

This has created another Employee named Momad, and his age is set to 18 and his years working would be set to 7. Please Rate this Code:


    Comments for: Constructors
Annonymouse User says:
This is too lame :(

A.D [ad@hotmail.com] Posted: 7 times. says:
it was good but alternate constructor has been used without mentioning.

A.D [ad@hotmail.com] Posted: 7 times. says:
it was good but alternate constructor has been used without mentioning.




Add Comment:
Name:
Email:


 «