How to create classes and objects in C++

Objects and classes are the building blocks that allow us to model real-world entities in code. A class is a template, and objects are the actual things made from it. For example, consider a "Car" in the real world. Each car has attributes like color, model, and engine capacity, and behaviors like driving and braking. In programming, we use classes to define these attributes and behaviors, and objects represent individual cars, such as "Red Tesla Model S."

Why do we need classes and objects?

Classes and objects help:

  • They allow us to translate real-world scenarios into code.

  • Once a class is created, we can reuse it to create multiple objects.

  • By grouping related data and methods, classes make programs more readable and maintainable.

What is a class in C++?

A class in C++ is a user-defined data type that encapsulates data membersVariables defined inside a class that hold data specific to objects (e.g., title, author). (variables) and member functionsMethods defined inside a class that operate on data members (e.g., displayDetails()). (methods). It serves as a template to create objects. Think of a class as a template for a library—it defines what a library should look like, but it’s not a library itself.

Now that we know what a class is, let’s see how to define one in C++.

Syntax of class in C++

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Imagine you're designing a library system. You’ll need a way to represent each book with its title, author, and ISBN. That’s where classes come in!

Let's write down a complete code in example below:

Code example in C++

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

What is an object in C++?

An object is an instance of a class. While a class defines the structure and behavior, the object represents a specific entity based on that structure.

Syntax of an object in C++

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

For example:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

What are data members and member functions?

  • Data members: Variables defined inside a class that hold data specific to objects (e.g., title, author).

  • Member functions: Methods defined inside a class that operate on data members (e.g., displayDetails()).

What are access specifiers in class?

Access specifiers control how data members and member functions of a class can be accessed. Let's explore one by one:

  • Public: Members declared as public are accessible from outside the class.

  • Private: Members declared as private can only be accessed within the class itself. By default, class members are private in C++.

  • Protected: Members declared as protected are accessible within the class and its derived classes.

Example:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

What are constructors in C++?

Constructors are special member functions that run automatically when an object is created. They are used to initialize an object's data members.

Types of constructors:

  1. Default Constructor: A constructor that does not take any arguments. It initializes the object with default values.

  2. Parameterized Constructor: A constructor that takes arguments to initialize the object with specific values.

Example

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Let’s revisit the "Book" class with an additional feature to borrow a book.

Code example

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Try it: Modify the Book class to include a returnBook() function on line 30 that marks the book as available again. Create an object and test this functionality.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Key takeaways

  • Classes and objects are foundational concepts in OOP, allowing you to model real-world scenarios in code.

  • A class is a blueprint, while an object is an instance of that blueprint.

  • Data members store attributes, and member functions define behavior.

  • Syntax for creating classes and objects is straightforward but powerful for organizing and managing code.

  • Practicing with real-world examples like the "Book" class helps solidify understanding and application of these concepts.

Start your programming journey with our Learn C++ Course. This course offers step-by-step guidance, hands-on examples, and practical exercises to help you build a strong foundation in C++. Looking to go from beginner to expert? Explore the Become a C++ Programmer Path. This skill path covers everything from the basics of C++ to advanced programming concepts, with the skills needed to excel in real-world projects.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources