2017-10-16 50 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上。

相关问题