C++: Constructor with parameters

This entry is part 26 of 61 in the series C++

Imaging you work for a record company and you need to retrieve some information about some artistes.

You can use constructor to quickly initialize the data members and print them out.

Take note that parameters are not needed for destructor.

#include <iostream>
#include <string>

using namespace std;

class Rock
{
public:

	string name;
	string track;

	Rock(string, string);  //This is the constructor
	~Rock();  //This is the destructor
};

Rock::Rock(string n2, string t2)
{
	name = n2;
	track = t2;
}

Rock::~Rock()
{
	cout << "This is Rod Stewart Signing Off";
	cout << endl;
}

int main()
{

	string n1 = "Rod Stewart";
	string t1 = "I was only Joking";

	Rock rs(n1,t1); // instance declaration

	cout << "This is " << rs.name << " Singing " << rs.track;
	cout << endl;

	return 0;
}

constructor1

C++: A simple experiment on Constructor/Destructor

This entry is part 25 of 61 in the series C++

We can work out a simple example to show how a constructor and desctructor work.

Destructors destroy the class objects created by constructors. The destructors have the same name as their class, preceded by a tilde (~).

This example does nothing but create the ts instance or object and once the program exits out of the main, the desctructor will be called releasing the ts instance back to the memory.

#include <iostream>
#include <string>

using namespace std;

class Country
{
public:
	Country();  //This is the constructor
	~Country();  //This is the destructor
};

Country::Country()
{
	cout << "This is Taylor Swift Singing.";
	cout << endl;
}

Country::~Country()
{
	cout << "This is Taylor Swift Signing Off.";
	cout << endl;
}

int main()
{
	Country ts; // instance declaration
	return 0;
}

When the ts instance declaration is called in line 27, the constructor function will be called automatically.

destructor

C++: The actual constructor

This entry is part 24 of 61 in the series C++

As shown the previous example, we do not actually need a constructor to initialize member data.

Constructor is created to better manage memory allocated for an object.  There is also a Destructor meant for releasing the object back to the memory stack and we will go through that in the later posts.

An example of a constructor is as follows.  Class Country has three member variables. The declaration of constructor can be done inside the class and definition outside the class.

#include <iostream>
#include <string>

using namespace std;

class Country
{
public:
	string name;
	int noAlbum;
	string latest;

	Country();  //This is the constructor
};

Country::Country()
{
	name = "Taylor Swift";
	noAlbum = 5;
	latest = "1986";
}

int main()
{

	Country ts; // instance declaration
	 
	cout << "Country Singer = " << ts.name << endl;
	cout << "No of Album = " << ts.noAlbum << endl;
	cout << "Her latest album = " << ts.latest << endl;
	cout << endl;

	return 0;
}

Line 16 is the actual function of the constructor.  Notice how it is defined with the scope operator ::

The constructor function is called when the ts instance is declared in in line 26.  This is done implicitly without the need to specify a calling function.

constructor

C++: Constructor and data members initialization

This entry is part 23 of 61 in the series C++

In our example on data member, we have a part that initializes the data members of the class.  This can be done using constructor of a class.

The example is as shown below.  In line 19, we are creating an instance of the class Country. When creating this instance, the constructor will be called so as to initialize the data members.

However, in the example, I actually did not explicitly declare a constructor. The C++ program will, however, lazily create a constructor itself and initialize the data members to null values.

#include <iostream>
#include <string>

using namespace std;

class Country
{

public:
	string name;
	int noAlbum;
	string latest;

};

int main()
{

	Country ts; // instance declaration

	ts.name = "Taylor Swift"; // member initialization
	ts.noAlbum = 5;
	ts.latest = "1986";

	cout << "\n Country Singer = " << ts.name;
	cout << "\n No of Album = " << ts.noAlbum;
	cout << "\n Her latest album = " << ts.latest;
	cout << "\n";

	return 0;
}

We will look at the actual constructor in the next example.