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?
Let's say you want to call the setFields() method from outside the Book class, and you want to pass in the values for t and a, which you will assign to the title and author data members. The setFields() method needs to take t and a as parameters like this:





void Book::setFields(char[80] t, char[80]a)





This means that when you call the setFields function, you need to pass it two char arrays for the values of t and a. You won't need this line:





char t[80], a[80]





anymore because those arrays were declared automatically in the first line of the method definition. The rest of what you have in the method is correct.
Reply:insted of inheritance public book try private book


ex


class Fiction:private Book





i hope it works
Reply:you cannot assign array elements to another array using the implementation you are using in the setFields method. you will need to utilize loops to achieve this, good luck.





eg.


for(inti=0;i%26lt;t.size();i++) {title[i] == t[i];}


No comments:

Post a Comment