C++/Class/Public

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

Assign the public object member address to a pointer

<source lang="cpp">

  1. include <iostream>

using namespace std; class MyClass { public:

 int i;
 MyClass(int j) { 
    i = j; 
 }

}; int main() {

 MyClass myObject(1);
 int *p;
 p = &myObject.i;                     // get address of myObject.i
 cout << *p;                          // access myObject.i via p
 return 0;

}



 </source>


Class with only public fields and methods

<source lang="cpp">

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

using namespace std; class movie {

  public:
    char name[64];
    char first_star[64];
    char second_star[64]; 
    void show_movie(void);
    void initialize(char *name, char *first, char *second);

}; void movie::show_movie(void) {

  cout << "Movie name: " << name << endl;
  cout << "Starring: " << first_star << " and " << second_star << endl << endl;

} void movie::initialize(char *movie_name, char *first, char *second) {

  strcpy(name, movie_name);
  strcpy(first_star, first);
  strcpy(second_star, second);

} int main(void) {

  movie fugitive, sleepless;
  fugitive.initialize("A", "F", "B");
  sleepless.initialize("C", "D", "E");
  cout << fugitive.name << " and " << sleepless.name << endl;
  cout << fugitive.first_star << endl;

}


 </source>


field in struct is public by default

<source lang="cpp">

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

using namespace std; struct MyBook {

 char title[64];  // Public by default
 void show_book(void) 
   {
     cout << "Book: " << title << " Price: $" << price ;  
   };
 
 void set_price(float amount) { price = amount; };
 void assign_title(char *name) { strcpy(title, name); };
 private:
   float price;

}; int main(void) {

  MyBook book; 
  book.assign_title("A");
  book.set_price(49.95);
  book.show_book();

}


 </source>


Illustrates the use of a public variable

<source lang="cpp">

  1. include <iostream>

using namespace std; class myclass { public:

 int i, j, k; // accessible to entire program

}; int main() {

 myclass a, b;
 a.i = 100; 
 a.j = 4;
 a.k = a.i * a.j;
 b.k = 12; 
 cout << a.k << " " << b.k;
 return 0;

}


 </source>


public class extending

<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_title(void) { cout << title << endl; };
 private:
   char title[64];

}; class LibraryCard : public Book {

 public:
   LibraryCard(char *title, char *author, char *publisher) : Book(title) { 
       strcpy(LibraryCard::author, author); 
       strcpy(LibraryCard::publisher, publisher); 
   };
   void show_library(void) { 
       show_title();
       cout << author << " " << publisher; 
 };
 private:
   char author[64];
   char publisher[64];

};

int main(void) {

  LibraryCard card("A", "B", "C");
  card.show_library();

}


 </source>


public data field

<source lang="cpp">

  1. include <iostream>

using namespace std; class Base {

public:
  Base(void) { cout << "Base class constructor\n"; };
  int data; 

}; class Derived:public Base {

public:
  Derived(void): Base() 
   { cout << "Derived class constructor\n"; };

}; int main(void)

{
  Derived object;
  object.data = 5;
  cout << object.data << endl;
}
 
   
 </source>


public inheritance from three parent classes

<source lang="cpp">

  1. include <iostream>

using namespace std; class One{

public:
 One(void) { cout << "Constructor for One\n"; };

}; class Two{

public:
 Two(void) { cout << "Constructor for Two\n"; };

}; class Three {

public:
 Three(void) { cout << "Constructor for Three\n"; };

}; class Derived: public One, public Three, public Two {

public:
  Derived(void) : One(), Two(), Three() 
  {
    cout << "Derived constructor called\n"; 
  };

}; int main(void) {

  Derived my_class;

}


 </source>


Virtual public inheritance

<source lang="cpp">

  1. include <iostream>

using namespace std; class base {

public:
  int i;
};

class derived1 : virtual public base {

public:
  int j;
};

class derived2 : virtual public base {

public:
  int k;
};

class derived3 : public derived1, public derived2 {

public:
  int sum;

}; int main(void) {

  derived3 object;
  object.i = 10;             // now i is unambiguous
  object.j = 20;
  object.k = 30;
  object.sum = object.i + object.j + object.k;
  cout << object.i << " ";
  cout << object.j << " " << object.k << " ";
  cout << object.sum << endl;

}


 </source>