field in struct is public by default : Public « Class « C++






field in struct is public by default

   
#include <iostream>
#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();
}
  
    
    
  








Related examples in the same category

1.Illustrates the use of a public variableIllustrates the use of a public variable
2.Assign the public object member address to a pointerAssign the public object member address to a pointer
3.public class extending
4.Class with only public fields and methods
5.Virtual public inheritance
6.public data field
7.public inheritance from three parent classes