C++ 将对象传递给方法C++;
对不起,语法不好,不是以英语为母语的人 所以我有一个任务,创建一个简单的程序,你应该能够创建三个人,输入他们的姓名、国家、职业和电话号码。您应该能够将保存的信息打印为电子表格 所以我想出了这样一段代码:C++ 将对象传递给方法C++;,c++,C++,对不起,语法不好,不是以英语为母语的人 所以我有一个任务,创建一个简单的程序,你应该能够创建三个人,输入他们的姓名、国家、职业和电话号码。您应该能够将保存的信息打印为电子表格 所以我想出了这样一段代码: #include <iostream> #include <iomanip> using namespace std; class Person { public: string surname; string name; string cou
#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;
}
}
}
#包括
#包括
使用名称空间std;
班主任{
公众:
串姓;
字符串名;
弦国;
字符串占用;
字符串电话;
//设置默认值
人(){
姓氏=“空”;
name=“empty”;
country=“空”;
职业=“空”;
phone=“空”;
}
//设置个人数据
无效集\姓氏(输入字符串\姓氏){
姓氏=输入的姓氏;
}
无效集合名称(输入的字符串名称){
名称=输入的名称;
}
无效集合\国家(字符串输入\国家){
国家=输入的国家/地区;
}
无效集合\占用(字符串输入\占用){
职业=输入的职业;
}
无效设置\u电话(输入字符串\u电话){
电话=输入的电话;
}
//返回人员数据
字符串get_姓氏(){
返回姓氏;
}
字符串get_name(){
返回名称;
}
字符串get_country(){
返回国;
}
字符串get_职业(){
返回职业;
}
字符串get_phone(){
回电话;
}
};
无效创建科学怪人(个人){
输入数据的字符串;
cout>输入的_数据;
person.set_姓氏(输入_数据);
cout>输入的_数据;
人员.设置单位名称(输入单位数据);
cout>输入的_数据;
个人。设置国家(输入的国家数据);
cout>输入的_数据;
人员.设置职业(输入的职业数据);
cout>输入的_数据;
person.set_phone(输入的_数据);
}
int main(){
个人fst;
个人snd;
个人trd;
个人组[3]={fst,snd,trd};
int people_created=0;
bool开关打开=真;
while(打开){
问题出在void create\u a\u frankenstein(Person)
方法上
您正在传递此人对象的副本。如果要保留对对象所做的更改,请将其作为引用传递:void create\u a\u frankenstein(Person&Person)
注:
不要使用数组。如果要存储对象序列,请使用std::vector
如果将任何getter成员函数定义为const
-->返回类型getter\u name(params)const{//body here}
尝试通过引用传递Person对象。您可以在此处找到更多信息:。顺便说一句,在您的代码示例中,您没有调用名为“first”的函数。您可能需要查看引用。如果我理解正确,它们会完全按照您的要求执行。a[I]=a[I]++< /COD>无效,调用未定义的行为。无论如何,它将被执行。您的函数值一个人,意味着您得到调用方所拥有的任何一个副本。然后您更改您的副本,而不触及函数之外的任何东西。使用引用如杰罗姆所说的C++中更好的样式,但指针也将是WOR。k、 或者,让create_a_frankenstein
实际返回一个Person
并完全放弃争论。@JeromeReinländer,谢谢!这正是我需要的。谢谢你的回答。链接非常有用!谢谢你的回答。这正是我需要的:)如果你不介意,你能扩展你的范围吗r回答并告诉我为什么对对象使用向量更好?@VanityAloe its因为a)向量为您管理对象的生命周期,也就是说,follow RAII(尽管您的情况更简单,但我们很清楚。b)实际上,您正在存储6个对象。其中3个是fst、snd、thrd和组中的3个。也就是说,Person group[3]={fst、snd、trd};
在数组中创建对象的副本。
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] << ' ';
}
}