Lesson 1
Understanding Classes - Definition, Creation, and Usage
Lesson Introduction

Hello! Today, we're diving into an exciting topic: Classes in C++. This lesson aims to introduce you to the concept of classes, how to create them, and how to use them in real-life scenarios. By the end of this lesson, you'll understand the definition of classes, how to create a class in C++, and how to use this class in your code. Ready? Let's get started!

Definition of Classes

So, what exactly is a class? In simple terms, a class is like a blueprint or template for creating objects. Just like blueprints describe the structure and behavior of a house, a class describes the attributes (data members) and behaviors (member functions) of objects. To put this in a real-life context, think of a "Car" as a class. It can have attributes like color, brand, and speed. The class is not some specific car, it is rather a description of what is the car, what information we know about the car.

C++
1#include <string> 2 3class Car { 4public: 5 // Attributes 6 std::string color; 7 std::string brand; 8 int speed; 9};
Components of a Class

Classes in C++ are made up of attributes and methods. Attributes store information about the object, while methods define what actions the object can perform. We will learn about methods in the next lesson.

Here's our Car class example:

Attributes:

  • string color - the car's color
  • string brand - the car's brand
  • int speed - the car's current speed

Understanding these components is crucial, as it forms the basis of creating and using classes.

Creating a Class

Creating a class in C++ is straightforward. Start with the class keyword, followed by the class name, and then its attributes and methods inside curly braces. Here's our example once again:

C++
1#include <iostream> 2#include <string> 3 4class Car { 5public: 6 std::string color; 7 std::string brand; 8 int speed; 9};

Note how we define car attributes the same way we define regular variables, but inside the class. Now this variables are the parts of the class.

Pay attention to two things:

  1. The class declaration always ends with ;
  2. We start the class with public: keyword. It is the access modifier, and we will discuss its meaning and other options in one of the following lessons. By now, always use public:.
Using a Class

Once you've created a class, you can create objects from that class. Objects are instances of the class and can have unique values for the attributes defined by the class. In our example, myCar is an object of the Car class. We can set its attributes like color, brand, and speed.

C++
1int main() { 2 Car myCar; // Creating an object of class Car 3 myCar.color = "Red"; 4 myCar.brand = "Toyota"; 5 myCar.speed = 0; 6 7 // Working with class attributes 8 myCar.speed += 10; // Increase speed 9 std::cout << "Speed: " << myCar.speed << std::endl; // Print out the speed 10}

We can work with class attributes just like we do with regular variables. The only difference is that the class attributes are a part of an object. In the example above, we increased the speed of myCar by 10 and then printed it out using cout.

Creating Multiple Objects

Creating multiple objects from the same class is straightforward. Each object can have its unique values for the attributes defined in the class.

Let's extend our Car example by creating two different Car objects:

C++
1int main() { 2 Car myCar; // First object of class Car 3 myCar.color = "Red"; 4 myCar.brand = "Toyota"; 5 myCar.speed = 0; 6 7 Car yourCar; // Second object of class Car 8 yourCar.color = "Blue"; 9 yourCar.brand = "Honda"; 10 yourCar.speed = 20; 11 12 // Working with class attributes 13 myCar.speed += 10; // Increase speed of myCar 14 yourCar.speed += 30; // Increase speed of yourCar 15 16 std::cout << "MyCar - Brand: " << myCar.brand << ", Color: " << myCar.color << ", Speed: " << myCar.speed << std::endl; 17 std::cout << "YourCar - Brand: " << yourCar.brand << ", Color: " << yourCar.color << ", Speed: " << yourCar.speed << std::endl; 18}

In this example, myCar and yourCar are two different objects created from the Car class. Each object has its attributes set to different values. We can interact with these objects independently.

Running the code would result in:

1MyCar - Brand: Toyota, Color: Red, Speed: 10 2YourCar - Brand: Honda, Color: Blue, Speed: 50

This demonstrates how classes allow us to create multiple objects with unique attributes, enabling flexible and scalable code design.

Lesson Summary

In this lesson, we covered the basics of classes in C++. We learned that classes are blueprints for creating objects, and they consist of attributes and methods. We created a simple Car class, discussed how to instantiate objects from it.

Now, it's time to put this theory into practice. Let's move on to the practice section, where you'll get hands-on experience in creating and using classes in C++.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.