It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




7年前关闭。




我在类中编写了一个简单的对象数组程序。
谁能告诉我我在做什么错
#include<iostream>
#include<iomanip>
using namespace std;
class employee
    {
    int name;
    public:
        void getdata(void);
        void putdata(void);
    };
void employee :: getdata(void)
        {
        cout<<"enter name:";
        cin>>name;
        }
void employee :: putdata(void)
        {
        cout<<"Name:"<<name<<endl;
        }
int main()
{   int i;
    employee manager[4];
    for(i=0;i<3;i++)
      {
        manager[i].getdata();
        }
    for(i=0;i<3;i++)
      {
         manager[i].putdata();
        }
return 0;
}

输出:
enter name:naveen
enter name:enter name:Name:0
Name:0
Name:134515033
naveen@naveen-Ex

最佳答案

您有int name;,当您使用cin naveen命名时失败,因此名称未初始化。

更改:
int name;

 #include <string>
 std::string name;

关于c++ - 类中的对象数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14457521/

10-11 18:01