O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Copy Constructor and When You Need It

There is one special form of constructor with arguments: the copy constructor (see Listing 17.8).

This is used so that you can create an object whose state is exactly the same as the state of another object of the same class.

Listing 17.8. A Copy Constructor in aSAMSAccumulator
  1: class aSAMSAccumulator
  2: {
  3:    private:
  4:
  5:       float myAccumulator;
  6:
  7:    public:
  8:
  9:       enum anOperator
               {add,subtract,multiply,divide,query,reset} ;
 10:
 11:       aSAMSAccumulator(void);
 12:       aSAMSAccumulator(float theInitialAccumulatorValue);
*13:       aSAMSAccumulator
               (aSAMSAccumulator theOtherAccumulator);
 14:
 15:       virtual ~aSAMSAccumulator(void);
 16:
 17:       void Accumulate
               (char theOperator,float theOperand = 0);
 18: } ;

aSAMSAccumulator ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required