programming_languages:cpp:constructor

Basic constructor

class Example
{
   public:
      void someMethod( double someDouble );
      double someFunction( void );
      Example();  // This is the constructor
 
   private:
      double someDouble;
};

Example::Example(void)
{
    cout << "Constructor was called" << endl;
}

Initialization lists to initialize members

Example::Example( double someDoubleParam): someDouble(someDoubleParam)
{
       cout << "Constructor was called" << endl;
}

Create objects

int x(5); // (int x = 5)
Fraction fiveThirds(5, 3); // Fraction fiveThirds = Fraction(5,3) 
  • programming_languages/cpp/constructor.txt
  • Last modified: 2020/05/29 16:40
  • by phreazer