What is a copy constructor? What is the need for it?

What is a copy constructor? What is the need for it? What is the shallow and deep copy? When it gets called? What is document-view architecture? Give me one real time example for SDI?

What is a copy constructor? What is the need for it?

Copy constructor is a constructor type which is of the same name as the class and it is used to constructs an object by copying the state from another object of the same class. It makes deep copy of objects because whenever an object is copied, another object is created and in this process the copy constructor gets called. It is called either when an object is created from another object type or when an object is passed by value as a parameter or when an object is returned from a function.

The syntax of it is as:
x::x (const x&)
where x is the object of the class being copied.

What is the shallow and deep copy? When it gets called?

Shallow copy is also known as member wise copy. In this if the copy constructor is not defined by the user then compiler itself defines the copy constructor and assignment operator and it also provides the copying method. This type copies all the member variable values but doesn’t work well with the variable which point to the dynamically allocated memory. Example of this is default copy constructor.
class cont
{
   int length;
   char *name;
   public:
   cont(){}
   cont(char *n)
   {
       length=strlen(n) + 1;
       name=new char[length+1];
       strcpy(name,n);
       name[length-1] = '\0';
   }
   void show()
   {
       cout<<"name::"<<name<<endl;
   }
   ~cont()
   {
       delete name;
   }
};
int main()
{
   cont tk("harris"),tk1(tk);
   cont tk2= tk;
   TestObj1.show();
   getch();
   return 0;
}

Deep Copy is also called as user-defined copy. In this copy user has to create its own copy constructor and overload assignment operator. It also copies all the member but it makes copies of dynamically allocated memory pointed to by the variables. In this a destructor is essentially required to delete the dynamically allocated memory.
class cont
{
   Same as above
   cont(const cont &s)
   {
       length=strlen(s.name) + 1;
       name=new char[s.length+1];
       strcpy(name,s.name);
       name[length-1] = '\0';
   }

   ~cont()
   {
       delete name;
   }
};
int main()
{
   cont tk("harris"),tk1(tk);
   cont tk2= tk;
   TestObj1.show();
   getch();
   return 0;
}

What is document-view architecture? Give me one real time example for SDI?

In this data management gets separated in these two classes. In this document stores the data, manages the data and updates multiple views of the data. The view displays the data and manages user interaction with it, including selection and editing. This architecture consists of four key classes:-
CDocument class supports objects used to store or control program's data.
CView provides the basic functionality for programmer-defined view classes. View renders an image of the document on the screen and readies it for the preview.
CFrameWnd provides frame around one or more views of a document.
CDocTemplate which creates the correct document, view, and frame window objects for that type.
SDI is also known as Single Document Interface which can open a document one at a time. The main application for this is Wordpad.
Explain pointer to the constant and constant pointer? Explain difference between them.
If you have a value which you don’t want to modify or let anyone else to modify then you make that value as constant. It also means that you don’t want to allow the value to be changed...........
Modal and modeless dialog box
Modal dialog box is the dialog box in which user response is required before continuing with the program........
Difference between critical section, mutex and semaphore.
A critical section in which the process may be changing common variables, updating table, writing a file and perform another function..........
Post your comment