這裏是我的類代碼:類聲明和定義
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
接下來,如果我想使用這個h和.cpp文件中的一個主要功能,什麼是所需的代碼能夠做到這一點?包含哪些聲明?
你爲什麼不編譯它?順便說一下,純虛函數應該在類體內部。 – iammilind