类声明和定义

时间:2012-08-29 11:39:14

标签: c++ class header-files

这是我的班级代码:

class LibItem
{
public:
    virtual void PrintDetails() = 0;
    void setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
    {
        Title = setItemTitle;
        Author = setItemAuthor;
        ReleaseDate = setItemReleaseDate;
        Copyright = setItemCopyright;
        Genre = setItemGenre;
        Status = setItemStatus;
    }
    void setTitle(string TitleName)
    {
        Title = TitleName;
    }
    string getTitle()
    {
        return Title;
    }
    void setReleaseDate(string date)
    {
        ReleaseDate = date;
    }
    string getReleaseDate()
    {
        return ReleaseDate;
    }
    void setAuthor(string AuthorName)
    {
        Author = AuthorName;
    }
    string getAuthor()
    {
        return Author;
    }
    void setCopyright(string CopyrightDetails)
    {
        Copyright = CopyrightDetails;
    }
    string getCopyright()
    {
        return Copyright;
    }
    void setGenre(string GenreDetails)
    {
        Genre = GenreDetails;
    }
    string getGenre()
    {
        return Genre;
    }
    void setStatus(string StatusDetails)
    {
        Status = StatusDetails;
    }
    string getStatus()
    {
        return Status;
    }
private:
    string Title;
    string ReleaseDate;
    string Author;
    string Copyright;
    string Genre;
    string Status;
};

我想将它放入.h文件和.cpp文件中。 以下代码是否正确?

LibItem.cpp:

//---------------------------------------------------------------------------

#pragma hdrstop

#include "LibItem.h"

virtual void LibItem::PrintDetails() = 0;
void LibItem::setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
Title = setItemTitle;
Author = setItemAuthor;
ReleaseDate = setItemReleaseDate;
Copyright = setItemCopyright;
Genre = setItemGenre;
Status = setItemStatus;
}
void LibItem::setTitle(string TitleName)
{
Title = TitleName;
}
string LibItem::getTitle()
{
return Title;
}
void LibItem::setReleaseDate(string date)
{
ReleaseDate = date;
}
string LibItem::getReleaseDate()
{
return ReleaseDate;
}
void LibItem::setAuthor(string AuthorName)
{
Author = AuthorName;
}
string LibItem::getAuthor()
{
return Author;
}
void LibItem::setCopyright(string CopyrightDetails)
{
Copyright = CopyrightDetails;
}
string LibItem::getCopyright()
{
return Copyright;
}
void LibItem::setGenre(string GenreDetails)
{
Genre = GenreDetails;
}
string LibItem::getGenre()
{
return Genre;
}
void LibItem::setStatus(string StatusDetails)
{
Status = StatusDetails;
}
string LibItem::getStatus()
{
return Status;
}
};

//---------------------------------------------------------------------------
#pragma package(smart_init)

LibItem.h

//---------------------------------------------------------------------------

#ifndef LibItemH
#define LibItemH

class LibItem
{
public:
    virtual void PrintDetails();
    void setDetails(string, string, string, string, string, string);
    void setTitle(string);
    void setReleaseDate(string);
    string getReleaseDate();
    void setAuthor(string);
    string getAuthor();
    void setCopyright(string);
    string getCopyright();
    void setGenre(string);
    string getGenre();
    void setStatus(string);
    string getStatus();
private:
    string Title;
    string ReleaseDate;
    string Author;
    string Copyright;
    string Genre;
    string Status;
};


//---------------------------------------------------------------------------
#endif

接下来,如果我想在main函数中使用这个.h和.cpp文件,那么能够执行此操作所需的代码是什么?需要什么包含声明?

1 个答案:

答案 0 :(得分:2)

不正确:

virtual void LibItem::PrintDetails() = 0;

=0应该在类定义内(在标题中)。

要使用该课程,您需要#include "LibItem.h"

另外,在标题中:

#include <string>

并将string的出现替换为std::string

string参数应通过引用传递:

void setReleaseDate(const string& date)

而不是

void setReleaseDate(string date)