如何创建一个类函数,可以使用其他类函数和对象变量作为参数

时间:2017-10-16 07:13:13

标签: c++

我的任务是创建一个打印功能,打印用户输入的特定于对象的数据。此打印功能必须使用我创建的Get()函数命令。

我用谷歌搜索并寻找类似的问题,但找不到如何处理这个问题的方法。我怎样才能创建老师想要的这个功能?

我想要专门打印的对象是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();

1 个答案:

答案 0 :(得分: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 ++进行一些研究之前,你是否被告知过这些信息。对于初学者我已经在下面提供了一些链接来帮助。一旦您的代码格式整齐,您就可以解决问题所在。

快乐编码:)

<强>参考文献: Herb Sutter Cpp Convention 2014 - 简单性超越复杂性: https://www.youtube.com/watch?v=xnqTKD8uD64

标题和包含 - C ++格式: http://www.cplusplus.com/forum/articles/10627/

另请参阅cplusplus.com上的教程。