Intro to Classes and Methods
Classes will not fully be described here but other sections in the Data Structures chapter will be better understood. This will be fully explained in a future chapter.
Introduction to Classes
In C++, a class is a user-defined data type that groups variables (known as attributes) and functions (called methods) under a single name. It serves as a blueprint for creating objects, which are instances of the class.
Think of a class like a template for a cookie-cutter: you can create many cookies (objects) with the same shape (attributes) and actions (methods).
Attributes
Attributes are variables defined within a class. They represent the properties or actions of an object created from the class. For example, if we have a Car
class, some possible attributes could be color
, brand
, and speed
.
Methods
Methods are functions defined within a class. They usually represent actions that can be performed by objects created from the class. Continuing with our Car
example, some possible methods could be accelerate()
, brake()
, and changeGear()
.