Initialize the public data members in a class : public « Class « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

class Box {
  public:
    double length;
    double width;
    double height;

    double volume() {
      return length * width * height;
    }
};

int main() {
  Box firstBox = { 80.0, 50.0, 40.0 };

  double firstBoxVolume = firstBox.volume();
  cout << firstBox.length
       << firstBox.width
       << firstBox.height
       << endl;
       
  cout << "Volume of first Box object is " << firstBoxVolume << endl;

  return 0;
}
805040
Volume of first Box object is 160000








9.11.public
9.11.1.public fields and member functions
9.11.2.Initialize the public data members in a class