Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Defining and using a default class constructor

#include <iostream> #include <iostream> using std::cout; using std::endl; class Box {   public:     double length;     double width;     double height;     Box() {       cout << "Default constructor called" << endl;       length = width = height = 1.0;          // Default dimensions     }          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);   Box smallBox;   return 0; } Box constructor called Default constructor called