programming_languages:cpp:constructor

This is an old revision of the document!


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.1487515591.txt.gz
  • Last modified: 2017/02/19 14:46
  • by phreazer