In the previous chapter, we learned about structures. C++ also provides classes, which are one of the most important parts of Object-Oriented Programming (OOP).
A class helps us keep data and functions together.
For example, a Student class can contain a student’s name, age, and a function to display the information.
Let’s begin with C++ Classes and Objects.
What is a Class?
A class is like a blueprint.
Think about a house blueprint. The blueprint tells us how the house will be built, but it is not the actual house.
In the same way, a class tells C++ what an object will contain.
class Student {
public:
string name;
int age;
};
Here, Student is a class.
What is an Object?
An object is a real thing created from a class.
If Student is the blueprint, then student is the object.
Student student;
Let’s see a complete example:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
};
int main() {
Student student;
student.name = "Rahul";
student.age = 14;
cout << "Name: " << student.name << "\n";
cout << "Age: " << student.age;
return 0;
}
Output:
Name: Rahul
Age: 14
Here:
Student → Class
student → Object
name → Data
age → Data
Accessing Class Members
We use the dot (.) operator to access data inside an object.
student.name
student.age
For example:
student.age = 15;
This changes the age of the student object to 15.
Class with a Function
A class can also contain functions.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
void introduce() {
cout << "Hello, my name is " << name;
}
};
int main() {
Student student;
student.name = "Rahul";
student.introduce();
return 0;
}
Output:
Hello, my name is Rahul
Here, introduce() is a function inside the Student class.
This is useful because the data and the function that works with that data can stay together.
Creating Multiple Objects
You can create many objects from the same class.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
};
int main() {
Student student1;
Student student2;
student1.name = "Rahul";
student1.age = 14;
student2.name = "Anjali";
student2.age = 15;
cout << student1.name << " - " << student1.age << "\n";
cout << student2.name << " - " << student2.age;
return 0;
}
Output:
Rahul - 14
Anjali - 15
Both objects belong to the same class, but each object has its own data.
public and private
C++ allows us to control which parts of a class can be accessed from outside.
public
Members under public can be accessed outside the class.
class Student {
public:
string name;
};
So this is allowed:
student.name = "Rahul";
private
Members under private cannot be accessed directly from outside the class.
class Student {
private:
int age;
};
This is not allowed:
student.age = 14;
Instead, we can use a public function to change or read the private value.
Simple Getter and Setter
A function used to set a value is commonly called a setter.
A function used to get a value is commonly called a getter.
#include <iostream>
using namespace std;
class Student {
private:
int age;
public:
void setAge(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Student student;
student.setAge(14);
cout << "Age: " << student.getAge();
return 0;
}
Output:
Age: 14
You don’t need to worry too much about getters and setters yet. We will understand them better when we learn encapsulation.
Class vs Object
The difference is easy to remember:
Class → Blueprint
Object → Something created from the blueprint
For example:
class Car {
public:
string brand;
};
Car car;
Here:
Caris the class.caris the object.
A Simple Example
Let’s create a Car class with data and a function.
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string brand;
void start() {
cout << brand << " is starting.";
}
};
int main() {
Car car;
car.brand = "Toyota";
car.start();
return 0;
}
Output:
Toyota is starting.
Key Points to Remember
- A class is like a blueprint.
- An object is created from a class.
- A class can contain data and functions.
- Use the
.operator to access object members. publicmembers can be accessed from outside the class.privatemembers cannot be accessed directly from outside.- You can create multiple objects from one class.
What’s Next?
In the next chapter, we will learn about C++ Constructors and Destructors and see how C++ can automatically run special functions when objects are created and destroyed.
Frequently Asked Questions (FAQs)
Q1. What are C++ Classes and Objects?
C++ Classes and Objects are core OOP concepts. A class acts as a blueprint, while an object is an instance created from that class.
Q2. What is C++ class?
A C++ class is a blueprint that defines the data and functions that objects created from it can contain.
Q3. How are objects created in C++?
Objects are created by using the class name followed by the object name, such as Student student;.
Q4. What is the difference between a class and an object in C++?
A class defines the structure and behavior, while an object is a real instance of that class.
Q5. What are public and private members in a C++ class?
Public members can be accessed from outside the class, while private members cannot be accessed directly from outside the class.
Q6. What is the difference between a class and an object in C++?
A class is a blueprint that defines data and functions, while an object is an instance of that class created to use those members.
Q7. Can a C++ Class have multiple Objects?
Yes. You can create multiple objects from the same C++ Class, and each object can have its own data.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
