Add default parameter value to class constructor - C++ Class

C++ examples for Class:Constructor

Description

Add default parameter value to class constructor

Demo Code

                                                                                                                     
#include <iostream>
using namespace std;
class Complex// w ww .j a  va2s. c  o m
{
   private:
   double realPart;
   double imaginaryPart;
   public:
   Complex(double = 0.0, double = 0.0);  // constructor prototype with default arguments
};
// implementation section
Complex::Complex(double real, double imag)   // constructor
{
   realPart = real;
   imaginaryPart = imag;
   cout << "Created the new complex number object " << realPart << " + " << imaginaryPart << "i\n";
}
int main()
{
   Complex a;             // declare an object
   Complex b(6.8, 9.7);    // declare an object
   return 0;
}

Result


Related Tutorials