Class combination with private fields : Private « Class « C++






Class combination with private fields

   
#include <iostream>
#include <string.h>
using namespace std;
class Book 
{
  public: 
    Book(char *title) { strcpy(Book::title, title); } ;
    void show_book(void) { cout << title; };  
    friend class Reader;
  private:
    char title[64];
};  

class Reader 
{
  public:
    Reader(char *name) { strcpy(Reader::name, name); };
    void show_reader(class Book book) { 
          cout << "Reader: " << name << ' ' << "Book: " << book.title; 
    };
    void show_book(void) { cout << "The book's reader is " << name << endl; } ;
  private:
    char name[64];
};

int main(void)
{
    Reader reader("K");
    Book favorite_book("C");
    reader.show_book();
    reader.show_reader(favorite_book);
}
  
    
    
  








Related examples in the same category

1.Private and protected member variablesPrivate and protected member variables
2.Private and public variables and methodsPrivate and public variables and methods
3.Keep the private on your own
4.Private and public sections
5.Setting member access levels
6.Use public methods to access private fields