设计一个图书管理程序,能进行添加,删除,查询操作,有date类,author类和book类,要求使用类和对象的知识,最好不用继承,然后我就用了子对象来解决。
前面设计类问题都不大,到了设计图书管理程序时直接给我整不会了
全部代码如下
#include <iostream>
using namespace std;
class date
{
protected:
int year;//年
int month;//月
int day;//日
public:
date() {};
date(int y, int m, int d)//构造函数
{
year = y;
month = m;
day = d;
}
void showdate()
{
cout << year<< "年" << month<<"月" << day << "日";
}
};
class author
{
protected:
string name;//姓名
int age;//年龄
date wridate;//写作时间为子对象
public:
author() {};
author(int y, int m, int d, string nam, int ag) ://构造函数
wridate(y, m, d)
{
name = nam;
age = ag;
}
void showwrdate()//输出写作时间子对象
{
cout << "图书写作时间:" ;
wridate.showdate();
}
void showau()//输出作者信息
{
cout << "作者姓名:" << name <<endl << "年龄:" << age ;
}
};
class book
{
protected:
int num;//书号
string bookname;//书名
float price;//价格
string press;//出版社
date pubdate;//出版时间为子对象
author writer;//作者为子对象
public:
book() {};
book(int a,int b,int c,int y, int m, int d,int n,string bna,float pri,string pre,string nam,int ag ) :
pubdate(a,b,c),writer(y,m,d,nam,ag)
{
num = n;
bookname = bna;
price = pri;
press = pre;
}
void showbook()//输出图书信息
{
cout << "书号: " << num << endl << "书名: " << bookname << endl<< "价格:" << price << endl << "出版社:" << press << endl;
cout << "图书出版时间:"; pubdate.showdate(); cout << endl;
writer.showwrdate(); cout << endl;
writer.showau(); cout << endl;
}
};
void showMenu()
{
cout << "图书管理程序" << endl;
cout << "1.添加图书 " << endl;
cout << "2.删除图书 " << endl;
cout << "3.查询图书 " << endl;
cout << "4.全部显示 " << endl;
}
int main()
{
/*book b1(2002, 1, 1, 2000, 1, 1, 01, "c++", 80, "清华大学出版社", "小明", 70);
b1.showbook();*/
while (true)
{
book sumbo[1];
showMenu();
int se = 0;
cin >> se;
switch (se)
{
case 1:
{
cout << "请依次输入图书出版时间,写作时间,书号,书名,价格,出版社,作者姓名,年龄" << endl;
int a; int b; int c; int y; int m; int d; int n; string bna; float pri; string pre; string nam; int ag;
cin >> a >> b >> c >> y >> m >> d >> n >> bna >> pri >> pre >> nam >> ag;
sumbo[0] = { book(a,b,c,y,m,d,n,bna,pri,pre,nam,ag) };
system("pause");
system("cls");
break;
}
case 2:
break;
case 3:
break;
case 4:
sumbo[0].showbook();
break;
default:break;
}
}
system("pause");
return 0;
}
在不运行while(true)这一行以及往下的代码时,运行注释的代码可以得到
书号: 1
书名: c++
价格:80
出版社:清华大学出版社
图书出版时间:2002年1月1日
图书写作时间:2000年1月1日
作者姓名:小明
年龄:70
也就是下图
但是如果运行Switch里面的代码
先选case1
再选case4
这里三个string类的变量都没有输出
已经理解不能了,希望能指点一下是哪个地方出了问题。另外如果不使用对象数组的话,应该要怎么做