Learning C++: Introduction To Classes

An example "Hello World" in C++

In this blog post I am going to take a look at creating classes in C++. This forms part of my learning C++ series of blog posts. A class is a type blueprint, which defines the characteristics and behaviours of all instances of the class.

To create a class, the class keyword is used followed by the name to call the class and then the code to define the class. For example:

class Rectangle
{
 public:
    int _width;
    int _height;
};

This defines a class called Rectangle, then uses public: to make the class available throughout the program and defines that any Rectangle objects created can contain two integer variables called _width and _height.

To use this Rectangle class to create an object requires that the class name is called (Rectangle) and the name to give the object e.g. to create a rectangle called firstRectangle:

Rectangle firstRectangle;

Once the firstRectangle object is created its _width and _height can be defined:

firstRectangle._width = 4;
firstRectangle._height = 6;

An example of this in a program is:

#include <iostream>

using namespace std;

class Rectangle

{

public:

    int _width;

    int _height;

};

int main()

{

  Rectangle firstRectangle;

  

  firstRectangle._width = 4;

  firstRectangle._height = 6;

  cout << firstRectangle._width << endl;

  cout << firstRectangle._height << endl;

  return 0;

}

The program defines the class Rectangle and then defines the object firstRectangle of the class, Rectangle with _width and _height defined. It then outputs the values of _width and _height.

An example of a C++ class being created
An example of a C++ class being created

Note: When creating the object it is recommended to initialise the values, even if that means initialising them to 0 (zero).

The object’s values can also be initialised on the same line as creating the object:

Rectangle secondRectangle{8,12};

Within a program this may look like:

#include <iostream>

using namespace std;

class Rectangle

{

public:

    int _width;

    int _height;

};

int main()

{

  Rectangle firstRectangle;

  Rectangle secondRectangle{8,12}; 

  firstRectangle._width = 4;

  firstRectangle._height = 6;

  cout << "first rectangle" << endl;

  cout << firstRectangle._width << endl;

  cout << firstRectangle._height << endl;

  

  cout << "second rectangle" << endl;

  cout << secondRectangle._width << endl;

  cout << secondRectangle._height << endl;

  return 0;

}
A C++ class object being created and initialised on one line
A C++ class object being created and initialised on one line

One thought on “Learning C++: Introduction To Classes

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.