How to Create Multiple Objects for a class In C++


We do not manipulate the data directly. We can do so only by using functions and objectsImagine you want to build an application to keep track of all the books. The data has to be considered in OOP Here, our data is all about books. Therefore, the first object type is a book.
Now, we have designed the object in such a way that all the data can be stored in a state for any further manipulation. In C++, the design of an object is done by creating a class. For programmers, a class is similar to (a blueprint of a building) to an architect. In case, if you give those books to others then, the person also becomes an object. Therefore, you create a person class. The tracking app can then create a new object of a person class and fill it with data about your friend.

C++ Program to Create Multiple Objects for a class

#include<iostream> using namespace std; class Summer{ int x; public: void read(); void print(); }; void Summer :: read(){ cout<<"Enter any integer value "<<endl; cin>>x; } void Summer :: print(){ cout<<"The value is :"<<x<<endl; } int main(){ Summer s1,s2,s3; s1.read(); s2.read(); s3.read(); s1.print(); s2.print(); s3.print(); return 0; }

Output:-

Enter any integer value 234
Enter any integer value 456
Enter any integer value 789
The value is :234
The value is :456
The value is :789

Explanation of the program

In this program, the read() and print() member functions are shared by all the objects of the type sample. Whereas in the case of data members, each object i.e. s1, s2, and s3 has its own copy.