类中的数组,求赐教啊
#include<iostream>#include<string>
using namespace std;
enum place{home,office,fax,cell,pager};
class Phone
{
private:
int cc;
int ac;
int num;
place type;
public:
Phone(int c=0,int a=0, int n=0, place t=(place) 0):cc(c),ac(a),num(n),type(t){}
void set();
void print();
friend void find(Phone &, int );
};
/*Phone :: Phone
{ cc=0;
ac=0;
num=0;
type=(place)0;
}
*/
void Phone:: set()
{
int i;
cout<<"Please enter the phone number (Country code Area code Phone number and Type)"<<endl;
cin>>cc>>ac>>num>>i;
type=(place) i;
}
void Phone:: print()
{
cout<<"Country code"<<'\t'<<"Area code"<<'\t'<<"Phone number"<<'\t'<<"Type"<<endl;
cout<<cc<<'\t'<<ac<<'\t'<<num<<'\t';
switch(type)
{
case 0: cout<<"Home"<<endl;
case 1: cout<<"Office"<<endl;
case 2: cout<<"Fax"<<endl;
case 3: cout<<"Cell"<<endl;
case 4: cout<<"Pager"<<endl;
default:cout<<"Type Wrong"<<endl;
}
}
void find( Phone &p ,int num)
{
int i,j=0;
for(i=0;i<3;i++)
{
if(p[i].num == num)
{
cout<<"Found the same number"<<endl;
p[i].print();
j=1;
break;
}
}
if(!j) cout<<"No phone number match"<<endl;
}
int main()
{
Phone p[3]={Phone(),Phone(),Phone()};
int i,num
for(i=1;i<3;i++)
{
p[i].set();
p[i].print();
}
cout<<"Input the number you want to find"<<endl;
cin>>num;
find(p,num);
return 0;
}
本程序主要的功能是输入电话号码(其中有区号还有类型),可以进行查询与更改。
我运用的是数组,但是为什么说我 if(p[i].num == num) 这句话有问题,有没有哪位高手能帮我一下啊?数组在类中是这样用的吧,还有存在一些原则性的错误?