[英]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)
注意:
不要使用數組。 如果要存儲對象序列,請使用std::vector
。
如果將任何getter member function
定義為const
- > return_type getter_name(params) const { //body here}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.