电话目录:划分代码/制作菜单



我正在制作电话簿。现在,正如有人所说,将代码分成小部分是更好的代码。我想把它做成类似的东西,但问题是我无法访问从主函数到类的成员函数或传统函数的对象。我不知道如何将对象(对象数组(作为参数(传递给函数(。

case 2:
{
print_header("Update Menu");
print_update_menu();
break;

这是我的代码:-

case 2:
Print_Header("Update Menu");
while (1)
{
cout<<"Please enter a Name or ID to Update:"<<endl;
cin>>search_ID_name;
for(int j=0 ; j<=records-1 ; j++)
{
if((search_ID_name==Telephone_directory[j].ID) || (search_ID_name==Telephone_directory[j].Name) )
{
Telephone_directory[j].Phone_Directory_data_display();
cout<<"Do you want to update this record? [1/0]"<<endl;
cin>>choice;
if(choice==1)
{
Telephone_directory[j].Phone_Directory_data_input();
cout<<"Record successfully updated"<<endl;
Telephone_directory[j].Phone_Directory_data_display();
}
else if (choice==0)
{
cout<<"Record not updated"<<endl;
break;
}
}
}
cout<<"Want to update another record? [y/n]"<<endl;
cin>>choice_y_n;
if(choice_y_n=='y')
{
cout<<"Redirecting to Update Menu again"<<endl;
}
else if (choice_y_n=='n')
break;
}
break;

在C++中,有两种方法可以将对象传递给函数。

  • 按值传递(副本(
  • 传递指针(对内存位置的引用(

其中,您通常通过引用传递对象。

如果您想将类型为A的对象传递给名为f的函数,您可以这样做:

void f(A& a) {
// You can access a's members here i.e if a has a method called b you do a.b();
}

最新更新