Assign class object : instance object « 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 };

  Box secondBox = firstBox;

  secondBox.length *= 1.1;
  secondBox.width *= 1.1;
  secondBox.height *= 1.1;

  cout << secondBox.length
       << secondBox.width
       << secondBox.height
       << endl;
       
  cout << "Volume of second Box object is " << secondBox.volume()
       << endl;


  return 0;
}
885544
Volume of second Box object is 212960








9.32.instance object
9.32.1.Using an empty initializer
9.32.2.Automatically converted into MyClass(4): MyClass ob = 4;
9.32.3.MyClass ob(4)
9.32.4.MyClass ob = MyClass(4);
9.32.5.Creates two objects.
9.32.6.Object assignment
9.32.7.Creating objects on the heap using new
9.32.8.Accessing members of objects on the heap
9.32.9.Call class constructor without new operator
9.32.10.Assign class object