Chapter 2: Variables and Object-Oriented Basics
In C++, variables function similarly to C, but with a major upgrade: the ability to handle complex, user-defined types called Classes. While you will still use fundamental types like int, double, and char, you will soon realize that these are just the starting point for building sophisticated software architectures.
1. Enhanced Data Types
C++ introduces the std::string type. Unlike the character arrays in C, std::string is a class that manages its own memory, meaning you don't have to worry about the length of your text. It makes string manipulation, like concatenation and searching, safer and more intuitive.
std::string greeting = "Hello";
std::string name = "User";
std::string full = greeting + ", " + name; // Concatenation is easy!
2. The Concept of Classes
Object-Oriented Programming (OOP) is about modeling real-world objects. A Class is a blueprint for an object. It bundles data (variables) and functions (methods) into a single unit. For example, if you were building a game, you might create a Player class that holds health, speed, and location data, along with functions to move or attack.
class Player {
public:
int health = 100;
void attack() {
std::cout << "Player attacks!" << std::endl;
}
};
3. Objects and Access Modifiers
Once you define a class, you create an Object, which is an "instance" of that class. You can create as many players as you need. Notice the public: keyword—this is an access modifier. It tells C++ that these members can be accessed from outside the class. If you use private:, only the class itself can touch that data, which is a key security feature in OOP.
4. Why OOP?
OOP promotes three main pillars: Encapsulation (hiding internal data), Inheritance (creating new classes based on existing ones), and Polymorphism (the ability for one function to act differently depending on the object). These concepts prevent your code from becoming an unmanageable mess as your projects scale up.
Common Beginner Mistakes
- Forgetting Semicolons after Classes: Unlike functions, a class definition must end with a semicolon (
};). Forgetting this causes confusing compiler errors. - Overusing Public Data: A common mistake is making all variables public. As a rule of thumb, keep data private and provide public functions to interact with it.
- Confusing the Dot Operator: You use the dot operator (
.) to access an object's variables and methods (e.g.,myPlayer.attack();). Forgetting it is a standard syntax error.
Try It Yourself
- Create a class named
Carwith a public variablespeed. - Create an object of type
Car, set its speed, and print it to the console. - Try adding a method to your
Carclass calledaccelerate()that increases the speed variable by 10.
Introducing Classes
A class is a blueprint for creating objects that bundle data (attributes) and behavior (methods) together — this is the foundation of object-oriented programming.
class Car {
public:
string brand;
void honk() {
cout << brand << " says beep!" << endl;
}
};
int main() {
Car myCar;
myCar.brand = "Toyota";
myCar.honk(); // Toyota says beep!
}
Objects created from a class are like variables that carry their own data and functions, letting you model real-world things directly in code.
CodeMaster Academy