Read Data From File Into Object Array C++

An array in C/C++ or exist it in any programming linguistic communication is a collection of similar data items stored at contiguous retentivity locations and elements can be accessed randomly using indices of an assortment.  They can be used to store the collection of archaic data types such as int, float, double, char, etc of whatsoever item type. To add together to it, an assortment in C/C++ can shop derived data types such equally structures, pointers, etc. Given below is the picture representation of an array.

Instance:
Let'due south consider an example of taking random integers from the user.

Array allocation

Array

Array of Objects

When a course is defined, only the specification for the object is divers; no retentiveness or storage is allocated. To use the data and access functions defined in the class, you demand to create objects.

Syntax:

ClassName ObjectName[number of objects];

The Array of Objects stores objects. An assortment of a class type is also known every bit an assortment of objects.

Case#one:
Storing more than one Employee data. Let's assume at that place is an assortment of objects for storing employee information emp[50].

Array of objects

Beneath is the C++ program for storing data of one Employee:

C++

#include<iostream>

using namespace std;

class Employee

{

int id;

char name[thirty];

public :

void getdata();

void putdata();

};

void Employee::getdata(){

cout<< "Enter Id : " ;

cin>>id;

cout<< "Enter Proper name : " ;

cin>>proper noun;

}

void Employee::putdata(){

cout<<id<< " " ;

cout<<name<< " " ;

cout<<endl;

}

int primary(){

Employee emp;

emp.getdata();

emp.putdata();

return 0;

}

Allow'south sympathize the to a higher place instance –

  • In the in a higher place example, a form named Employee with id and name is beingness considered.
  • The ii functions are alleged-
    • getdata(): Taking user input for id and name.
    • putdata(): Showing the data on the console screen.

This programme tin can take the data of only i Employee. What if in that location is a requirement to add data of more than than one Employee. Here comes the reply Array of Objects. An assortment of objects can be used if there is a demand to store data of more than one employee. Beneath is the C++ program to implement the above approach-

C++

#include<iostream>

using namespace std;

form Employee

{

int id;

char name[xxx];

public :

void getdata();

void putdata();

};

void Employee::getdata()

{

cout << "Enter Id : " ;

cin >> id;

cout << "Enter Proper name : " ;

cin >> name;

}

void Employee::putdata()

{

cout << id << " " ;

cout << proper noun << " " ;

cout << endl;

}

int principal()

{

Employee emp[xxx];

int northward, i;

cout << "Enter Number of Employees - " ;

cin >> n;

for (i = 0; i < north; i++)

emp[i].getdata();

cout << "Employee Data - " << endl;

for (i = 0; i < n; i++)

emp[i].putdata();

}

Output:

Explanation:
In this example, more than ane Employee's details with an Employee id and name can be stored.

  • Employee emp[30] – This is an array of objects having a maximum limit of 30 Employees.
  • Two for loops are being used-
    • Starting time one to take the input from user by calling emp[i].getdata() function.
    • Second one to print the data of Employee by calling the role emp[i].putdata() part.

Example#2:

C++

#include<iostream>

using namespace std;

grade item

{

char name[30];

int price;

public :

void getitem();

void printitem();

};

void item::getitem()

{

cout << "Item Name = " ;

cin >> name;

cout << "Price = " ;

cin >> price;

}

void item ::printitem()

{

cout << "Name : " << proper noun <<

"\n" ;

cout << "Price : " << price <<

"\north" ;

}

const int size = iii;

int main()

{

item t[size];

for ( int i = 0; i < size; i++)

{

cout << "Item  : " <<

(i + ane) << "\n" ;

t[i].getitem();

}

for ( int i = 0; i < size; i++)

{

cout << "Detail Details : " <<

(i + 1) << "\due north" ;

t[i].printitem();

}

}

Output:

Output#2

Advantages of Assortment of Objects:

  1. The array of objects represent storing multiple objects in a single proper noun.
  2. In an array of objects, the information tin can be accessed randomly by using the index number.
  3. Reduce the time and memory past storing the data in a unmarried variable.

Desire to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Training Form .


sullivanpooked91.blogspot.com

Source: https://www.geeksforgeeks.org/array-of-objects-in-c-with-examples/

0 Response to "Read Data From File Into Object Array C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel