C++ displayBooks函数c++;
我有一个作业,我需要显示一本书的信息。所有数据都需要通过driver.cpp中的displayBooks()函数显示。它必须输出标题、作者、每本书的页数以及每本书的价格。为我提供了Driver.h和Driver.cpp,并给出了Author类和Book类的指南。下面是我的代码: DRIVER.CPPC++ displayBooks函数c++;,c++,C++,我有一个作业,我需要显示一本书的信息。所有数据都需要通过driver.cpp中的displayBooks()函数显示。它必须输出标题、作者、每本书的页数以及每本书的价格。为我提供了Driver.h和Driver.cpp,并给出了Author类和Book类的指南。下面是我的代码: DRIVER.CPP #include "stdafx.h" #include "driver.h" #include <iostream> using namespace std; int main()
#include "stdafx.h"
#include "driver.h"
#include <iostream>
using namespace std;
int main()
{
// create a vector for storing the account objects
vector<Book> myBooks;
// create three Author objects
Author p1("J.K.Rowling", "Edinburgh, Scotland");
Author p2("Suzanne Collins", "Connecticut, USA");
Author p3("J.R.R. Tolkien", "Bournmouth, England");
// Create three Book objects
Book b1(p1, "Harry Potter and the Sorcerer's Stone", 256, 24.95);
Book b2(p2, "Mockingjay", 400, 12.99);
Book b3(p3, "The Hobbit", 322, 14.29);
// add the books to the vector
myBooks.push_back(b1);
myBooks.push_back(b2);
myBooks.push_back(b3);
// call the displayBooks function to display the books
displayBooks(myBooks);
cout << "\n\n";
system("PAUSE");
return 0;
}
void displayBooks(const vector<Book>& books)
{
// students need to write the code for this function
for (unsigned int i = 0; i < books.size(); i++)
{
//THIS IS WHAT I NEED TO WRITE
}
}
#包括“stdafx.h”
#包括“driver.h”
#包括
使用名称空间std;
int main()
{
//创建用于存储account对象的向量
矢量myBooks;
//创建三个Author对象
作者p1(“J.K.Rowling”,“苏格兰爱丁堡”);
作者p2(“美国康涅狄格州苏珊·柯林斯”);
作者p3(“J.R.R.托尔金”,“英格兰伯恩茅斯”);
//创建三个图书对象
b1册(p1,《哈利波特与魔法石》,256,24.95);
第b2册(p2,“模仿杰”,400,12.99);
b3书(p3,《霍比特人》,322,14.29);
//将书籍添加到向量
我的书。推回(b1);
我的书。推回(b2);
我的书。推回(b3);
//调用displayBooks函数来显示书籍
显示书籍(myBooks);
不能在书中添加getAuthor()
成员函数
Author getAuthor() {
return author;
}
在displayBooks()
中,遍历myBooks
向量
for(unsigned i = 0; i < myBooks.size(); i++) {
Book temp1 = myBooks.at(i);
Author temp2 = temp1.getAuthor();
cout <<"Author Name "<<temp2.getName()<<" Address "<<temp2.getAddress();
cout <<"Book Title "<<temp1.getTitle();
/**
* print the remaining attribute.
*/
}
for(无符号i=0;i 你到底在哪里失败了?你尝试过什么?你最近一定讨论过潜水员的displayBooks函数中的。cpp是我需要编写代码来显示所有书籍数据的地方,但我不知道写什么来输出它。我试过book::book(books[I]);但是我不知道还有什么需要domolbdnilo,是的。我显然需要帮助。好的,我这样做了,但是当我编译时,我得到了错误_main已经在driver.obj中定义,还有另一个错误,找到了一个或多个乘法定义的符号哦,等等,我修复了它。非常感谢你!!很高兴帮助@riahtron3000
#pragma once
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include "Author.h"
using namespace std;
class Book
{
private:
Author author;
string title;
int pageNumber;
double price;
public:
// The default constructor for the Book class
// Purpose: none
// Parameters: none
// Returns: none
Book();
// The constructor for the Book class
// Purpose: Take in parameters to initialize class
// Parameters: Author object, title string, page number int, price double
// Returns: none
Book(Author, string, int, double);
// The get title function
// Purpose: get titleof a book
// Parameters: none
// Returns: none
string getTitle();
// The get page number function
// Purpose: Get page number of a book
// Parameters: none
// Returns: none
int getPageNumber();
// The get price function
// Purpose: Get the price of a book
// Parameters: none
// Returns: none
double getPrice();
};
#include "stdafx.h"
#include "Author.h"
#include <iostream>
#include <string>
Author::Author()
{
name = "";
address = "";
}
Author::Author(string n, string a)
{
name = n;
address = a;
}
string Author::getAddress()
{
return address;
}
string Author::getName()
{
return name;
}
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Author
{
private:
string address,
name;
public:
// The default constructor for the Autho class
// Purpose: Construct the author class
// Parameters: none
// Returns: none
Author();
// The constructor for the Author class
// Purpose: Take in parameters to initialize class
// Parameters: string address, string name
// Returns: none
Author(string, string);
// The get address function
// Purpose: get the address of an author
// Parameters: none
// Returns: none
string getAddress();
// The get name function
// Purpose: get the name of an author
// Parameters: none
// Returns: none
string getName();
};
Author getAuthor() {
return author;
}
for(unsigned i = 0; i < myBooks.size(); i++) {
Book temp1 = myBooks.at(i);
Author temp2 = temp1.getAuthor();
cout <<"Author Name "<<temp2.getName()<<" Address "<<temp2.getAddress();
cout <<"Book Title "<<temp1.getTitle();
/**
* print the remaining attribute.
*/
}