Cpp - Use copy constructor to initialize Objects


Constructors can be overloaded.

You can't overload destructors.

Destructors always have the same signature: the name of the class prepended by a tilde (~) and no parameters.

A member variable can be set during the initialization or by assigning it a value in the body of the constructor.

To assign values in a constructor's initialization, put a colon after the closing parentheses of the constructor's parameter list.

After the colon, list the name of a member variable followed by a pair of parentheses.

Inside the parentheses, put an expression that initializes the member variable.

If more than one variable is being set in this manner, separate each one with a comma.

The following example shows how to initialize member variables:

    // body of constructor 

The preceding example sets the speed member variable to 5 and the wheelSize variable to 12.

Copy Constructor

In addition to providing a default constructor and destructor, the compiler provides a default copy constructor.

The copy constructor is called every time a copy of an object is made.

When you pass an object by value, a temporary copy of that object is made.

If the object is a user-defined object, the class's copy constructor is called.

All copy constructors take one parameter: a reference to an object of the same class.

Cart(const Cart &trike); 

The default copy constructor copies each member variable from the object passed as a parameter to the member variables of the new object.

This is called a shallow copy.

A shallow copy copies the exact values of one object's member variables into another object.

Pointers in both objects end up pointing to the same memory.

A deep copy copies the values allocated on the heap to newly allocated memory.

The following code shows how to do deep copy via your own copy constructor.


#include <iostream> 
   /*w  w w.j  a  va2s .c o  m*/
class Cart 
    Cart();                                 // default constructor 
    Cart(const Cart&);                 // copy constructor 
    ~Cart();                                // destructor 
    int getSpeed() const { return *speed; } 
    void setSpeed(int newSpeed) { *speed = newSpeed; } 
    void pedal(); 
    void brake(); 
    int *speed; 
    speed = new int; 
    *speed = 5; 
Cart::Cart(const Cart& rhs) 
    speed = new int; 
    *speed = rhs.getSpeed(); 
    delete speed; 
    speed = NULL; 
void Cart::pedal() 
    setSpeed(*speed + 1); 
    std::cout << "\nPedaling " << getSpeed() << " mph" << std::endl; 
void Cart::brake() 
    setSpeed(*speed - 1); 
    std::cout << "\nPedaling " << getSpeed() << " mph" << std::endl; 
int main() 
    Cart shoppingCart; 
    Cart your_cart(shoppingCart); 
    std::cout << "shoppingCart's speed: " << shoppingCart.getSpeed() << std::endl; 
    std::cout << "your_cart's speed: " << your_cart.getSpeed() << std::endl; 
    std::cout << "setting shoppingCart to 10 ..." << std::endl; 
    std::cout << "shoppingCart's speed: " << shoppingCart.getSpeed() << std::endl; 
    std::cout << "your_cart's speed: " << your_cart.getSpeed() << std::endl; 
    return 0; 


Related Topic