C++ 如何将数组与类一起使用 #包括 #包括 #包括 #包括 使用名称空间std; 类存储 { 公众: 存储(); 字符串信息[10][7]; void SetInformation(字符串,int); 无效信息(int); }; Storage::Storage(){cout

C++ 如何将数组与类一起使用 #包括 #包括 #包括 #包括 使用名称空间std; 类存储 { 公众: 存储(); 字符串信息[10][7]; void SetInformation(字符串,int); 无效信息(int); }; Storage::Storage(){cout,c++,arrays,class,C++,Arrays,Class,只要有一个包含成员firstName,lastName等的类就可以了。可以叫它Person或其他任何名称。然后用std::vector替换数组 创建从标准输入读取特定信息的成员方法(可能是告诉方法要读取哪些信息的enum) 将末尾的cout替换为显示每个对象信息的方法。简单类和一些刚刚开始的方法: 这比C++更像是一种“C”风格,但理解起来很简单。 如果你知道更多的OOP规则,你可以(也应该)升级它 #include <iostream> #include <string&g

只要有一个包含成员
firstName
lastName
等的类就可以了。可以叫它
Person
或其他任何名称。然后用
std::vector
替换数组

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


将末尾的
cout
替换为显示每个对象信息的方法。

简单类和一些刚刚开始的方法: 这比C++更像是一种“C”风格,但理解起来很简单。 如果你知道更多的OOP规则,你可以(也应该)升级它

#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 Person
{
public:
    Person() { /*init with default values here if needed*/ isValid = false; }
    ~Person() { }

    /* add setters/getters if needed */
public:
    string firstName;
    string lastName;
    int age;
    string email;
    int doorNumber;
    string roadName;
    string postCode;
    bool isValid;   // is the data valid
};
void inputPersonData(个人和个人)
{
cout>person.firstName;
cout>person.lastName;
cout>person.age;
cout>person.email;
cout>person.doorNumber;
cout>person.roadName;
cout>person.postCode;
person.isValid=true;//现在数据是有效的
}
无效输出PersonData(个人和个人)
{
//不打印未加载的人员数据。。。
如果(person.isValid==false)返回;
//使用cout打印。。。
}
//让我们这样做:
//删除人(信息,10,索引至删除)
void deletePerson(Person*peopleArray,int count,int toDelete)
{
peopleArray[toDelete].isValid=false;
}
简单按名称搜索函数,返回索引:

void inputPersonData(Person &person)
{
    cout << "\nFirst Name: ";
    cin >> person.firstName;

    cout << "\nLast Name: ";
    cin >> person.lastName;

    cout << "\nAge: ";
    cin >> person.age;

    cout << "\nEmail: ";
    cin >> person.email;

    cout << "\nDoor Number: ";
    cin >> person.doorNumber;

    cout << "\nRoad Name: ";
    cin >> person.roadName;

    cout << "\nPost Code: ";
    cin >> person.postCode;

    person.isValid = true; // now the data is valid
}

void outputPersonData(Person &person)
{
    // do not print not loaded person data...
    if (person.isValid == false) return;

    // use cout to print...
}

// us it that way:
// deletePerson(information, 10, index_to_delete)
void deletePerson(Person *peopleArray, int count, int toDelete)
{
    peopleArray[toDelete].isValid = false;
}
int searchPersonByName(Person*peopleArray,int count,const string&name)
{
对于(int i=0;i
大体上:

int searchPersonByName(Person *peopleArray, int count, const string &name)
{
    for (int i = 0; i < count; ++i)
    {
        if (peopleArray[i].firstName == name)
            return i;
    }
}
人员信息[10];
//输入
cout>i;
我--;
输入个人数据(信息[i]);
输出个人数据(信息[i]);

创建一个循环,并在该循环中提出问题以填充数据或搜索

如果不激活类,则创建该类的实例

这里我只是指出一些语法错误。你还应该遵循上面其他答案中的建议,为这个人创建一个更合适的类定义

在你们班

Person information[10];

// input
cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
cin >> i;
i--;
inputPersonData(information[i]);
outputPersonData(information[i]);
在这些方法中,格式化块的方法类似于

void Storage::SetInformation(string name, int i)
这将使它更容易阅读

在switch()语句中

如果您想从用户那里获得一些值,请在每次提示“First Name”、“Last Name”等后使用“cin”语句

主要是,

    default: break;
} 

或者
Person*people=newperson[length];
如果您不能使用
std::vector
@evanmdonnal
Person-people[10]“EvnMcDunAn”,你为什么不能够使用<代码> STD::向量< /代码>?如果你可以使用堆,但是不能使用<代码> STD::向量< /代码>为什么使用C++?还有其他语言可能比“残缺C++”更适合这个……如果你不能使用STD::vector,为什么写C++?为什么不是C?“因为一些不真实的教室里的跛脚老师说你不能使用STD::向量(或者任何其他标准容器),但是要求你使用C++。"这可能是一个原因。我们都一次又一次地看到了它。+1,顺便说一句。公共数据成员,免费
inputPersonData
outputPersonData
而不是成员,原始数组……在我看来,这弊大于利。@fen如果我用你的方式创建它,我以后是否可以使用搜索来查找数据?例如,键入一个我可以找到的名称他所有的细节都有一个删除选项?(带排序)还有个人信息[10]数组现在的意义是什么?我还需要数组吗?@MRKHANCOMUTILeader据我所知,你想为一些人建立“数据库”,所以类Person代表单个人和这个数组的信息[10]'表示“数据库”。首先填写数据库,然后需要编写用于搜索和删除的方法。我将使用这些方法更新我的上述答案…非常感谢您的帮助。更多问题将很快出现:D
for ( ... ) {
    switch ( ... ) {

    }
}
default:;}  <--- Error
    default: break;
} 
Storage();  <--- Delete this line
Storage Someone; <--- This is creating an instance of your class.
Someone.SetInformation(int); <--- This is incorrect.
Someone.SetInformation("some text", x);