Arrays of Class Objects - C++ Class

C++ examples for Class:object

Description

Arrays of Class Objects

Demo Code

                                                                                                                                                            
#include <iostream>
                                                                                                                                                            
class Pool/*from w ww.j av  a2  s.  c  om*/
{
private:
  double length {1.0};
  double width {1.0};
  double height {1.0};
                                                                                                                                                            
public:
  // Constructors
  Pool(double lv, double wv, double hv);
                                                                                                                                                            
  Pool(double side) : Pool{side, side, side}                            // Constructor for a cube
    { std::cout << "Pool constructor 2 called." << std::endl; }
                                                                                                                                                            
  Pool()                                                               // No-arg constructor
    { std::cout << "No-arg Pool constructor called." << std::endl; }
                                                                                                                                                            
  Pool(const Pool& pool)                                                 // Copy constructor
             : length {pool.length}, width {pool.width}, height {pool.height}
     { std::cout << "Pool copy constructor called." << std::endl; }
                                                                                                                                                            
  double volume() const;                            // Function to calculate the volume of a pool
};
                                                                                                                                                            
// Constructor definition
Pool::Pool(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv}
{  std::cout << "Pool constructor 1 called." << std::endl; }
                                                                                                                                                            
// Function to calculate the volume of a pool
double Pool::volume() const
{ return length*width*height; }
                                                                                                                                                            
int main()
{
  const Pool pool1 {2.0, 3.0, 4.0};                               // An arbitrary pool
  Pool pool2 {5.0};                                               // A pool that is a cube
  std::cout << "pool1 volume = " << pool1.volume() << std::endl;
  std::cout << "pool2 volume = " << pool2.volume() << std::endl;
  Pool pool3 {pool2};
  std::cout << "pool3 volume = " << pool3.volume() << std::endl;  // Volume = 125
  Pool pools[6] {pool1, pool2, pool3, Pool {2.0}};
}

Result


Related Tutorials