Saturday, May 22, 2010

How do I access private data members in C++?

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


#include %26lt;cctype%26gt;


class Book{


private:


char title[80]; int pages; char author[80];


public:


void displayData();


void getData(Book);


void getBook(); };





class Fiction:public Book{


private: int level;


public: void getReadingLevel();


void displayReadingLevel(Fiction);


}; class NonFiction:public Book{private: int numPages;


public: void setNumPages(); void getNumPages(NonFiction);


void displayNumPages(NonFiction);


};


void displayData(Book newBook)


{cout %26lt;%26lt; "The Book you entered is " %26lt;%26lt; newBook.title %26lt;%26lt; "by " %26lt;%26lt; newBook.author %26lt;%26lt; endl;}


void Book::getData(Book newBook)


{cout %26lt;%26lt; "Enter the book title--%26gt;: "; cin.getline(newBook.title,80);


cout %26lt;%26lt; "Enter the author's name--%26gt;: "; cin.getline(newBook.author, 80);}


void Fiction::getReadingLevel()


{cout %26lt;%26lt; "Enter the reading level--%26gt;";cin %26gt;%26gt; level;}


void Fiction::displayReadingLevel(Fiction f)


{cout %26lt;%26lt; "Reading level is " %26lt;%26lt; f.level %26lt;%26lt; endl;}





Sorry, all I can fit. Public %26amp; private members were required

How do I access private data members in C++?
void displayData(Book newBook)





Does not belong to Book class.





void Book::displayData(Book newBook)





Does





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


#include %26lt;cctype%26gt;


class Book{


private:


char title[80]; int pages; char author[80];


public:


void displayData();


void getData();


void getBook();


};





class Fiction:public Book{


private: int level;


public: void getReadingLevel();


void displayReadingLevel();


};





void Book::displayData()


{


cout %26lt;%26lt; "The Book you entered is " %26lt;%26lt; title %26lt;%26lt;


"by " %26lt;%26lt; author %26lt;%26lt; endl;


}


void Book::getData()


{


cout %26lt;%26lt; "Enter the book title--%26gt;: "; cin.getline(title,80);


cout %26lt;%26lt; "Enter the author's name--%26gt;: "; cin.getline(author, 80);


}





void Fiction::getReadingLevel()


{


cout %26lt;%26lt; "Enter the reading level--%26gt;";


cin %26gt;%26gt; level;


}





void Fiction::displayReadingLevel()


{


cout %26lt;%26lt; "Reading level is " %26lt;%26lt; level %26lt;%26lt; endl;


}





You can access your classes data members by name in it's own methods. It's been a while since I've done C++ so I'm a bit rusty.
Reply:You have to write a public function for Book that returns the value of the private data member you need. These methods are called "getters" because they allow you to get the value of a data member. I believe your getData() function and similar functions should be named something else just for that reason, because the name of the function implies that it's a "getter" when it obviously doesn't return anything since its type is void.





Let's say you want to write a getter function to access the variable pages. It would look something like this:





int Book::getPages()


{ return pages; }
Reply:There are two possible solutions:


1. Provide a public get/set method combo to each 'private' member that you want to access externally. e.g:


void set_title(const char *title);


const char *get_title(void);





2. In the example that you have quoted you would be able to access the 'private' members if you change the private keyword in class Book to "protected". Protected members are almost like private members except that the derived classes like Fiction can also access and manipulate them.

funeral flowers

No comments:

Post a Comment