6.8 COPY CONSTRUCTOR

A copy constructor is a special type of constructor which initializes all the data members of the newly created object by copying the contents of an existing object. The compiler provides a default copy constructor. The syntax of calling a copy constructor is as shown below:

class_name new_object ( existing object) ;

Assume that we have a class Period and object p1 of this class has already been defined. Then we can create new object p2 belonging to same class as

Period p2(p1);

Problem: Write a program to demonstrate default copy constructor

Solution: See Program 6.8.

Program 6.8 Default copy constructor

// ecopy1.cpp#include<iostream.h>class Period{ private :

    int days,months,years ;

  public:    void show();    Period(int ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.