Use Double value as the constructor parameter : Constructor « Class « C++






Use Double value as the constructor parameter

Use Double value as the constructor parameter
  

#include <iostream>
using namespace std;

class MyClass {
  double l, w, h;
  double volume;
public:
  MyClass(double a, double b, double c);
  void vol();
};

MyClass::MyClass(double a, double b, double c)
{
  l = a;
  w = b;
  h = c;

  volume = l * w * h;
}

void MyClass::vol()
{
  cout << "Volume is: " << volume << endl;
}

int main()
{
  MyClass x(2.2, 3.97, 8.09), y(1.0, 2.0, 3.0);

  x.vol();
  y.vol();
  return 0;
}

           
         
    
  








Related examples in the same category

1.Constructing and Destructing sequence for three level inheritance
2.Parameterized ConstructorsParameterized Constructors
3.string type constructorstring type constructor
4.Use constructor to init member variablesUse constructor to init member variables
5.Constructor: different parameter typeConstructor: different parameter type
6.Use automatic conversions to assign new valuesUse automatic conversions to assign new values
7.Call constructor from base classCall constructor from base class
8.Constructor with 2 parametersConstructor with 2 parameters
9.Define constructor outside a class definition
10.overloading class constructors
11.Constructor with parameter value checking