c++ - 我如何使用带有类的数组

标签 c++ arrays class

#include <iostream> 
#include <string>
#include <math.h> 
#include <fstream>
using namespace std;

class Storage
{
public:
Storage();
string information[10][7];
void SetInformation(string,int);
void GetInformation(int);
};  

Storage::Storage(){cout<<"\nStorage Activated\n";}

void Storage::SetInformation(string,int i)
{//input
    i--;
    for (int j=0;j<7;j++)
    {
    switch(j+1)
    {case 1: cout << "\nFirst Name: "; break;
    case 2: cout << "\nLast Name: "; break;
    case 3: cout << "\nAge: "; break;
    case 4: cout << "\nEmail: "; break;
    case 5: cout << "\nDoor Number: "; break;
    case 6: cout << "\nRoad Name: "; break;
    case 7: cout << "\nPost Code: "; break;
    default:;}      
    cin >> information[i][j];}
    }

void Storage::GetInformation(int i){
// output
    i--;
    for (int j=0;j<7;j++)
    {   
    switch(j+1)
    {case 1: cout << "\nFirst Name: "; break;
    case 2: cout << "\nLast Name: "; break;
    case 3: cout << "\nAge: "; break;
    case 4: cout << "\nEmail: "; break;
    case 5: cout << "\nDoor Number: "; break;
    case 6: cout << "\nRoad Name: "; break;
    case 7: cout << "\nPost Code: "; break;
    default:;}      

    cout << information[i][j];}
}

int main()
{
    int x;
    Storage();
    Storage Someone;
    cin >> x;
    Someone.SetInformation(int);

return 0;
}

好的,这就是我现在所做的。但是我现在在激活类(class)时遇到问题?我的意思是我不能使用该类或其方法?我是否以正确的方式创建了类?

最佳答案

刚和成员们一起上课firstName , lastName等。叫它Person管他呢。然后用 std::vector<Person> 替换你的数组.

创建从标准输入中读取特定信息的成员方法(可能是一个 enum 告诉方法要读取哪些信息)。

替换 cout最后用一种方法显示每个对象的信息。

关于c++ - 我如何使用带有类的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14075388/

相关文章:

Javascript 将字符串变量作为数组访问

javascript - Angular js : how to use ngMousedown ngMouseup

c++ - 信号处理程序和多线程

c++ - 将二维数组传输到 csv 文件?

java - java中无法动态增加数组的大小

python - 在 Kivy.kv 文件中如何引用另一个类中的方法

c++ - 在头类构造函数中使用嵌套类

c++ - 使用 Linux 命令行 g++ 构建 Crypto++ 5.6.5 问题

c++ - C++ 中 "::delete"的用途是什么?

c - 我想对c中的字符数组进行排序