0% found this document useful (0 votes)
6 views

Class and object

In C++, an object is a real-world entity with state (data) and behavior (functionality), created at runtime as an instance of a class. A class serves as a blueprint for creating objects and can contain fields, methods, and constructors. Examples illustrate how to define classes, create objects, and access their data members and methods in C++.

Uploaded by

yoxop87048
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Class and object

In C++, an object is a real-world entity with state (data) and behavior (functionality), created at runtime as an instance of a class. A class serves as a blueprint for creating objects and can contain fields, methods, and constructors. Examples illustrate how to define classes, create objects, and access their data members and methods in C++.

Uploaded by

yoxop87048
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ 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 a runtime entity, it is created at runtime.

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.

1. Student s1; //creating an object of Student

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:

4. int id; //field or data member

5. float salary; //field or data member

6. String name;//field or data member

7. }

C++ Object and Class Example

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>

2. using namespace std;

3. class Student {
4. public:

5. int id;//data member (also instance variable)

6. string name;//data member(also instance variable)

7. };

8. int main() {

9. Student s1; //creating an object of Student

10. s1.id = 201;

11. s1.name = "Sonoo Jaiswal";

12. cout<<s1.id<<endl;

13. cout<<s1.name<<endl;

14. return 0;

15. }

Output:

201

Sonoo Jaiswal

C++ Class Example: Initialize and Display data through method

Let's see another example of C++ class where we are initializing and displaying object through method.

1. #include <iostream>

2. using namespace std;

3. class Student {

4. public:

5. int id;//data member (also instance variable)

6. string name;//data member(also instance variable)

7. void insert(int i, string n)

8. {
9. id = i;

10. name = n;

11. }

12. void display()

13. {

14. cout<<id<<" "<<name<<endl;

15. }

16. };

17. int main(void) {

18. Student s1; //creating an object of Student

19. Student s2; //creating an object of Student

20. s1.insert(201, "Sonoo");

21. s2.insert(202, "Nakul");

22. s1.display();

23. s2.display();

24. return 0;

25. }

Output:

201 Sonoo

202 Nakul

C++ Class Example: Store and Display Employee Information

Let's see another example of C++ class where we are storing and displaying employee information using
method. #include <iostream>

1. using namespace std;

2. class Employee {

3. public:
4. int id;//data member (also instance variable)

5. string name;//data member(also instance variable)

6. float salary;

7. void insert(int i, string n, float s)

8. {

9. id = i;

10. name = n;

11. salary = s;

12. }

13. void display()

14. {

15. cout<<id<<" "<<name<<" "<<salary<<endl;

16. }

17. };

18. int main(void) {

19. Employee e1; //creating an object of Employee

20. Employee e2; //creating an object of Employee

21. e1.insert(201, "Sonoo",990000);

22. e2.insert(202, "Nakul", 29000);

23. e1.display();

24. e2.display();

25. return 0;

26. }

Output:

201 Sonoo 990000

202 Nakul 29000


C++ Class

A class is a blueprint for the object.

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(){

return length * breadth;

double calculateVolume(){

return length * breadth * height;

};
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.

Syntax to Define Object in C++

className objectVariableName;

We can create objects of Room class (defined in the above example) as follows:

// sample function

void sampleFunction() {

// create objects

Room room1, room2;

int main(){

// create objects

Room room3, room4;

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.

Also, we can create as many objects as we want from a single class.


C++ Access Data Members and Member Functions

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.

Similarly, the data members can be accessed as:

room1.length = 5.5;

In this case, it initializes the length variable of room1 to 5.5.

Example 1: Object and Class in C++ Programming

// Program to illustrate the working of

// objects and class in C++ Programming

#include <iostream>

using namespace std;

// create a class

class Room {

public:

double length;

double breadth;

double height;

double calculateArea() {

return length * breadth;

}
double calculateVolume() {

return length * breadth * height;

};

int main() {

// create object of Room class

Room room1;

// assign values to data members

room1.length = 42.5;

room1.breadth = 30.8;

room1.height = 19.2;

// calculate and display the area and volume of the room

cout << "Area of Room = " << room1.calculateArea() << endl;

cout << "Volume of Room = " << room1.calculateVolume() << endl;

return 0;

You might also like