2017-10-16 96 views
-7

我的任務是創建一個打印函數,用於打印特定於對象的用戶輸入數據。這個打印函數必須使用我創建的Get()函數命令。如何創建一個可以使用其他類函數和對象變量作爲參數的類函數

我已經使用Google搜索並尋找類似的問題,但無法找到解決方法。我如何創建我的老師想要的這個功能?

我想特別打印目的是BOOK1

我的代碼:

 #include <iostream> 
    #include <string> 
    #include <cstdio> 
    using namespace std; 

     class Book { 
     public: 
     void SetTitle(string title_input); 
     string GetTitle(); 
     void SetAuthor(string& author_input); 
     string GetAuthor(); 
     void SetCopyRightYear(int copyright_year_input); 
     int GetCopyRightYear(); 
     void PrintBook(); 

     private: 
     string title; 
     string author; 
     int copyright_year; 
    }; 

    void Book::SetTitle(string title_input) { 
      title = title_input; 
     } 
     string Book::GetTitle() { 
      return title; 
     } 
     void Book::SetAuthor(string& author_input) { 
      author = author_input; 
     } 
     string Book::GetAuthor() { 
      return author; 
     } 
     void Book::SetCopyRightYear(int copyright_year_input) { 
      copyright_year = copyright_year_input; 
     } 
     int Book::GetCopyRightYear() { 
      return copyright_year; 
     } 
     void Book::PrintBook() { 
      cout << "Title of Book: " << GetTitle() << endl; 
      cout << "Author of Book: " << GetAuthor() << endl;   // Function is broken FIXME 
      cout << "Copyright Year: " << GetCopyRightYear() << endl; 
     } 


    int main() 
    { 
     string title_input = ""; 
     string author_input = ""; 
     int copyright_year_input = 0; 


     Book book1; 
     Book book2; 
     Book book3; 
     Book book4; 

     cout << "Enter the book title: "; 
     cin >> title_input; 
     book1.SetTitle(title_input); 
     cout << book1.GetTitle(); 
     cout << "Enter the author name: "; 
     cin >> author_input; 
     book1.SetAuthor(author_input); 
     cout << "Enter the copyright year: "; 
     cin >> copyright_year_input; 
     book1.SetCopyRightYear(copyright_year_input); 

     cout << PrintBook(); 
+3

你的問題是什麼?你的意見是什麼?你的實際產出是多少?你的預期產出是多少? *您的問題是什麼?*請[請閱讀如何提出問題](http://stackoverflow.com/help/how-to-ask),並學習如何創建一個[Minimal,** Complete **和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

根據您的要求,您的'PrintBook()'功能看起來很好。實際上哪些不起作用? – user0042

+0

正在運行/home/ubuntu/workspace/lab25/lab25.cpp /home/ubuntu/workspace/lab25/lab25。cpp:在函數'int main()'中: /home/ubuntu/workspace/lab25/lab25.cpp:75:31:錯誤:'PrintBook'未在此範圍內聲明 cout << PrintBook(); ^這是我收到的錯誤 –

回答

0

Book.h

#pragma once 
#include <string> 

class Book 
{ 
public: 

    Book() = default; 
    ~Book() = default; 

    const std::string GetTitle() const; 
    const std::string GetAuthor() const; 
    const int GetCopyRightYear() const; 

    void SetTitle(const std::string); 
    void SetAuthor(const std::string); 
    void SetCopyRightYear(const int); 
    void PrintBook(); 

private: 
    std::string title; 
    std::string author; 
    int copyright_year; 
}; 

Book.cpp

#include "Book.h" 
// ------------------------------ 
#include <iostream> 




void Book::SetTitle(const std::string title_input) 
{ 
    title = title_input; 
} 



const std::string Book::GetTitle() const 
{ 
    return title; 
} 



const int Book::GetCopyRightYear() const 
{ 
    return copyright_year; 
} 



const std::string Book::GetAuthor() const 
{ 
    return author; 
} 



void Book::SetCopyRightYear(const int copyright_year_input) 
{ 
    copyright_year = copyright_year_input; 
} 



void Book::SetAuthor(const std::string author_input) 
{ 
    author = author_input; 
} 



void Book::PrintBook() 
{ 
    std::string output_str = ""; 
    std::cout << "Title of Book: " << GetTitle() << std::endl; 
    std::cout << "Author of Book: " << GetAuthor() << std::endl; 
    std::cout << "Copyright Year: " << GetCopyRightYear() << std::endl; 
} 

的main.cpp

// C++ Libraries. 
#include <iostream> 
#include <string> 

// User classes 
#include "Book.h" 

// Namespaces 

int main() 
{ 
    std::string title_input = ""; 
    std::string author_input = ""; 
    int copyright_year_input = 0; 

    // research dynamic memory allocation. 
    Book book1; 
    Book book2; 
    Book book3; 
    Book book4; 


    // user sets book title. 
    std::cout << "Enter the book title: "; 
    std::getline(std::cin, title_input); 
    book1.SetTitle(title_input); 

    // user sets the authors name 
    std::cout << "Enter the author name: "; 
    std::getline(std::cin, author_input); 
    book1.SetAuthor(author_input); 

    // user inputs the copyright year. 
    std::cout << "Enter the copyright year: "; 
    std::cin >> copyright_year_input; 
    book1.SetCopyRightYear(copyright_year_input); 

    // Display the information. 
    book1.PrintBook(); 
} 

注:

  • 當你開始使用多個命名空間的更容易地看到什麼是什麼,如果你不預先定義它們。
  • Const正確性意味着您和其他開發人員知道可以更改什麼以及哪些不能。它也使編譯器更清晰。
  • std :: getline讀取包含空格的整行。

只是關於清晰度和理解的簡短說明。目前你的代碼很混亂,這使得它不僅對你自己而且對其他人進行調試非常困難。

我不能說這裏,但爲了以防萬一,你的類應該在頭文件和源代碼格式,主要功能(入口點)的主要源代碼文件。在強烈建議對基本C++進行一些研究之前,您是否被告知了這些信息。僅僅爲了初學者,我已經在下面提供了一些鏈接來幫助。一旦你的代碼整齊地格式化,你可能會找出問題所在。

編碼愉快:)

參考文獻: 香草薩特.cpp的公約2014 - 簡單了複雜性: https://www.youtube.com/watch?v=xnqTKD8uD64

頁眉和包括 - C++格式: http://www.cplusplus.com/forum/articles/10627/

另請參見教程在cplusplus.com上。

相關問題