#Previous question #Index of C++ Questions #Next question

Deep Copy:
Overriding default copy constructor with a constructor which copies all the members including all dynamically allocated members is called deep copy constructor.

Example

#include<string.h>
#include<iostream.h>
class User
{
  protected:
  int user_id;
  char *user_name;
  public:
  User()
  {
    user_id = 0;
    user_name = new char[10];
  }
  ~ User()
  {
    user_id = 0;
    delete [] user_name;
  }
  int get_id()
  {
    return user_id;
  }
  void set_id(int id)
  {
    user_id = id;
  }
  char* get_name(void)
  {
    return user_name;
  }
  void set_name(char* name)
  {
    strcpy(user_name, name);
  }
  User(User &u)
  {
    user_id = u. user_id;
    user_name = new char[10];
    strcpy(user_name, u.user_name);
  }
  User & operator = (User &u)
  {
    user_id = u. user_id;
    strcpy(user_name, u.user_name);
    return *this;
  }
};
void main(int argc, char *argv[])
{
  User user1;
  user1.set_id(1);
  user1.set_name("User1");
  User user2(user1);
  cout << "User1 id " << user1 .get_id()
       << ", User1 name " << user1 .get_name() << endl;
  cout << "User2 id " << user2 .get_id() 
       << ", User2 name " <<user2 .get_name() << endl;
  cout.flush();
  user1.set_id(2);
  user2.set_name("User2");
  cout << "User1 id " << user1.get_id() 
       << ", User1 name " << user1 .get_name() << endl;
  cout << "User2 id " <<user2.get_id()
       << ", User2 name "<<user2 .get_name() << endl;
  cout.flush();
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14