Class and object
Class and object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
In other words, object is an entity that has state and behavior. Here, state means data and behavior means
functionality.
Object is an instance of a class. All the members of the class can be accessed through object.
Let's see an example to create object of student class using s1 as the reference variable.
In this example, Student is the type and s1 is the reference variable that refers to the instance of Student
class.
C++ Class
In C++, class is a group of similar objects. It is a template from which objects are created. It can have
fields, methods, constructors etc.
Let's see an example of C++ class that has three fields only.
1. class Student
2. {
3. public:
7. }
Let's see an example of class that has two fields: id and name. It creates instance of the class, initializes
the object and prints the object value.
1. #include <iostream>
3. class Student {
4. public:
7. };
8. int main() {
12. cout<<s1.id<<endl;
13. cout<<s1.name<<endl;
14. return 0;
15. }
Output:
201
Sonoo Jaiswal
Let's see another example of C++ class where we are initializing and displaying object through method.
1. #include <iostream>
3. class Student {
4. public:
8. {
9. id = i;
10. name = n;
11. }
13. {
15. }
16. };
22. s1.display();
23. s2.display();
24. return 0;
25. }
Output:
201 Sonoo
202 Nakul
Let's see another example of C++ class where we are storing and displaying employee information using
method. #include <iostream>
2. class Employee {
3. public:
4. int id;//data member (also instance variable)
6. float salary;
8. {
9. id = i;
10. name = n;
11. salary = s;
12. }
14. {
16. }
17. };
23. e1.display();
24. e2.display();
25. return 0;
26. }
Output:
We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors,
doors, windows, etc. Based on these descriptions we build the house. House is the object.
Create a Class
A class is defined in C++ using keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
class className {
// some data
// some functions
};
For example,
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
double calculateVolume(){
};
Here, we defined a class named Room.
The variables length, breadth, and height declared inside the class are known as data members. And, the
functions calculateArea() and calculateVolume() are known as member functions of a class.
C++ Objects
When a class is defined, only the specification for the object is defined; no memory or storage is
allocated.
To use the data and access functions defined in the class, we need to create objects.
className objectVariableName;
We can create objects of Room class (defined in the above example) as follows:
// sample function
void sampleFunction() {
// create objects
int main(){
// create objects
Here, two objects room1 and room2 of the Room class are created in sampleFunction(). Similarly, the
objects room3 and room4 are created in main().
As we can see, we can create objects of a class in any function of the program. We can also create objects
of a class within the class itself, or in other classes.
We can access the data members and member functions of a class by using a . (dot) operator. For
example,
room2.calculateArea();
This will call the calculateArea() function inside the Room class for object room2.
room1.length = 5.5;
#include <iostream>
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
}
double calculateVolume() {
};
int main() {
Room room1;
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
return 0;