C++中对象数组的问题
#include <iostream>
#include <string>
using namespace std;
const int size=20;
class plorg
{
private:
char name[size];
int CI;
public:
void setCI(int c);
void get();
void showplorg() const;
plorg();
plorg(char *na,int c=20);
};
plorg::plorg()
{
strcpy_s(name,"Plorg");
name[size]='\0';
CI=0;
}
plorg::plorg(char *na,int c)
{
strncpy_s(name,na,size);
name[size]='\0';
CI=c;
}
void plorg::setCI(int c)
{
CI=c;
}
void plorg::showplorg() const
{
cout<<"plorg's name: "<<name<<" and CI is "<<CI<<endl;
}
int main()
{
plorg *plorgs[3];
char temp[size];
int c;
for(int i=0;i<3;i++)
{
cout<<"Enter plorg's name: ";
cin.getline(temp,size);
cout<<"Ener plorg's CI: ";
cin>>c;
plorgs[i] = new plorg(temp,c);
plorgs[i]->showplorg();
}
}
而在运行时,第二次输入却不能输入名字,为什么呢?
#include <string>
using namespace std;
const int size=20;
class plorg
{
private:
char name[size];
int CI;
public:
void setCI(int c);
void get();
void showplorg() const;
plorg();
plorg(char *na,int c=20);
};
plorg::plorg()
{
strcpy_s(name,"Plorg");
name[size]='\0';
CI=0;
}
plorg::plorg(char *na,int c)
{
strncpy_s(name,na,size);
name[size]='\0';
CI=c;
}
void plorg::setCI(int c)
{
CI=c;
}
void plorg::showplorg() const
{
cout<<"plorg's name: "<<name<<" and CI is "<<CI<<endl;
}
int main()
{
plorg *plorgs[3];
char temp[size];
int c;
for(int i=0;i<3;i++)
{
cout<<"Enter plorg's name: ";
cin.getline(temp,size);
cout<<"Ener plorg's CI: ";
cin>>c;
plorgs[i] = new plorg(temp,c);
plorgs[i]->showplorg();
}
}
而在运行时,第二次输入却不能输入名字,为什么呢?
...全文
相关推荐
Cpp Primer Plus第6版源码
Cpp Primer Plus第6版源码
Cpp_practice
cpp_practice Stephen Prata,Maeng Chul-Gyu翻译C ++ Basics Plus 6th Edition示例代码实践库。
python调用c++传递数组的实例
今天小编就为大家分享一篇python调用c++传递数组的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
leetcode刷题班-algorithms-cpp:算法-cpp
leetcode 刷题班算法-cpp C++ 通用数组类 - 创建以刷新概念 C++ 通用堆栈分配数组 用LeetCode学习C++练习
cpp_ex1_a
cpp_ex1_a


