将类实现到数据库程序中

时间:2018-10-26 05:25:52

标签: c++ class

因此,我对C ++还是陌生的,因此我很难学习如何将类实现为我拥有的函数。我的教授没有任何帮助,我非常迷茫。

该程序的宗旨是获取一个文件“ books.txt”,其中每行包含以下格式的作者和一本书:Douglas Adams,《银河漫游指南》

我正在尝试使用该函数来填充带有“ 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))){

    }
}

我敢打赌,这可能是一个非常简单的问题,但是我所参考的书或任何其他资源都没有对我有很大帮助。任何帮助或建议,将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

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

Book.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

Book.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或您的Library类等)。std::vector<Book>对象将通过已经填充有书本对象的引用传回,并且std::vector<>具有一个{ {1}}函数将其大小返回为.size()