English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include
#include
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 << "The Book you entered is " << newBook.title << "by " << newBook.author << endl;}
void Book::getData(Book newBook)
{cout << "Enter the book title-->: "; cin.getline(newBook.title,80);
cout << "Enter the author's name-->: "; cin.getline(newBook.author, 80);}
void Fiction::getReadingLevel()
{cout << "Enter the reading level-->";cin >> level;}
void Fiction::displayReadingLevel(Fiction f)
{cout << "Reading level is " << f.level << endl;}

Sorry, all I can fit. Public & private members were required

2006-10-19 18:29:42 · 3 answers · asked by ny2neyme 1 in Computers & Internet Programming & Design

3 answers

void displayData(Book newBook)

Does not belong to Book class.

void Book::displayData(Book newBook)

Does

#include
#include
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 << "The Book you entered is " << title <<
"by " << author << endl;
}
void Book::getData()
{
cout << "Enter the book title-->: "; cin.getline(title,80);
cout << "Enter the author's name-->: "; cin.getline(author, 80);
}

void Fiction::getReadingLevel()
{
cout << "Enter the reading level-->";
cin >> level;
}

void Fiction::displayReadingLevel()
{
cout << "Reading level is " << level << 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.

2006-10-19 18:47:08 · answer #1 · answered by To Be Free 4 · 0 0

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.

2006-10-20 01:18:14 · answer #2 · answered by swami060 3 · 0 0

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; }

2006-10-19 18:42:56 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers