Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Call class constructor without new operator

#include <iostream> using std::cout; using std::endl; class Box {   public:     double length;     double width;     double height;     // Constructor     Box(double lengthValue, double widthValue, double heightValue) {       cout << "Box constructor called" << endl;       length = lengthValue;       width = widthValue;       height = heightValue;     }     double volume() {       return length * width * height;     } }; int main() {   Box firstBox(80.0, 50.0, 40.0);   cout << "Size of first Box object is "        << firstBox.length  << " by "        << firstBox.width << " by "        << firstBox.height << "\n "        << "Volume of first Box object is " << firstBox.volume()        << endl;   return 0; } Box constructor called Size of first Box object is 80 by 50 by 40 Volume of first Box object is 160000