
谢谢各位帮我写一个C++程序,两天后要交了!
我有几个关键点不会写,但具体问题又很难说出来.还不如就写一个完整的吧classBookInventoryprivate-title:string-author:strin...
我有几个关键点不会写,但具体问题又很难说出来.还不如就写一个完整的吧
class BookInventory
private
- title: string
- author: string
- cost : double
- stock: int
public
+ BookInventory()
+ BookInventory(string t, string a, double c, int s)
+ setTitle(string t):void
+ setAuthor(string a): void
+ setCost (double c): void
+ setStock(int s): void
+ getTitle(): string
+ getAuthor(): string
+ getCost(): double
+ getStock():int
+ printBookInfo():void
+ sameCostAs(BookInventory book):bool
+ sameAuthorAs(BookInventory book):bool
+ setToSameCostAs(BookInventory book):void
bookInventory(): is the default constructor.
bookInventory(string t, string a, double c, int s): is a constructor with 4 arguments; the book title, the
author of the book, the price and the number in stock.
setTitle(string t), setAuthor(string a), setCost(double c), setStock(int s): these functions will set the
attributes title, author, cost and stock to the values passed.
getTitle(), getAuthor(), getCost() and getStock(): will return the content of the attributes title, author,
cost and stock respectively.
printBookInfo(): will output to the screen the content of all attributes with descriptive messages. The
cost attribute should be printed with 2 decimal places preceded by a $.
sameCostAs(BookInventory book): will compare the unit costs of the calling object and the object
passed and return true if they are the same and false otherwise.
sameAuthorAs(BookInventory book): will compare the authors of the calling object and the object
passed and return true if they are the same and false otherwise.
setToSameCostAs(BookInventory book): will set the cost of the calling object to the cost of the object
that is passed.
具体要求:
Write a program that uses the class bookInventory to
i) create 3 objects of type bookInventory;
a. book1: “The Foot Book” , by Dr. Seuss, $6.50 with 10 in stock
b. book2: “Green Egg and Ham” by Dr. Seuss, $9.99 with 12 in stock
c. book3: no information (use default constructor)
ii) display the title only of objects book1 and book2
iii) compare the price and the titles of books book1 and book2 and displays appropriate messages
iv) set the attributes of object book3 to:
a. title: same title as title of book1 (what ever the title is)
b. author: Glen Copeland
c. price: $11.98
d. stock: same stock as book2 (what ever the amount is)
v) display all of the information for book3
vi) display the title and cost of the most expensive book with a descriptive message 展开
class BookInventory
private
- title: string
- author: string
- cost : double
- stock: int
public
+ BookInventory()
+ BookInventory(string t, string a, double c, int s)
+ setTitle(string t):void
+ setAuthor(string a): void
+ setCost (double c): void
+ setStock(int s): void
+ getTitle(): string
+ getAuthor(): string
+ getCost(): double
+ getStock():int
+ printBookInfo():void
+ sameCostAs(BookInventory book):bool
+ sameAuthorAs(BookInventory book):bool
+ setToSameCostAs(BookInventory book):void
bookInventory(): is the default constructor.
bookInventory(string t, string a, double c, int s): is a constructor with 4 arguments; the book title, the
author of the book, the price and the number in stock.
setTitle(string t), setAuthor(string a), setCost(double c), setStock(int s): these functions will set the
attributes title, author, cost and stock to the values passed.
getTitle(), getAuthor(), getCost() and getStock(): will return the content of the attributes title, author,
cost and stock respectively.
printBookInfo(): will output to the screen the content of all attributes with descriptive messages. The
cost attribute should be printed with 2 decimal places preceded by a $.
sameCostAs(BookInventory book): will compare the unit costs of the calling object and the object
passed and return true if they are the same and false otherwise.
sameAuthorAs(BookInventory book): will compare the authors of the calling object and the object
passed and return true if they are the same and false otherwise.
setToSameCostAs(BookInventory book): will set the cost of the calling object to the cost of the object
that is passed.
具体要求:
Write a program that uses the class bookInventory to
i) create 3 objects of type bookInventory;
a. book1: “The Foot Book” , by Dr. Seuss, $6.50 with 10 in stock
b. book2: “Green Egg and Ham” by Dr. Seuss, $9.99 with 12 in stock
c. book3: no information (use default constructor)
ii) display the title only of objects book1 and book2
iii) compare the price and the titles of books book1 and book2 and displays appropriate messages
iv) set the attributes of object book3 to:
a. title: same title as title of book1 (what ever the title is)
b. author: Glen Copeland
c. price: $11.98
d. stock: same stock as book2 (what ever the amount is)
v) display all of the information for book3
vi) display the title and cost of the most expensive book with a descriptive message 展开
5个回答
展开全部
看不懂外国文,翻译成中文后估计还有人帮忙。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
BookInventory::BookInventory(std::string t, std::string a, double c, int s)
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(std::string t)
{title=t;}
void BookInventory::setAuthor(string a)
{author=a;}
void BookInventory::setCost(double c)
{cost=c;}
void BookInventory::setStock(int s)
{stock=s;}
string BookInventory::getTitle()
{return title;}
string BookInventory::getAuthor()
{return author;}
string BookInventory::getCost()
{return cost;}
string BookInventory::getStock()
{return stock;}
给你写了几个 都很简单的 剩下的自己试试~
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(std::string t)
{title=t;}
void BookInventory::setAuthor(string a)
{author=a;}
void BookInventory::setCost(double c)
{cost=c;}
void BookInventory::setStock(int s)
{stock=s;}
string BookInventory::getTitle()
{return title;}
string BookInventory::getAuthor()
{return author;}
string BookInventory::getCost()
{return cost;}
string BookInventory::getStock()
{return stock;}
给你写了几个 都很简单的 剩下的自己试试~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//写了个完整的,DEV C++测试通过!
#include <string>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
class BookInventory
{
public:
BookInventory();
BookInventory( string , string , double , int );
void setTitle( string );
void setAuthor( string );
void setCost( double );
void setStock( int );
string getTitle();
string getAuthor();
double getCost();
int getStock();
void printBookInfo()const;
bool sameCostAs( BookInventory& )const;
bool sameAuthorAs( BookInventory& )const;
void setToSameCostAs( BookInventory& );
private:
string title;
string author;
double cost;
int stock;
};
//------------------------------------------------------------------------------
BookInventory::BookInventory(): title(""), author(""), cost(0.0), stock(0) {}
BookInventory::BookInventory(string t, string a, double c, int s)
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(string t) {title=t;}
void BookInventory::setAuthor(string a) {author=a;}
void BookInventory::setCost(double c) {cost=c;}
void BookInventory::setStock(int s) {stock=s;}
string BookInventory::getTitle() {return title;}
string BookInventory::getAuthor() {return author;}
double BookInventory::getCost() {return cost;}
int BookInventory::getStock() {return stock;}
void BookInventory::printBookInfo()const
{
cout<<"title:\t"<<title<<endl;
cout<<"author:\t"<<author<<endl;
cout<<"cost:\t$"<<cost<<endl ;
cout<<"stock:\t"<<stock<<endl;
}
bool BookInventory::sameCostAs(BookInventory& book)const
{return cost == book.getCost();}
bool BookInventory::sameAuthorAs(BookInventory& book)const
{return author == book.getAuthor();}
void BookInventory::setToSameCostAs(BookInventory& book)
{cost = book.getCost();}
//------------------------------------------------------------------------------
int main()
{
BookInventory book1("The Foot Book","Dr.Seuss",6.50,10);
BookInventory book2("Green Egg and Ham","Dr.Seuss",9.99,12);
BookInventory book3;
cout<<"book1.title: "<<book1.getTitle()<<endl;
cout<<"book2.title: "<<book2.getTitle()<<endl;
cout<<endl;
//比较价格和作者,并给出结果
if(book1.sameCostAs(book2))cout<<"book1.cost and book2.cost is same\n";
else cout<<"book1.cost and book2.cost is different \n";
if(book1.sameAuthorAs(book2))cout<<"book1.author and book2.author is same\n";
else cout<<"book1.author and book2.author is different\n";
cout<<endl;
//设置book3的相关信息
book3.setTitle(book1.getTitle());
book3.setAuthor("Glen Copeland");
book3.setCost(11.98);
book3.setStock(book2.getStock());
cout<<endl;
//显示book3的信息
book3.printBookInfo();
cout<<endl;
//比较出最贵的书籍并显示其相关信息
if( book1.getCost()>book2.getCost() )
if( book1.getCost()>book3.getCost() )
{cout<<"The title of the most expensive book is <<"<<book1.getTitle()
<<">>"<<endl;
book1.printBookInfo();}
else
{cout<<"The title of the most expensive book is <<"<<book3.getTitle()
<<">>"<<endl;
book3.printBookInfo();}
else
if( book1.getCost()>book3.getCost() )
{cout<<"The title of the most expensive book is <<"<<book2.getTitle()
<<">>"<<endl;
book2.printBookInfo();}
else
{cout<<"The title of the most expensive book is <<"<<book3.getTitle()
<<">>"<<endl;
book3.printBookInfo();}
system("pause");
return 0;
}
//==============================================================================
#include <string>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
class BookInventory
{
public:
BookInventory();
BookInventory( string , string , double , int );
void setTitle( string );
void setAuthor( string );
void setCost( double );
void setStock( int );
string getTitle();
string getAuthor();
double getCost();
int getStock();
void printBookInfo()const;
bool sameCostAs( BookInventory& )const;
bool sameAuthorAs( BookInventory& )const;
void setToSameCostAs( BookInventory& );
private:
string title;
string author;
double cost;
int stock;
};
//------------------------------------------------------------------------------
BookInventory::BookInventory(): title(""), author(""), cost(0.0), stock(0) {}
BookInventory::BookInventory(string t, string a, double c, int s)
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(string t) {title=t;}
void BookInventory::setAuthor(string a) {author=a;}
void BookInventory::setCost(double c) {cost=c;}
void BookInventory::setStock(int s) {stock=s;}
string BookInventory::getTitle() {return title;}
string BookInventory::getAuthor() {return author;}
double BookInventory::getCost() {return cost;}
int BookInventory::getStock() {return stock;}
void BookInventory::printBookInfo()const
{
cout<<"title:\t"<<title<<endl;
cout<<"author:\t"<<author<<endl;
cout<<"cost:\t$"<<cost<<endl ;
cout<<"stock:\t"<<stock<<endl;
}
bool BookInventory::sameCostAs(BookInventory& book)const
{return cost == book.getCost();}
bool BookInventory::sameAuthorAs(BookInventory& book)const
{return author == book.getAuthor();}
void BookInventory::setToSameCostAs(BookInventory& book)
{cost = book.getCost();}
//------------------------------------------------------------------------------
int main()
{
BookInventory book1("The Foot Book","Dr.Seuss",6.50,10);
BookInventory book2("Green Egg and Ham","Dr.Seuss",9.99,12);
BookInventory book3;
cout<<"book1.title: "<<book1.getTitle()<<endl;
cout<<"book2.title: "<<book2.getTitle()<<endl;
cout<<endl;
//比较价格和作者,并给出结果
if(book1.sameCostAs(book2))cout<<"book1.cost and book2.cost is same\n";
else cout<<"book1.cost and book2.cost is different \n";
if(book1.sameAuthorAs(book2))cout<<"book1.author and book2.author is same\n";
else cout<<"book1.author and book2.author is different\n";
cout<<endl;
//设置book3的相关信息
book3.setTitle(book1.getTitle());
book3.setAuthor("Glen Copeland");
book3.setCost(11.98);
book3.setStock(book2.getStock());
cout<<endl;
//显示book3的信息
book3.printBookInfo();
cout<<endl;
//比较出最贵的书籍并显示其相关信息
if( book1.getCost()>book2.getCost() )
if( book1.getCost()>book3.getCost() )
{cout<<"The title of the most expensive book is <<"<<book1.getTitle()
<<">>"<<endl;
book1.printBookInfo();}
else
{cout<<"The title of the most expensive book is <<"<<book3.getTitle()
<<">>"<<endl;
book3.printBookInfo();}
else
if( book1.getCost()>book3.getCost() )
{cout<<"The title of the most expensive book is <<"<<book2.getTitle()
<<">>"<<endl;
book2.printBookInfo();}
else
{cout<<"The title of the most expensive book is <<"<<book3.getTitle()
<<">>"<<endl;
book3.printBookInfo();}
system("pause");
return 0;
}
//==============================================================================
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面这个应该算基本合格,自己再补补细节吧!
#include <string>
#include <iostream>
using namespace std;
class BookInventory
{
public:
BookInventory();
BookInventory(string t, string a, double c, int s);
void setTitle(string t);
void setAuthor(string a);
void setCost(double c);
void setStock(int s);
string getTitle();
string getAuthor();
double getCost();
int getStock();
void printBookInfo();
bool sameCostAs(BookInventory book);
bool sameAuthorAs(BookInventory book);
void setToSameCostAs(BookInventory book);
private:
string title;
string author;
double cost;
int stock;
};
BookInventory::BookInventory():
title(""), author(""), cost(0.0), stock(0)
{
}
BookInventory::BookInventory(string t, string a, double c, int s)
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(string t)
{title=t;}
void BookInventory::setAuthor(string a)
{author=a;}
void BookInventory::setCost(double c)
{cost=c;}
void BookInventory::setStock(int s)
{stock=s;}
string BookInventory::getTitle()
{return title;}
string BookInventory::getAuthor()
{return author;}
double BookInventory::getCost()
{return cost;}
int BookInventory::getStock()
{return stock;}
void BookInventory::printBookInfo()
{
cout<<"title:\t"<<title<<endl
<<"author:\t"<<author<<endl
<<"cost:\t$"<<cost<<endl
<<"stock:\t"<<stock<<endl;
}
bool BookInventory::sameCostAs(BookInventory book)
{return cost == book.getCost();}
bool BookInventory::sameAuthorAs(BookInventory book)
{return author == book.getAuthor();}
void BookInventory::setToSameCostAs(BookInventory book)
{cost = book.getCost();}
int main(int argc, char *argv[])
{
BookInventory book1( "The Foot Book", "Dr. Seuss", 6.50, 10);
BookInventory book2( "Green Egg and Ham", "Dr. Seuss", 9.99, 12);
BookInventory book3;
cout<<book1.getTitle()<<endl
<<book2.getTitle()<<endl;
if ( book1.sameCostAs(book2))
{
cout<<"book1's cost is same as book2"<<endl;
}
else
{
cout<<"book1's cost does not same as book2"<<endl;
}
book3.setTitle( book1.getTitle());
book3.setAuthor( "Glen Copeland" );
book3.setCost(11.98);
book3.setStock(book2.getStock());
book3.printBookInfo();
cout<<"the most expensive book's title is:"<<book3.getTitle()<<endl
<<"and the cost is:"<<book3.getCost()<<endl;
return 0;
}
#include <string>
#include <iostream>
using namespace std;
class BookInventory
{
public:
BookInventory();
BookInventory(string t, string a, double c, int s);
void setTitle(string t);
void setAuthor(string a);
void setCost(double c);
void setStock(int s);
string getTitle();
string getAuthor();
double getCost();
int getStock();
void printBookInfo();
bool sameCostAs(BookInventory book);
bool sameAuthorAs(BookInventory book);
void setToSameCostAs(BookInventory book);
private:
string title;
string author;
double cost;
int stock;
};
BookInventory::BookInventory():
title(""), author(""), cost(0.0), stock(0)
{
}
BookInventory::BookInventory(string t, string a, double c, int s)
{
title=t;
author=a;
cost=c;
stock=s;
}
void BookInventory::setTitle(string t)
{title=t;}
void BookInventory::setAuthor(string a)
{author=a;}
void BookInventory::setCost(double c)
{cost=c;}
void BookInventory::setStock(int s)
{stock=s;}
string BookInventory::getTitle()
{return title;}
string BookInventory::getAuthor()
{return author;}
double BookInventory::getCost()
{return cost;}
int BookInventory::getStock()
{return stock;}
void BookInventory::printBookInfo()
{
cout<<"title:\t"<<title<<endl
<<"author:\t"<<author<<endl
<<"cost:\t$"<<cost<<endl
<<"stock:\t"<<stock<<endl;
}
bool BookInventory::sameCostAs(BookInventory book)
{return cost == book.getCost();}
bool BookInventory::sameAuthorAs(BookInventory book)
{return author == book.getAuthor();}
void BookInventory::setToSameCostAs(BookInventory book)
{cost = book.getCost();}
int main(int argc, char *argv[])
{
BookInventory book1( "The Foot Book", "Dr. Seuss", 6.50, 10);
BookInventory book2( "Green Egg and Ham", "Dr. Seuss", 9.99, 12);
BookInventory book3;
cout<<book1.getTitle()<<endl
<<book2.getTitle()<<endl;
if ( book1.sameCostAs(book2))
{
cout<<"book1's cost is same as book2"<<endl;
}
else
{
cout<<"book1's cost does not same as book2"<<endl;
}
book3.setTitle( book1.getTitle());
book3.setAuthor( "Glen Copeland" );
book3.setCost(11.98);
book3.setStock(book2.getStock());
book3.printBookInfo();
cout<<"the most expensive book's title is:"<<book3.getTitle()<<endl
<<"and the cost is:"<<book3.getCost()<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-11-26
展开全部
不能翻译成中文吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询