关于c++图书类型求解
定义一个Book(图书)类,在该类定义中包括数据成员:m_Bookname(书名);m_Number(存书数量);m_Price(价格)和m_Count(静态数据成员,统...
定义一个Book(图书)类,在该类定义中包括数据成员: m_Bookname(书名);m_Number(存书数量);m_Price(价格)和m_Count(静态数据成员,统计图书对象的个数)。成员函数:带默认参数构造函数,复制构造函数,析构函数,Display()显示图书的情况;Borrow()将存书数量减1,并显示当前存书数量;Restore()将存书数量加1,并显示当前存书数量。使用一个友元函数来计算书本的总价(m_Number* m_Price)。编写main函数来调用测试。
我的函数:
#include <iostream>
using namespace std;
class Book{
public:
Book(char mBookname,int mNumber,double mPrice,double mCount);
void Display();
void Borrow();
void Restore();
friend double sum(Book &mNumber,Book &mPrice);
private:
char bookname;
double price;
int number;
};
Book::Book(char mBookname,int mNumber,double mPrice){
bookname=mBookname;
number=mNumber;
price=mPrice;
}
void Book::Display(){
cout<<"The bookname is "<<bookname<<" and its price is "<<price<<", we have
"<<number<<"of them."<<endl;
}
void Book::Borrow(){
number=number-1;
cout<<"We have"<<number<<"books left."<<endl;
}
void Book::Restore(){
number=number+1;
cout<<"We have "<<number<<"books left."<<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 or 0-->no: ";
cin >> contin;
}
return 0;
}
错哪里了,友元函数怎么加入??? 展开
我的函数:
#include <iostream>
using namespace std;
class Book{
public:
Book(char mBookname,int mNumber,double mPrice,double mCount);
void Display();
void Borrow();
void Restore();
friend double sum(Book &mNumber,Book &mPrice);
private:
char bookname;
double price;
int number;
};
Book::Book(char mBookname,int mNumber,double mPrice){
bookname=mBookname;
number=mNumber;
price=mPrice;
}
void Book::Display(){
cout<<"The bookname is "<<bookname<<" and its price is "<<price<<", we have
"<<number<<"of them."<<endl;
}
void Book::Borrow(){
number=number-1;
cout<<"We have"<<number<<"books left."<<endl;
}
void Book::Restore(){
number=number+1;
cout<<"We have "<<number<<"books left."<<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 or 0-->no: ";
cin >> contin;
}
return 0;
}
错哪里了,友元函数怎么加入??? 展开
2个回答
展开全部
#include <iostream>
using namespace std;
#define MAXSIZE 10
class Book
{
public:
//Book(char mBookname, int mNumber, double mPrice, double mCount);
//书名应该为字符串,不应该是字符。可以用字符数组,也可以用string类
Book(char* mBookname, int mNumber, double mPrice);
~Book(); //析构函数
public:
void Display();
void Borrow();
void Restore();
//friend double sum(Book &mNumber, Book &mPrice);
friend double sum(Book& book);
private:
//char bookname;
char bookname[MAXSIZE];
double price;
int number;
static int m_Count; //静态字段
};
int Book::m_Count = 0; //静态数据成员在类外初始化
//Book::Book(char mBookname, int mNumber, double mPrice)
Book::Book(char* mBookname, int mNumber, double mPrice)
{
//bookname = mBookname;
strcpy(bookname, mBookname);
number = mNumber;
price = mPrice;
m_Count++; //每构造一个Book对象,计数+1
}
Book::~Book()
{
m_Count--; //每析构一个Book对象,计数-1
}
void Book::Display(){
/*cout << "The bookname is " << bookname << " and its price is " << price << ", we have
"<<number<<"of them."<<endl;*/
cout << "The bookname is " << bookname << " and its price is " << price << ", we have"\
<<number<<"of them."<<endl;
//若要换行,要在行尾加 \
}
void Book::Borrow(){
number = number - 1;
cout << "We have" << number << "books left." << endl;
}
void Book::Restore(){
number = number + 1;
cout << "We have " << number << "books left." << endl;
}
double sum(Book& book)
{
return book.number*book.price;
}
int main()
{
//Book book("C++ primer", 99.00, 6);
Book book("C++ primer", 6, 99.00);
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 or 0-->no: ";
cin >> contin;
}
return 0;
}
展开全部
#include <iostream>
#include <string>
using namespace std;
class Book{
public:
Book(string mBookname,int mNumber,double mPrice);//这个构造函数参数数量声明与定义不一样,char是单个字符 应该改用string
void Display();
void Borrow();
void Restore();
friend double sum(Book &mNumber,Book &mPrice);//友元函数在定义的时候不加friend 就和普通函数一样定义
private:
string bookname;
double price;
int number;
};
Book::Book(string mBookname,int mNumber,double mPrice){
bookname=mBookname;
number=mNumber;
price=mPrice;
}
void Book::Display(){
cout<<"The bookname is "<<bookname<<" and its price is "<<price<<", we have"
<<number<<"of them."<<endl;
}
void Book::Borrow(){
number=number-1;
cout<<"We have"<<number<<"books left."<<endl;
}
void Book::Restore(){
number=number+1;
cout<<"We have "<<number<<"books left."<<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 or 0-->no: ";
cin >> contin;
}
system("pause");
}
很不错的程序 问题不大
#include <string>
using namespace std;
class Book{
public:
Book(string mBookname,int mNumber,double mPrice);//这个构造函数参数数量声明与定义不一样,char是单个字符 应该改用string
void Display();
void Borrow();
void Restore();
friend double sum(Book &mNumber,Book &mPrice);//友元函数在定义的时候不加friend 就和普通函数一样定义
private:
string bookname;
double price;
int number;
};
Book::Book(string mBookname,int mNumber,double mPrice){
bookname=mBookname;
number=mNumber;
price=mPrice;
}
void Book::Display(){
cout<<"The bookname is "<<bookname<<" and its price is "<<price<<", we have"
<<number<<"of them."<<endl;
}
void Book::Borrow(){
number=number-1;
cout<<"We have"<<number<<"books left."<<endl;
}
void Book::Restore(){
number=number+1;
cout<<"We have "<<number<<"books left."<<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 or 0-->no: ";
cin >> contin;
}
system("pause");
}
很不错的程序 问题不大
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询