一个类里包含了其他类的对象数组,不知道怎么用构造函数初始化,另外下面程序有点问题,望高手支招啊,急

2025-02-01 01:44:02
推荐回答(2个)
回答1:

#include
#include
using namespace std;

class book{
private:
char bookname[20];
int number;
char writer[20];
public:
book::book()//在此处定义无参构造函数
{
}
book(char na[20],int nu,char w[20])
{
strcpy(bookname,na);
number=nu;
strcpy(writer,w);
}
void displaybook()
{
cout< }
};

class reader{ //这边你多了一个冒号!!我已删除
private:
char readername[20];
char readerkind[20];
int bookamount;
book allbook[5];
public:
reader::reader()//在此处定义无参构造函数
{
}
void input(char rname[20],char rkind[20],int bamount,book abook[]) //在此处你要加一个void
{
strcpy(readername,rname);
strcpy(readerkind,rkind);
bookamount=bamount;
int i;
for(i=0;i {
allbook[i]=abook[i];
}
}
void display()
{
cout<<"NAME:"< cout<<"KIND:"< int i;
for(i=0;i {
cout<<"BOOK"< allbook[i].displaybook();
}
}
};

int main()
{
book b[2]={
book("Chinese",1,"aaa"),
book("Maths",2,"bbb")
};
reader stu;
stu.input("lixinyao","student",2,b);
stu.display();
return 0;
}

回答2:

在book类中加一个无参构造函数book(){};