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

	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;

	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;


Series Navigation<< C++: A simple experiment on Constructor/Destructor
C++: Methods of a class >>

Leave a comment

Leave a Reply