🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #87

C++ Classes/Objects


C++ Classes/Objects

C++ is an object-oriented programming language.

Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members".

A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.


Create a Class

To create a class, use the class keyword:

Example

class MyClass {
// The class
  public:
// Access specifier
    int myNum;        //
Attribute (int variable)
    string myString;  //
Attribute (string variable)
};

Note: Example explained The class keyword is used to create a class called MyClass. The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class. You will learn more about access specifiers later. Inside the class, there is an integer variable myNum and a string variable myString. When variables are declared within a class, they are called attributes. At last, end the class definition with a semicolon ;.

What's the difference between a Class and a Structure?

The example above looks much like a structure (struct).

  • Structures are much simpler than objects. Unlike objects, structures cannot do encapsulation, inheritance or polymorphism, which you will learn more about in the next chapters. If all you need is a collection of variables, a structure is easier to use than an object.


Create an Object

In C++, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects.

To create an object of MyClass, specify the class name, followed by the object name.

To access the class attributes (myNum and myString), use the dot syntax (.) on the object:

Example

class MyClass {       // The class
  public:
// Access specifier
    int myNum;        //
Attribute (int variable)
    string myString;  //
Attribute (string variable)
};

int main() {
  MyClass myObj;
// Create an object of MyClass

  // Access attributes and set values
  myObj.myNum
= 15;

myObj.myString = "Some text";

  // Print attribute values

cout << myObj.myNum << "\n";
  cout << myObj.myString;
  return 0;
}

Multiple Objects

You can create multiple objects of one class:

Example

// Create a Car class with some attributes
class Car {
  public:
    string brand;
    string model;
    int
year;
};

int main() {
  // Create an object of Car
  Car carObj1;
  carObj1.brand = "BMW";

carObj1.model = "X5";
  carObj1.year = 1999;

// Create another object of Car
  Car
carObj2;
  carObj2.brand = "Ford";
  carObj2.model =
"Mustang";
  carObj2.year = 1969;

  // Print
attribute values
  cout << carObj1.brand
<< " " << carObj1.model << " " << carObj1.year << "\n";
  cout <<
carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
  return 0;
}

Challenge Task

Create a class called Book with the following attributes:

  • title (string)
  • author (string)
  • year (int)

Then create two objects of the class and print their attribute values.

Expected Output

Matilda, Roald Dahl, 1988
The Giving Tree, Shel Silverstein, 1964
#include <iostream>
#include <string>
using namespace std;

class Book {
  public:
    string title;
    string author;
    int year;
};

int main() {
  Book book1;
  book1.title = "Matilda";
  book1.author = "Roald Dahl";
  book1.year = 1988;

  Book book2;
  book2.title = "The Giving Tree";
  book2.author = "Shel
Silverstein";
  book2.year = 1964;

  cout << book1.title << ", " << book1.author << ", " << book1.year << "\n";
  cout << book2.title << ", " << book2.author << ", " << book2.year;
  return 0;
}

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C++ Classes/Objects – FAQs

Quick answers about learning C++ Classes/Objects in C++.

This free note from CodingNow 2.0 explains C++ Classes/Objects in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including C++ Classes/Objects, is 100% free with no signup required.
With focused practice, most students grasp C++ Classes/Objects in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now