将类实现到数据库程序中



所以我对C++很陌生,我很难学习如何将类实现到我拥有的函数中。我的教授没有任何帮助,我很迷茫。

因此,该程序的要点是获取一个文件"books.txt",其中每行包含以下格式的作者和一本书:道格拉斯·亚当斯,《银河系漫游指南》

我正在尝试让函数使用文件中的标题和作者数据填充带有"Book"对象的数组。它需要 4 个输入参数:字符串形式的文件名、"Book"对象数组、存储在 Book 数组中的"Book"对象的数量以及图书馆系统的容量(最多 200(。

对于文件中的每一行,我应该实例化一个 Book 对象,填写作者和标题数据成员(在下面的代码中列出(,并将该对象附加到"Book"对象数组中,它将以整数形式返回系统中的书籍数量。

这是我的头文件(Book.h(:

#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
using namespace std;
#ifndef BOOK_H
#define BOOK_H
class Book{
private: //Member Variables
string author;
string title;
string inauthor;
string intitle;
string input;
string input2;
public:
Book();
Book(string intitle, string inauthor);
string getTitle();
void setTitle(string input);
string getAuthor();
void setAuthor(string input2);
};
#endif

以下是与头文件关联的.cpp文件:

#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
#include "Book.h"
using namespace std;
//Default constructor
Book::Book(){
author = "";
title = "";
}
//Parameterized Constructor
Book::Book(string intitle, string inauthor){
}
//An accessor function that returns the name of the title
string Book::getTitle() {
return title;
}
//A function that assigns the value title to the input given by the user
void Book::setTitle(string title){
title = intitle;
}
//An accessor function that returns the name of the author
string Book::getAuthor() {
return author;
}
//A function that assigns the value author to the input given by the user
void Book::setAuthor(string author){
author = inauthor;
}

最后,这是我尝试将其放入的函数(它是不完整的,因为我之前尝试使用类的每次尝试都以一长串错误结束,这是我可以自信地说我可以做到的地方(:

#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
#include "Book.h"
#include "User.h"
using namespace std;
int readBooks (string filename, int books[] , int bookObj, int capacity){
int i = 0;
ifstream file;
file.open (filename);
if (file.fail()){
return -1;
}
else{
string line;
while ((i < books) && (i < capacity) && (getline(file,line))){
}
}

我敢打赌,这可能是一个非常简单的问题,但这本书或我一直引用的任何其他资源都无法帮助我。任何帮助或建议将不胜感激!谢谢!

我可以帮助您进行类设计。它看起来像这样:

书.h

#ifndef BOOK_H
#define BOOK_H
#include <string>
#include <vector>
const int LIBRARY_MAX_CAPACITY = 200;
class Book {
private:
std::string author_;
std::string title_;
public:
Book() : author_( "" ), title_( "" ) {}
Book( const std::string& authorIn, const std::string& titleIn ) :
author_( authorIn ), title_( titleIn ) 
{}
void setAuthor( const std::string& authorIn ) {
this->author_ = authorIn; 
// or just author_ = authorIn;
}
void setTitle( const std::string& titleIn ) {
this->title_ = titleIn;
// or just title_ = titleIn;
}
std::string getAuthor() const { return author_; }
std::string getTitle() const { return title_; }
};
void readBooks( const std::string& filename, std::vector<Book>& books );
#endif // BOOK_H

书.cpp

#include "Book.h"
#include <fstream>
// this is just pseudo code and will not actually compile
void readBooks( const std::string& filename, std::vector<Book>& books ) {
// open file, test if open correctly
std::ifstream file;
file.open( filename );
// loop through file until end is reached by reading in 
// a line of code and getting the contents of the book
while ( file still has data && line <= LIBRARY_MAX_CAPACITY ) {
// get a line of text then parse that line of text.
std::string author = "first string from file before comma"
std::string title = "second string from file after comma"
// create a book object here:
Book book( author, title );
// push back into vector that is passed into this function by reference
books.push_back( book );
}
// done with loop close the file
file.close();  
}

现在其他函数调用此函数,例如main或您的库类等。std::vector<Book>对象将通过已填充书籍对象的引用传回,并且std::vector<>有一个.size()函数,该函数将其大小作为std::size_t返回。

最新更新