如何定义一个 Book( 图书 ) 类?

 我来答
feiryGod
2018-01-27 · TA获得超过120个赞
知道答主
回答量:14
采纳率:0%
帮助的人:11.6万
展开全部
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

class Book
{
public:
Book(string pName, double pPrice, int pNumber);//构造函数时从调用函数传过来参数,所以 用pass的首字母p表示接受传参
void display();
void borrow();
void restore();
private:
string name;
double price;
int number;
};

Book::Book(string pName, double pPrice, int pNumber)
{
name = pName;
price = pPrice;
number = pNumber;
}
void Book::display()
{
cout << "The book " << name << "'s price is " << price
<< " and, we have " << number << " of them."
<< endl;
}
void Book::borrow()
{
number -= 1;
cout << "we have " << number << " of them."
<< endl;
}

void Book::restore()
{
number = number + 1;
cout << "we have " << number << " of them."
<< endl;
}

int main()
{
Book book("C++ primer", 99.00, 6);
int choice;
cout << "1-->display the information of the book" << endl;
cout << "2-->borrow book" << endl;
cout << "3-->restore book" << endl;
int contin = 1;

while(contin)
{
cout << "Please input the function you want: ";
cin >> choice;
switch(choice)
{
case 1:book.display();break;
case 2:book.borrow();break;
case 3:book.restore();break;
default:cout << "wrong operation!";break;
}

cout <<"continue? 1-yes/0-no: ";
cin >> contin;
}
return 0;
}

I think this is perfect for you question! right?
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式