2012-08-29 150 views
-3

這裏是我的類代碼:類聲明和定義

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文件中的一個主要功能,什麼是所需的代碼能夠做到這一點?包含哪些聲明?

+0

你爲什麼不編譯它?順便說一下,純虛函數應該在類體內部。 – iammilind

回答

2

不正確:

virtual void LibItem::PrintDetails() = 0; 

=0應該是類定義(在報頭)的內部。

要使用該類,您需要#include "LibItem.h"

另外,在標頭:

#include <string> 

並用std::string取代的string OCCURENCES。

string參數應通過引用傳遞:中

void setReleaseDate(const string& date) 

代替

void setReleaseDate(string date) 
+0

用std :: string替換字符串的出現位置 - 這是在LibItem.h,LibItem.cpp還是包含LibItem.h的.cpp文件中? –

+0

@DarrylJanecek無處不在! –

+0

另外,對於'void setReleaseDate(const string&date)',我是否對LibItem.h中的情況執行此操作? –