
如何定义一个 Book( 图书 ) 类?
展开全部
#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?
#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?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询