C++ Constructor Defining Constructors Outside the Class

Description

C++ Constructor Defining Constructors Outside the Class

 
#include <iostream>
 
class Pool {/*www  .ja v a 2 s .  c o  m*/
private:
  double length {1.0};
  double width {1.0};
  double height {1.0};
 
public:
  // Constructors
  Pool(double lengthValue, double widthValue, double heightValue);
  Pool();                                    // No-arg constructor
  double volume();                          // Function to calculate the volume of a pool
};
 
 
// Constructor definition
Pool::Pool(double lengthValue, double widthValue, double heightValue) {
  std::cout << "Pool constructor called." << std::endl;
  length = lengthValue;
  width = widthValue;
  height = heightValue;
}
 
Pool::Pool() {}                                    // No-arg constructor
 
// Function to calculate the volume of a pool
double Pool::volume()
{
  return length*width*height;
}
 
int main() {
  Pool firstPool {80.0, 50.0, 40.0};              // Create a pool
  double firstPoolVolume{firstPool.volume()};     // Calculate the pool volume
  std::cout << "Volume of Pool object is" << firstPoolVolume << std::endl;
}



PreviousNext

Related