C++: Simple Classes

Simple Classes

In this video, we introduce the concept of classes. Classes form the basis of Object-Oriented Programming (OOP) and we will build on the simple class type that we used in this lesson in subsequent lessons.

The video introduces three classes for storing musical data: CNote, CMelody, and CSong, which represent a note, melody, and song respectively. With these, we built up a series of three progressively more sophisticated classes. We also created a CHighScore class which we can use later to create a high score table for games, similar to how we put notes together to create the melody class.

We can create a class to represent paintings. This class contains the artist’s name, the painting’s name and the year it was painted. If the class were more elaborate, we might include the image as well other information about the painting. Here we just use these three simple items and assign them the values for the painting shown here. This painting is entitled Napoleon I on His Imperial Throne and was painted by Jean-Auguste-Dominique Ingres in 1806.

This code sample shows the class definition before the main() function. In the main function, we declare and initialize an instance of the painting class. Then we use the dot operator to access data member in our painting object and send them to the console window.

We can define classes to represent anything. Below, we show how we might define a class to represent a hero character in a Role-Playing Game (RPG). We have a name and a few attributes like strength, dexterity, and intelligence, which typically correspond to fighters, thieves, and mage character classes respectively. Of course, we have to include hit points too in order to let us know when the character dies in battle.