A class in C++ is a user-defined data type that combines data members and functions into a single unit. An object is an instance of a class. Classes are one of the most important features of Object-Oriented Programming (OOP).
A class is a blueprint or template for creating objects.
For example, a Student class can describe information
such as:
class Student {
};
An object is an instance of a class.
class Student {
};
int main() {
Student student1;
return 0;
}
Here, Student is the class and
student1 is an object of that class.
class ClassName {
// data members
// member functions
};
A class definition ends with a semicolon.
class Student {
public:
std::string name;
int age;
float marks;
};
The public keyword makes these members accessible from
outside the class.
Student student1;
This statement creates an object named student1 from the
Student class.
Multiple objects can also be created:
Student student1;
Student student2;
Student student3;
Variables declared inside a class are called data members or member variables.
class Student {
public:
std::string name;
int age;
float marks;
};
Here, name, age, and marks are
data members.
The dot operator . is used to access public members
through an object.
Student student1;
student1.name = "Amit";
student1.age = 20;
student1.marks = 85.5;
Values can then be displayed using the same dot operator.
std::cout << student1.name;
A function declared inside a class is called a member function.
class Student {
public:
std::string name;
void display() {
std::cout << name;
}
};
Member functions can work with the data members of the same object.
Student student1;
student1.name = "Rahul";
student1.display();
The dot operator is also used to call a member function.
#include <iostream>
#include <string>
class Student {
public:
std::string name;
int age;
void display() {
std::cout << "Name: "
<< name
<< std::endl;
std::cout << "Age: "
<< age;
}
};
int main() {
Student student1;
student1.name = "Amit";
student1.age = 20;
student1.display();
return 0;
}
Members declared after public: can be accessed from
outside the class.
class Student {
public:
int age;
};
int main() {
Student student;
student.age = 20;
std::cout << student.age;
return 0;
}
Members declared after private: cannot be accessed
directly from outside the class.
class Student {
private:
int age;
public:
void setAge(int a) {
age = a;
}
};
A public member function can be used to work with private data.
The protected access specifier allows members to be
accessed inside the class and by derived classes.
class Parent {
protected:
int value;
};
Protected members are commonly discussed when learning inheritance.
| Specifier | Access |
|---|---|
| public | Accessible from outside the class. |
| private | Accessible within the class and related permitted contexts. |
| protected | Accessible within the class and derived classes. |
Members of a C++ class are private by default.
class Student {
int age;
};
Here, age is private because no access specifier was
provided.
To make it public, write:
class Student {
public:
int age;
};
Encapsulation means keeping data and the functions that operate on that data together and controlling access to the data.
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
A setter function is commonly used to assign a value to a private data member.
class Student {
private:
int age;
public:
void setAge(int a) {
age = a;
}
};
The setter provides controlled access to the private member.
A getter function is commonly used to read a private data member.
class Student {
private:
int age;
public:
void setAge(int a) {
age = a;
}
int getAge() {
return age;
}
};
Each object created from a class has its own object state.
class Student {
public:
std::string name;
int age;
};
int main() {
Student student1;
Student student2;
student1.name = "Amit";
student1.age = 20;
student2.name = "Neha";
student2.age = 19;
return 0;
}
Changing student1 does not automatically change
student2.
A constructor is a special member function that is automatically called when an object is created.
class Student {
public:
Student() {
std::cout << "Object created";
}
};
int main() {
Student student;
return 0;
}
The constructor is called when student is created.
class Student {
private:
std::string name;
int age;
public:
Student(
std::string n,
int a
) {
name = n;
age = a;
}
void display() {
std::cout << name
<< " "
<< age;
}
};
int main() {
Student student("Amit", 20);
student.display();
return 0;
}
A member function can be declared const when it should
not modify the object's non-mutable data members.
class Student {
private:
int age;
public:
Student(int a) {
age = a;
}
int getAge() const {
return age;
}
};
The const after the function parameter list is part of
the member function declaration.
An object can be passed to a function just like other values.
class Student {
public:
std::string name;
};
void display(
const Student& student
) {
std::cout << student.name;
}
A const reference avoids an unnecessary copy when the function only needs to read the object.
A pointer can store the address of an object.
class Student {
public:
std::string name;
};
int main() {
Student student;
student.name = "Rahul";
Student* ptr = &student;
std::cout << ptr->name;
return 0;
}
The arrow operator -> is used to access members through
an object pointer.
#include <iostream>
#include <string>
class Employee {
private:
int id;
std::string name;
double salary;
public:
void setData(
int employeeId,
std::string employeeName,
double employeeSalary
) {
id = employeeId;
name = employeeName;
salary = employeeSalary;
}
void display() {
std::cout << "ID: "
<< id
<< std::endl;
std::cout << "Name: "
<< name
<< std::endl;
std::cout << "Salary: "
<< salary;
}
};
int main() {
Employee employee;
employee.setData(
101,
"Rahul",
35000
);
employee.display();
return 0;
}
. with a pointer instead of ->.public: when members need external access.| Class | Object |
|---|---|
| A blueprint or template. | An instance of a class. |
| Defines data and behavior. | Contains actual object state. |
| Used to create objects. | Used to work with the class functionality. |
Example: Student |
Example: student1 |
Student or Employee.Classes are used to model real-world entities and software concepts.
| Concept | Meaning |
|---|---|
| Class | A blueprint or user-defined type containing data and behavior. |
| Object | An instance of a class. |
| Data Member | A variable declared inside a class. |
| Member Function | A function declared inside a class. |
| public | Members accessible from outside the class. |
| private | Members normally accessible only within the class. |
| protected | Members accessible within the class and derived classes. |
| Constructor | A special member function called when an object is created. |
| Dot Operator | Used to access members through an object. |
| Arrow Operator | Used to access members through an object pointer. |
class Student {
private:
std::string name;
int marks;
public:
void setData(
std::string n,
int m
) {
name = n;
marks = m;
}
void display() {
std::cout << name
<< " "
<< marks;
}
};
int main() {
Student student;
student.setData("Amit", 85);
student.display();
return 0;
}
public, private, and protected control member access.Question: What is an object in C++?