Ad space (banner)
Lesson 11 / 20

Classes (Object-Oriented Programming)

This lesson covers the basics of writing a class in C++. It's for anyone searching "C++ class usage."

A class bundles data (member variables) and logic (member functions) together. Use public: to specify which members can be used from outside the class.

The example defines class Animal, with a member variable name and a member function, speak(), that returns a sound.

A common early mistake is forgetting to write public:. In C++, a class's members are private by default, so you need to explicitly mark anything you want to use from outside as public:.

In real projects, classes are used to represent data that bundles state and behavior together โ€” a game character, or an element in a simulation.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)