Skip to content

Instantly share code, notes, and snippets.

@FraGoTe
Created April 28, 2018 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FraGoTe/496eadf5a38634e0c5c7abc31554b531 to your computer and use it in GitHub Desktop.
Save FraGoTe/496eadf5a38634e0c5c7abc31554b531 to your computer and use it in GitHub Desktop.
#include <iostream>
using std::cout;
using std::endl;
class Increment {
public:
Increment( int c = 0, int i = 1 ); // default constructor
void addIncrement()
{
count += increment;
} // end function addIncrement
void print() const; // prints count and increment
private:
int count;
const int increment; // const data member
}; // end class Increment
// constructor
Increment::Increment( int c, int i )
: count( c ), // initializer for non-const member
increment( i ) // required initializer for const member
{
// empty body
} // end constructor Increment
// print count and increment values
void Increment::print() const
{
cout << "count = " << count
<< ", increment = " << increment << endl;
} // end function print
int main()
{
Increment value( 10, 5 );
cout << "Before incrementing: ";
value.print();
for ( int j = 0; j < 3; j++ ) {
value.addIncrement();
cout << "After increment " << j + 1 << ": ";
value.print();
}
return 0;
} // end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment