Thursday, July 9, 2009

C++, unsure if setting values correctly, and if inheritance is being used correctly?

Here's the code I have so far. I figure my professor wants me to set the values to make the private data accessible. The setFields() method is giving me errors but I'm not sure how to correct it.





#include %26lt;iostream.h%26gt;





class Book


{


private:


title[80]; char author[80]; public:


void setFields();


void displayData();





};





class Fiction:public Book


{


private:


int level;


public:


void getReadingLevel();


void displayReadingLevel();


};








class NonFiction:public Book


{


private:


int numPages;


public:


void getNumPages();


void displayNumPages();


};





void Book::setFields()


{


char t[80], a[80];





title[80]=t[80];


author[80]=a[80];


}

C++, unsure if setting values correctly, and if inheritance is being used correctly?
For one, you don't have a type defined for what the varialbe "table" is. Some C++ compilers allow this, others do not.





What are you trying to accomplish in setFields()? How do you want to data to be accessible?





By the sounds of the problem, and because you're using inheritance, I suspect he wants you to use the 'protected' modifier, rather than public or private. Declaring the variables in the Book base call 'protected' will mean that they will be accessible from all derived classes (i.e. all classes that inherit from Book).


No comments:

Post a Comment