2013-03-14 71 views
0

所以我做了這個類:虛擬功能錯誤?

class Book 
    { 
     public: 
      Book(string newTitle = "???", string newAuthor = "???"); 
      virtual ~Book(); 

      string getTitle(); 
      string getAuthor(); 
      void setTitle(string newTitle); 
      void setAuthor(string newAuthor); 

      virtual string allInfo(); 

     private: 
      string title; 
      string author; 
    }; 

我正要覆蓋allInfo() -function其他兩類 一個叫HardcoverBooks和另一個名爲AudioBooks。兩者都從Book繼承。

這是我所做隨後在.cpp文件中的兩個類,第一AudioBook類:

string AudioBook::allInfo(){ 
    stringstream newString; 

    newString<<"Title: "<<this->title<<endl<<"Author: "<<this->author<<endl 
      <<"Narrator: "<<this->narrator<<endl 
      <<"Length(in minutes):  "<<this->length<<endl<<endl; 

    return newString.str(); 
} 

這在HardcoverBook類:

string HardcoverBook::allInfo(){ 
    stringstream newString; 

    newString<<"Title: "<<this->title<<endl<<"Author: "<<this->author<<endl 
      <<"Pages: "<<this->pages<<endl<<endl; 

    return newString.str(); 
} 

一切工作罰款和花花公子,除了AudioBook班正在抱怨:

include\Book.h||In member function 'virtual std::string AudioBook::allInfo()':| include\Book.h|41|error: 'std::string Book::title' is private| mningsuppgiftIIB\src\AudioBook.cpp|27|error: within this context| include\Book.h|42|error: 'std::string Book::author' is private| mningsuppgiftIIB\src\AudioBook.cpp|27|error: within this context| ||=== Build finished: 4 errors, 0 warnings ===|

但在HardcoverBook它並不抱怨這一點,奇怪的是。

我的問題:

  1. 我該怎麼做,使這項工作? (即,使這兩個類能夠以他們自己的方式使用功能allInfo()

  2. 爲什麼它不能這樣工作?

編輯: 這是一些功課,我做的,其中一個要求是使成員變量和屬性私人。所以保護沒有工作,爲那些傢伙的榮譽,但我會添加另一個獎金問題:

  1. 如何使它與私有成員變量一起工作?
+1

使'字符串標題;'和'字符串作者;'受保護的字段 – varnie 2013-03-14 19:37:18

+0

其他人已經回答,使他們受到保護。如果你想阻止子類修改這些成員,你也可以考慮提供公共訪問器。 編輯:或者只是使用你已經聲明的訪問器... – 2013-03-14 19:40:37

+0

我這樣做是作業,其中一個要求是使成員變量和屬性保密。應該在發佈之前提到這一點。 – user1770094 2013-03-14 19:43:34

回答

3

titleauthor成員是private。這意味着它們在子類中不可見,如AudioBook

爲了使它們對子類可見,您需要使這些成員protected而不是private

另一種方法是將成員字段保留爲專用,並添加受保護或公共訪問方法以允許讀取值。例如:

public: 
    string getAuthor() 
    { 
     return author; 
    } 

我也評論說,我不明白爲什麼你正在使用this->訪問您的類的成員。這是沒有必要的,通常最好是簡單地省略它。


沒有看到你的家庭作業,我不是100%肯定讓那

member variables and properties are private

我的猜測是,你的任務是覆蓋allInfo()的要求是什麼。您被要求擴展返回的string以包含基類實現包含的所有信息,並添加更多。

您目前的嘗試只需複製Book::allInfo()中的代碼。這就是問題所在。爲了這個工作,派生類需要訪問私有成員。你不能這樣做。因此,您的解決方案必須涉及在基類上調用allInfo(),然後附加到基類實現返回的字符串。

由於這是功課,我不會爲你實施這個!

+0

同樣作者我會說 – BlackBear 2013-03-14 19:37:44

+0

啊,好吧。我可以嘗試。我這樣做是爲了做家庭作業,其中一個要求是使成員變量和屬性保密。應該在發佈之前提到這一點。 – user1770094 2013-03-14 19:39:35

+1

你釘了它。謝謝一堆。 =) – user1770094 2013-03-14 19:52:27

2

讓你的會員protected通過派生類訪問它們,否則

protected: 
      string title; 
      string author; 

,他們是派生類的無形。

Private and Protected Members

Public members of a class A are accessible for all and everyone.

Protected members of a class A are not accessible outside of A's code, but is accessible from the code of any class derived from A.

Private members of a class A are not accessible outside of A's code, or from the code of any class derived from A.

如果你想離開他們private,另一種方式是創建protectedpublic他們訪問方法。

1

您應該private成員轉換爲:

protected: 
    string title; 
    string author; 

這樣子類可以訪問它們。