繁体   English   中英

将对象传递给方法C ++

[英]Passing an object to a method C++

对不起语法,不是母语。

所以我有一个任务是创建一个简单的程序,你应该能够创建三个人,传递他们的名字,国家,职业和电话号码。 您应该能够将保存的信息打印为电子表格。

所以我想出了这样一段代码:

#include <iostream>
#include <iomanip>

using namespace std;

class Person {
public:
    string surname;
    string name;
    string country;
    string occupation;
    string phone;

    // Set default value
    Person() {
        surname = "empty";
        name = "empty";
        country = "empty";
        occupation = "empty";
        phone = "empty";
    }

    // SET PERSON'S DATA
    void set_surname(string entered_surname) {
        surname = entered_surname;
    }

    void set_name(string entered_name) {
        name = entered_name;
    }

    void set_country(string entered_country) {
        country = entered_country;
    }

    void set_occupation(string entered_occupation) {
        occupation = entered_occupation;
    }

    void set_phone(string entered_phone) {
        phone = entered_phone;
    }

    // RETURN PERSONS DATA
    string get_surname() {
        return surname;
    }
    string get_name() {
        return name;
    }
    string get_country() {
        return country;
    }
    string get_occupation() {
        return occupation;
    }
    string get_phone() {
        return phone;
    }

};

void create_a_frankenstein(Person person) {
    string entered_data;
    cout << "Please, enter person's surname: \n";
    cin >> entered_data;
    person.set_surname(entered_data);

    cout << "Please, enter person's name: \n";
    cin >> entered_data;
    person.set_name(entered_data);

    cout << "Please, enter person's country: \n";
    cin >> entered_data;
    person.set_country(entered_data);

    cout << "Please, enter person's occupation: \n";
    cin >> entered_data;
    person.set_occupation(entered_data);

    cout << "Please, enter person's phone: \n";
    cin >> entered_data;
    person.set_phone(entered_data);
}

int main() {

    Person fst;
    Person snd;
    Person trd;
    Person group[3] = {fst, snd, trd};

    int people_created = 0;

    bool switch_on = true;

    while (switch_on) {
        cout << "What operation would you like to perform: \n";
        cout << "    1) Create new person \n";
        cout << "    2) Print out all of the available information \n";
        cout << "    3) Quit \n";


        //Get the number of operation to perform
        int operation;
        cout << "Please, enter a number: \n";
        cin >> operation;

        switch (operation) {
        //Option 1: create a person
        case 1:
            if (people_created == 3) {
                cout << "It is not possible to create more that three people";
            }

        else {
                create_a_frankenstein(group[people_created]);
                people_created++;
            }
            break;

        //Option 2: print out all of the available information
        case 2:
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_surname();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_name();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_country();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_occupation();
            for (int i = 0; i < 3; i++) cout << setw(20) << setfill(' ') << left << group[i].get_phone();
            break;

        // Option 3: quit
        case 3:
            switch_on = false;
            break;
        }
    }

}

一切似乎都很好。 除了它不会改变对象变量中的信息。

我的猜测是,当我将Person类型对象传递给create_a_frankenstein()时,方法会创建一个对象的副本,并开始使用副本而不更改原始对象中的任何内容。

我试过用指针。 我设法在简单的例子上做我想做的事情:

void first(int* a){
    for (int i = 0; i < 7; i++) {
        a[i] = a[i]+1;
    }
}

int main() {
    int a[7] = {0, 1, 2, 3, 4, 5, 6};
    for (int i=0; i<7; i++) {
        cout << a[i] << ' ';
    }
}

但是当我尝试在实验室中使用它时,它并不容易。

很高兴收到任何关于如何解决问题的建议以及我应该刷新或深入了解的主题。提前谢谢!

尝试通过引用传递Person对象。 你可以在这里找到更多: 在C ++中通过引用传递对象 在您的代码示例中,您没有调用名为“first”的函数。

问题在于void create_a_frankenstein(Person person)方法。

您正在传递Person对象的副本。 如果要保持对对象所做的更改将其作为引用传递: void create_a_frankenstein(Person& person)

注意:

  1. 不要使用数组。 如果要存储对象序列,请使用std::vector

  2. 如果将任何getter member function定义为const - > return_type getter_name(params) const { //body here}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM