Classes and objects are the foundation of Object-Oriented Programming (OOP) in C++. They help organize code, improve reusability, and make programs easier to manage. For example, if Car is a class, then BMW, Audi, or Tesla can be objects created from that class.
Class and Object in C++
What is class in OOPs?
A class contains variables and functions together; a class helps to make a program organised, reusable and easier to understand. An object is a single instance of a class. You can create many objects of the class. For example, if you have a Car class, then you do not require separate code for every car; you only have to create an object. The same class can be used multiple times. An object is just like a mirror, which means if you stand behind 5 mirrors, then your image will be created 5 times. In the same manner, if you call a class 5 times using an object, then the one-class program can be used 5 times.
Syntax:
class class_name
{
access-specifier:
variable and functions
access-specifier:
variable and functions
....
....
}
object-list;What is Object?
An object is a fundamental unit of object-orientated programming. In simple words, an object is an instance of a class; it encapsulates variables and functions together. An object can be created during program execution. For example, if you have a class βCarβ and inside the car class you have a variable and function. Object can help to access the variable and function from the class. One other benefit is that if you want to create multiple objects of the same class, it is possible, and every object can behave or store information separately.
Scope rules for different members of the class:
A class in C++ has a group of variables (data) and functions which are called data members and member functions. This variable and function depend on how those members are declared within a class. There are three access modifiers used for declaring variables or functions inside the class, namely public, private and protected.
Public access modifier:
The public modifier can be accessible everywhere inside and outside the class.
Syntax:
class Car {
public:
int speed; // public data member
void drive() { // public member function
speed = 100; // accessible inside the class
cout << "Driving at " << speed << " km/h";
}
};
int main() {
Car myCar;
myCar.speed = 80; // accessible outside the class
myCar.drive(); // accessible outside the class
}private access modifier:
The private modifier makes the variable and function private, which can be accessible inside the class only.
class Car {
private:
int speed; // private data member
public:
void setSpeed(int s) { // public function can access private member
speed = s;
}
void showSpeed() {
cout << "Speed: " << speed;
}
};
int main() {
Car myCar;
// myCar.speed = 100; Error: speed is private
myCar.setSpeed(100); // Allowed through public function
myCar.showSpeed(); // Allowed
}protected access modifier:
The protected modifiers are accessible inside the class and in the derived classes.
class Vehicle {
protected:
int speed; // protected member
public:
void setSpeed(int s) {
speed = s;
}
};
class Car : public Vehicle {
public:
void showSpeed() {
cout << "Speed: " << speed; // Accessible in derived class
}
};
int main() {
Car myCar;
// myCar.speed = 100; Error: speed is protected
myCar.setSpeed(100); // Allowed through public function
myCar.showSpeed(); // Allowed in derived class
}Passing parameters
C++ uses two different methods for passing parameters: call by value and pass by reference. When an object is passed as an argument to a function, a copy of that object is made.
- Pass by Value: When we pass a copy of the variableβs value to a function. The function works on the copy, so changes inside the function do not affect the original variable.
- Pass by Reference: When we pass the address of the variable (using &) to a function. The function works directly on the original variable, so changes inside the function do affect the original.
#include <iostream>
using namespace std;
// Pass by Value
void byValue(int x) {
x = 10; // changes only local copy
}
// Pass by Reference
void byReference(int &y) {
y = 10; // changes original variable
}
int main() {
int a = 5, b = 5;
byValue(a);
cout << "After byValue: " << a << endl; // Output: 5 (unchanged)
byReference(b);
cout << "After byReference: " << b << endl; // Output: 10 (changed)
return 0;
}Arrays of Objects in C++
An array is a collection of variables which have the same name and the same data type. An array of objects is a collection of objects which can be stored in contiguous memory locations. Array helps to manage multiple objects with similar properties and behaviours.
Syntax:
classname obj[dimension];
Simple array of objects program
#include <iostream>
using namespace std;
class Student {
int roll;
public:
void setData(int r) {
roll = r;
}
void showData() {
cout << "Roll No: " << roll << endl;
}
};
int main() {
Student s[3]; // array of 3 Student objects
// Assign roll numbers
s[0].setData(101);
s[1].setData(102);
s[2].setData(103);
// Display roll numbers
s[0].showData();
s[1].showData();
s[2].showData();
return 0;
}Classes vs. Structures
A structure is a class whose members are by default public. The structures are defined using the same syntax as classes, except that the keyword struct is used instead of class.
| Class | Structure |
|---|---|
| Members are private by default | Members are public by default |
| Supports encapsulation (data hiding with private/protected members) | Less emphasis on encapsulation (since members are public by default) |
| Classes can be used as base classes for inheritance | Structures can also be inherited from, but less commonly used |
| Can contain functions (methods) | Can also contain functions (same as class) |
#include <iostream>
using namespace std;
struct Student {
int roll;
char name[20];
};
int main() {
Student s1; // create a structure variable
// Assign values directly (members are public by default)
s1.roll = 101;
strcpy(s1.name, "Anurag");
// Display values
cout << "Roll No: " << s1.roll << endl;
cout << "Name: " << s1.name << endl;
return 0;
}Nested Classes
If the class created inside the class is known as a nested class. The name of such a nested class is local to the nesting class.
#include <iostream>
using namespace std;
class Outer {
int data1;
public:
void setData1(int x) {
data1 = x;
}
void showData1() {
cout << "Outer class data: " << data1 << endl;
}
class Inner {
int data2;
public:
void setData2(int y) {
data2 = y;
}
void showData2() {
cout << "Inner class data: " << data2 << endl;
}
};
};
int main() {
Outer obj1;
obj1.setData1(10);
obj1.showData1();
Outer::Inner obj2;
obj2.setData2(20);
obj2.showData2();
return 0;
}Disclaimer: We have provide you with the accurate handout of βClass and Object in C++β. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.
Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.