Use default argument : Function Arguments « Function « C++





Use default argument

Use default argument
 
#include <iostream>
using namespace std;

class myclass {
  int x;
public:

  myclass(int n = 0) { 
     x = n; 
  }
  int getx() { 
     return x; 
  }
};

int main()
{
  myclass o1(10);          // declare with initial value
  myclass o2;              // declare without initializer

  cout << "o1: " << o1.getx() << '\n';
  cout << "o2: " << o2.getx() << '\n';

  return 0;
}



           
         
  










Related examples in the same category

1.A example of default arguments.A example of default arguments.
2.Compute area of a rectangle using default arguments.Compute area of a rectangle using default arguments.
3.Default function arguments