#include <iostream>
#include <cstring>
using namespace std;
class assistantnurse{
private:
char* id;
char dutytime;
public:
assistantnurse(char[] ="", char =' ');
void setid(char*);
void setdutytime(char);
char* getid()const;
char getdutytime()const;
void print()const;
void inputinfo();
~assistantnurse();
};
assistantnurse::~assistantnurse()
{
delete[] id;
id=0;
}
assistantnurse::assistantnurse(char* i, char t)
{
setid(i);
setdutytime(t);
}
void assistantnurse::setid(char *i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void assistantnurse::setdutytime(char t)
{
dutytime=t;
}
char* assistantnurse::getid()const{return id;}
char assistantnurse::getdutytime()const{return dutytime;}
void assistantnurse::print()const
{
cout<<"ID: "<<id<<endl;
cout<<"Duty time: "<<dutytime<<endl<<endl;
}
void assistantnurse::inputinfo()
{
char nurseid[20];
char time;
cout<<"Enter nurse ID: ";
cin>>nurseid;
do
{
cout<<"Enter nurse Duty time(d for day/n for night): ";
cin>>time;
}while(time!='n' && time!='d');
setid(nurseid);
setdutytime(time);
}
class treatingphysician{
private:
char* id;
char* phonenumber;
assistantnurse assistant[2]; //3 assistant nurses through composition from assistantnurse class
public:
void setid(char*);
void setphonenumber(char*);
char* getid()const;
char* getphonenumber()const;
void setnurse();
void getnurse()const;
treatingphysician(char[] =" ", char[] =" ", assistantnurse[]);
~treatingphysician();
void print()const;
void inputinfo();
};
void treatingphysician::inputinfo()
{
char i[20];
char p[15];
cout<<"Enter physician ID: ";
cin>>i;
setid(i);
cout<<"Enter physican phone number: ";
cin>>p;
setphonenumber(p);
setnurse();
}
void treatingphysician::print()const
{
cout<<"Physician ID: "<<id<<endl;
cout<<"Physician phone number: "<<phonenumber<<endl<<endl;
getnurse();
}
treatingphysician::~treatingphysician()
{
delete[] id;
id=0;
delete[] phonenumber;
phonenumber=0;
}
treatingphysician::treatingphysician(char*i, char*p, assistantnurse k[2]):assistant(k)
{
setid(i);
setphonenumber(p);
}
void treatingphysician::setid(char*i)
{
int l=strlen(i);
id=new char[l+1];
strncpy(id,i,l);
id[l]='\0';
}
void treatingphysician::setphonenumber(char*p)
{
int l=strlen(p);
phonenumber=new char[l+1];
strncpy(phonenumber,p,l);
phonenumber[l]='\0';
}
char* treatingphysician::getid()const{return id;}
char* treatingphysician::getphonenumber()const{return phonenumber;}
void treatingphysician::setnurse()
{
for(int i=0;i<3;i++)
{
cout<<"Enter info for nurse #"<<i+1<<endl;
assistant[i].inputinfo();
cout<<"\n";
}
}
void treatingphysician::getnurse()const
{
for(int i=0;i<3;i++)
{
cout<<"Nurse #"<<i+1<<" ";
assistant[i].print();
}
}
int main()
{
treatingphysician e;
e.inputinfo();
e.print();
system("pause");
return 0;
}
两个班级; 护士班和医师班; 医生有3名护士和他所有的属性。 使用组合来解决这个问题,但是我坚持使用3名护士的对象数组。
在我的第二个构造函数中,我遇到了一个问题,我以前使用相同的方法处理合成,但唯一的区别是它不是合成中使用的对象数组。现在我被卡住了..请帮忙
简短的例子......
`A类{
公共: 一个(); };
B班{ 私人的: A a [2];
公共: B(A []); };
B :: B(A c [2]):a(c){}`
答案 0 :(得分:0)
使用std :: vector代替原始数组。
class A
{
public:
A();
};
typedef std::vector< A > A_vector;
class B
{
private:
A_vector m_a;
public:
B( A_vector av ) : m_a( av )
{
}
};