如何在结构的成员函数中使用结构成员?

时间:2018-11-12 22:02:47

标签: c++ struct parameters arguments

因此,该程序的目的是创建一个由3个人组成的数组,允许用户在for循环中填充数据,确保结果大写并输出结果。 这些新的项目说明是为了 1.重写capitalize()作为结构内的一种方法。 2.将printPerson()重写为结构中的方法

该程序本身运行良好,只是不符合我的教授想要的格式。他说,这样做的目的是不使用任何论据,但是我也不知道他的意思。几个月前,我才刚开始编程,尽管我一直在尝试,但我对术语并不了解。

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Person {
    string firstName;
    string middleName;
    string lastName;
    int age;
    string gender;

    void capitalize(Person &arg);
    void printPerson(Person arg);
};

很确定这些是这里的方法,但是我不确定(person&arg)和(person arg)是否为参数本身,或者它们是否为参数。我以为那是“ arg”部分,但我找不到一种没有它们就可以运行程序的方法,因为我很确定我需要&运算符来修改信息。

int main(void) {
   Person myPerson;
   Person a[3];
   const int size = 5;

   for (int i = 0; i <= 2; i++) {
       cout << "What is First Name #" << i + 1 << "? ";
       getline(cin, a[i].firstName);
       cout << "What is Middle Name #" << i + 1 << "? ";
       getline(cin, a[i].middleName);
       cout << "What is Last Name #" << i + 1 << "? ";
       getline(cin, a[i].lastName);
       cout << "Age #" << i + 1 << "? ";
       cin >> a[i].age;
       cin.ignore();
       cout << "Male or Female #" << i + 1 << "? ";
       getline(cin, a[i].gender);
       cout << endl;
    }

    for (int i = 0; i <= 2; i++) {
        myPerson.capitalize(a[i]);
        cout << "PERSON #" << i + 1 << endl;
        cout << "~~~~~~~~~~~~~~~" << endl;
        myPerson.printPerson(a[i]);
    }

    system("pause");
    return 0;
}

此外,我不知道如何在没有“参数/参数”(此时不确定是否有差异)或没有“ arg”的情况下操纵函数工作

void Person::capitalize(Person &arg) {
    transform(arg.firstName.begin(), arg.firstName.end(), arg.firstName.begin(), ::toupper);
    transform(arg.middleName.begin(), arg.middleName.end(), arg.middleName.begin(), ::toupper);
    transform(arg.lastName.begin(), arg.lastName.end(), arg.lastName.begin(), ::toupper);
}

void Person::printPerson(Person arg) {
    cout << "\nFirst Name: " << arg.firstName << endl;
    cout << "\nMiddle Name: " << arg.middleName << endl;
    cout << "\nLast Name: " << arg.lastName << endl;
    cout << "\nAge: " << arg.age << endl;
    cout << "\nGender: " << arg.gender << endl;
    cout << "\n\n";
}

1 个答案:

答案 0 :(得分:0)

capitalizeprintPerson现在是结构Person的成员(通常称为方法)。这意味着它们对Person实例的成员变量进行操作。这样,您可以只访问这些方法中的所有类成员。请参阅以下代码。我还用构造函数完成了它,并使它更具可读性。

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Person {
public:
    Person();
    void readFromUserInput();
    void capitalize();
    void print();
public:
    string firstName;
    string middleName;
    string lastName;
    int age;
    string gender;
};

Person::Person() :
    firstName(""),
    middleName(""),
    lastName(""),
    age(0),
    gender("")
{

}

void Person::readFromUserInput()
{
    cout << "What is the First Name ? ";
    getline(cin, firstName);

    cout << "What is Middle Name ? ";
    getline(cin, middleName);

    cout << "What is Last Name ? ";
    getline(cin, lastName);

    cout << "Age ? ";
    cin >> age;
    cin.ignore();

    cout << "Male or Female ? ";
    getline(cin, gender);
}

void Person::capitalize() 
{
    transform(firstName.begin(), firstName.end(), firstName.begin(), ::toupper);
    transform(middleName.begin(), middleName.end(), middleName.begin(), ::toupper);
    transform(lastName.begin(), lastName.end(), lastName.begin(), ::toupper);
}

void Person::print() 
{
    cout << "\nFirst Name: " << firstName << endl;
    cout << "\nMiddle Name: " << middleName << endl;
    cout << "\nLast Name: " << lastName << endl;
    cout << "\nAge: " << age << endl;
    cout << "\nGender: " << gender << endl;
    cout << "\n\n";
}

int main(void)
{
    const int NUM_PERSONS = 3;
    Person a[NUM_PERSONS];

    for (int i = 0; i < NUM_PERSONS; i++)
    {
        cout << "### " << (i + 1) << ". User:" << endl;
        a[i].readFromUserInput();
        cout << endl;
    }

    for (int i = 0; i < NUM_PERSONS; i++) 
    {
        a[i].capitalize();
        cout << "PERSON #" << i + 1 << endl;
        cout << "~~~~~~~~~~~~~~~" << endl;
        a[i].print();
    }

    system("pause");
    return 0;
}