C++: An instance of an object

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

It is easy for us to understand that an object is an entity.

When it comes to an object-oriented program, the emphasis now is on the object.  In a procedural programming such as PASCAL or Fortran, we would exchange data using variables.

Since an object is an entity, can we pass an object from one object to another?

Well, can can do that using an instance of the object.  Let’s consider the example below.

#include <iostream>
#include <string>

using namespace std;

class Country

	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";

	Country ts1;

	ts1 = ts;

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

	return 0;


In the example above, we have used the first instance ts to hold the first set of data.  ts is an object in itself but in OOP, we would call it an instance of the class Country.

Now after we have created another instance of the Class Country ts1, we are able to use an assignment operator = to assign an object from ts to ts1.

Notice when we print out the data members of ts1, it has the same set of data as ts.instance

Series Navigation<< C++ Keywords
C++: Constructor and data members initialization >>