C++/Class/Private

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Class combination with private fields

<source lang="cpp">

  1. include <iostream>
  2. 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);

}


 </source>


Keep the private on your own

<source lang="cpp">

  1. include <iostream>
  2. include <string.h>

using namespace std; class Base {

public:
  Base(char *message) { strcpy(Base::message, message); }; 
  void show_base(void) { cout << message << endl; };
private:
  char message[256];

}; class Derived: public Base {

public:
  Derived(char *dmsg, char *bmsg) : Base(bmsg) {
    strcpy(message, dmsg); 
  };
  void show_derived(void) 
  { 
  cout << message << endl; 
    show_base(); 
  };
private:
  char message[256];

}; int main(void) {

  Base some_base("This is a base");
  Derived some_derived("Derived message", "Base message");
  cout << "The size of the base class is " << sizeof(some_base) << " bytes" << endl;
  cout << "The size of the derived class is " << sizeof(some_derived) << " bytes" << endl;

}


 </source>


Private and protected member variables

<source lang="cpp">

  1. include <iostream>

using namespace std; class MyClass {

 int a;           // private by default

protected: // still private relative to MyClass

 int b;

public:

 int c; 
 MyClass(int n, int m) { 
    a = n; 
    b = m; 
 }
 int geta() { 
    return a; 
 }
 int getb() { 
    return b; 
 }

}; int main() {

 MyClass object(10, 20);
 object.c = 30; // OK, c is public
 cout << object.geta() << " ";
 cout << object.getb() << " " << object.c << "\n";
 return 0;

}



 </source>


Private and public sections

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>
  3. include <string.h>

using namespace std; class Book {

 public: 
   char title[256];
   char author[64];
   float price;
   void show_title(void) { cout << title << "\n"; };
   float get_price(void) { return(price); };
   void show_book(void) 
   { 
     show_title(); 
     show_publisher();
   };
   
   void assign_publisher(char *name) { strcpy(publisher, name); };
 private:
   char publisher[256];
   void show_publisher(void) { cout << publisher << "\n"; };

}; int main(void){

  Book bible; 
 
  strcpy(bible.title, "A");
  strcpy(bible.author, "B");
  bible.price = 49.95;
  bible.assign_publisher("C");
  bible.show_book();

}


 </source>


Private and public variables and methods

<source lang="cpp">

  1. include <iostream>
  2. include <cstring>

using namespace std; class Person {

 char name[80];             // private by default

public:

 void setName(char *n); 
 void getName(char *n);

private:

 double wage; 

public:

 void setWage(double w); 
 double getWage();

}; void Person::setName(char *n) {

 strcpy(name, n);

} void Person::getName(char *n) {

 strcpy(n, name);

} void Person::setWage(double w) {

 wage = w;

} double Person::getWage() {

 return wage;

} int main() {

 Person ted;
 char name[80];
 ted.setName("Joe");
 ted.setWage(75000);
 ted.getName(name);
 cout << name << " makes $";
 cout << ted.getWage() << " per year.";
 return 0;

}


 </source>


Setting member access levels

<source lang="cpp">

  1. include <iostream>

using namespace std; class MyClass { public:

   MyClass(int level = 0);
   int GetLevel() const;
   void SetLevel(int level);

private:

   int myLevel;

}; MyClass::MyClass(int level): myLevel(level) {

   cout << "A new object has been born!" << endl;

} int MyClass::GetLevel() const {

   return myLevel;

} void MyClass::SetLevel(int level) {

   if (level < 0)
       cout << "You can"t set a level to a negative number.\n\n";
   else
       myLevel = level;

} int main() {

   MyClass myObject(5);
   cout << myObject.GetLevel() << endl;
   cout << "Calling SetLevel() with -1.\n";
   myObject.SetLevel(-1);
   cout << "Calling SetLevel() with 9.\n";
   myObject.SetLevel(9);
   cout << "Calling GetLevel(): " << myObject.GetLevel() << "\n\n";
   return 0;

}


 </source>


Use public methods to access private fields

<source lang="cpp">

  1. include <iostream>
 using namespace std;  
 class Distance                  
 {  
    private:  
       const float MTF;          
       int feet;  
       float inches;  
    public:                      
       Distance() : feet(0), inches(0.0), MTF(3.28F)  
          {  }  
       explicit Distance(float meters) : MTF(3.28F){  
          float fltfeet = MTF * meters;  
          feet = int(fltfeet);  
          inches = 12*(fltfeet-feet);  
       }  
       void showdist()           
       { cout << feet << "\"-" << inches << "\""; }  
 };  
 int main()  
 {  
    void fancyDist(Distance);    
    Distance dist1(2.35F);       
   
    dist1.showdist();  
   
    float mtrs = 3.0F;  
   
    return 0;  
 }  
 void fancyDist(Distance d){  
    cout << "(in feet and inches) = ";  
    d.showdist();  
    cout << endl;  
 }
 
   
 </source>