📜  C++ 中的对象数组及示例

📅  最后修改于: 2022-05-13 01:54:22.439000             🧑  作者: Mango

C++ 中的对象数组及示例

C/C++或任何编程语言中数组存储在连续内存位置的类似数据项的集合,并且可以使用数组的索引随机访问元素。它们可用于存储原始数据类型的集合,例如任何特定类型的 int、float、double、char 等。此外,C/C++ 中的数组可以存储派生数据类型,例如结构、指针等。下面给出的是数组的图片表示。

例子:
让我们考虑一个从用户那里获取随机整数的例子。

数组分配

大批

对象数组

定义类时,只定义对象的规范;没有分配内存或存储空间。要使用类中定义的数据和访问功能,您需要创建对象。

句法:

ClassName ObjectName[number of objects];

对象数组存储对象。类类型的数组也称为对象数组。

示例#1:
存储多个员工数据。假设有一个对象数组用于存储员工数据 emp[50]。

对象数组

下面是用于存储一位员工数据的 C++ 程序:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
class Employee
{
  int id;
  char name[30];
  public:
  void getdata();//Declaration of function
  void putdata();//Declaration of function
};
void Employee::getdata(){//Defining of function
  cout<<"Enter Id : ";
  cin>>id;
  cout<<"Enter Name : ";
  cin>>name;
}
void Employee::putdata(){//Defining of function
  cout<


C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
class Employee
{
  int id;
  char name[30];
  public:
   
  // Declaration of function
  void getdata();
   
  // Declaration of function
  void putdata();
};
 
// Defining the function outside
// the class
void Employee::getdata()
{
  cout << "Enter Id : ";
  cin >> id;
  cout << "Enter Name : ";
  cin >> name;
}
 
// Defining the function outside
// the class
void Employee::putdata()
{
  cout << id << " ";
  cout << name << " ";
  cout << endl;
}
 
// Driver code
int main()
{
  // This is an array of objects having
  // maximum limit of 30 Employees
  Employee emp[30];
  int n, i;
  cout << "Enter Number of Employees - ";
  cin >> n;
   
  // Accessing the function
  for(i = 0; i < n; i++)
    emp[i].getdata();
   
  cout << "Employee Data - " << endl;
   
  // Accessing the function
  for(i = 0; i < n; i++)
    emp[i].putdata();
}


C++
// C++ program to implement
// the above approach
#include
using namespace std;
class item
{
  char name[30];
  int price;
  public:
  void getitem();
  void printitem();
};
  
// Function to get item details
void item::getitem()
{
  cout << "Item Name = ";
  cin >> name;
  cout << "Price = ";
  cin >> price;   
}
 
// Function to print item
// details
void item ::printitem()
{
  cout << "Name : " << name <<
          "\n";
  cout << "Price : " << price <<
          "\n";
}
 
const int size = 3;
 
// Driver code
int main()
{
  item t[size];
  for(int i = 0; i < size; i++)
  {
    cout << "Item  : " <<
            (i + 1) << "\n";
    t[i].getitem();
  }
   
  for(int i = 0; i < size; i++)
  {
    cout << "Item Details : " <<
             (i + 1) << "\n";
    t[i].printitem();
  }
}


让我们理解上面的例子——

  • 在上面的示例中,正在考虑一个名为 Employee 的具有 id 和 name 的类。
  • 这两个函数被声明-
    • getdata():接受用户输入的 id 和 name。
    • putdata():在控制台屏幕上显示数据。

该程序只能获取一个员工的数据。如果需要添加多个员工的数据怎么办。对象数组的答案来了。如果需要存储多个员工的数据,则可以使用对象数组。下面是实现上述方法的 C++ 程序——

C++

// C++ program to implement
// the above approach
#include
using namespace std;
 
class Employee
{
  int id;
  char name[30];
  public:
   
  // Declaration of function
  void getdata();
   
  // Declaration of function
  void putdata();
};
 
// Defining the function outside
// the class
void Employee::getdata()
{
  cout << "Enter Id : ";
  cin >> id;
  cout << "Enter Name : ";
  cin >> name;
}
 
// Defining the function outside
// the class
void Employee::putdata()
{
  cout << id << " ";
  cout << name << " ";
  cout << endl;
}
 
// Driver code
int main()
{
  // This is an array of objects having
  // maximum limit of 30 Employees
  Employee emp[30];
  int n, i;
  cout << "Enter Number of Employees - ";
  cin >> n;
   
  // Accessing the function
  for(i = 0; i < n; i++)
    emp[i].getdata();
   
  cout << "Employee Data - " << endl;
   
  // Accessing the function
  for(i = 0; i < n; i++)
    emp[i].putdata();
}

输出:

解释:
在此示例中,可以存储多个具有员工 ID 和姓名的员工详细信息。

  • Employee emp[30] – 这是一个对象数组,最大限制为 30 个员工。
  • 正在使用两个 for 循环 -
    • 第一个通过调用 emp[i].getdata()函数从用户那里获取输入。
    • 第二个是通过调用函数emp[i].putdata() 函数来打印 Employee 的数据。

示例#2:

C++

// C++ program to implement
// the above approach
#include
using namespace std;
class item
{
  char name[30];
  int price;
  public:
  void getitem();
  void printitem();
};
  
// Function to get item details
void item::getitem()
{
  cout << "Item Name = ";
  cin >> name;
  cout << "Price = ";
  cin >> price;   
}
 
// Function to print item
// details
void item ::printitem()
{
  cout << "Name : " << name <<
          "\n";
  cout << "Price : " << price <<
          "\n";
}
 
const int size = 3;
 
// Driver code
int main()
{
  item t[size];
  for(int i = 0; i < size; i++)
  {
    cout << "Item  : " <<
            (i + 1) << "\n";
    t[i].getitem();
  }
   
  for(int i = 0; i < size; i++)
  {
    cout << "Item Details : " <<
             (i + 1) << "\n";
    t[i].printitem();
  }
}

输出:

输出#2

对象数组的优点:

  1. 对象数组表示以单个名称存储多个对象。
  2. 在对象数组中,可以使用索引号随机访问数据。
  3. 通过将数据存储在单个变量中来减少时间和内存。