Tuesday, February 15, 2011

What is deep copy constructor?

What is deep copy constructor?

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


Example:

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
 
class Student
{
public:
      Student(char* szName,int nAge)
      {
           m_szName =new char[20];
           strcpy(m_szName,szName);
           m_nAge=nAge;
      }
     Student(const Student &obj )
      {
           m_szName =new char[20];
           strcpy(m_szName,obj.m_szName);
           m_nAge=obj.m_nAge;
      }
      ~Student()
      {
           delete[] m_szName;
           m_szName=NULL;
           cout<<"Memory destroyed \r\n";
      };
      int m_nAge;
      char *m_szName;
};
 
int main()
{
     Student *ptr=new Student("John",12);
     cout<<"Name: "<<ptr->m_szName<<"\r\n"<<"Age: "<< ptr->m_nAge<<"\r\n";
     {
          Student obj( *ptr);
          cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";
          //obj scope ends and ~Student() will be called
     }
     cout<<"Name: "<<ptr->m_szName<<"\r\n"<<"Age: "<< ptr->m_nAge<<"\r\n";
     delete ptr;
     getch();
     return 0;
}

Result:

Name: John
Age: 12
Name: John
Age: 12
Memory destroyed
Name: John
Age: 12
Memory destroyed

What is shallow copy?

What is shallow copy?

Default copy constructor of compiler copies all the member variables
from source to destination object. This is called shallow copy
constructor.

Example:


#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Student
{

public:
     Student(int nAge)
     {
          m_nAge=nAge;
     }              
     int m_nAge;    
};
int main()
{
     Student obj(13);
     cout<<"Age1: "<< obj.m_nAge<<"\r\n";    
     Student obj1(obj);
     cout<<"Age2: "<<obj1.m_nAge<<"\r\n";
     getch();
     return 0;
}




Result:
Age1: 13
Age2: 13

Example 2:


#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
 
class Student
{
public:
      Student(char* szName,int nAge)
      {
           m_szName =new char[20];
           strcpy(m_szName,szName);
           m_nAge=nAge;
      }
      ~Student()
      {
           delete[] m_szName;
           m_szName=NULL;
           cout<<"Memory destroyed \r\n";
      };
      int m_nAge;
      char *m_szName;
};
 
int main()
{
     Student *ptr=new Student("John",12);
     cout<<"Name: "<<ptr->m_szName<<"\r\n"<<"Age: "<< ptr->m_nAge<<"\r\n";
     {
          Student obj( *ptr);
          cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";

          //obj scope ends and ~Student() will be called
     }
     //Will Crash here
     //because m_szName is already freed in destructor.
     // this is the problem with shallow copy
     cout<<"Name: "<<ptr->m_szName<<"\r\n"<<"Age: "<< ptr->m_nAge<<"\r\n";
     delete ptr;
     getch();
     return 0;
}




Result:

Crash!!!!!!!!!!!

What is copy constructor?

What is copy constructor?
In addition to providing a default constructor and a destructor, the compiler also provides a default copy constructor which is called each time a copy of an object is made. When a program passes an object by value, either into the function or as a function return value, a temporary copy of the object is made. This work is done by the copy constructor.

All copy constructors take one argument or parameter which is the reference to an object of the same class. The default copy constructor copies each data member from the object passed as a parameter to the data member of the new object.

Example:



#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Student
{
public:

     Student()

     {
          cout << "Default constructor\r\n";         
     }

     Student(int nAge)

     {

          cout<<"Overloaded Constructor Called \r\n";    

          m_nAge=nAge;

     }    
     Student(Student &a)
     {
          cout << "Copy constructor\r\n";
          m_nAge = a.m_nAge;         
     }

     int m_nAge;    

};
int main()

{
    
     Student obj(13);
     cout<<"Age: "<< obj.m_nAge<<"\r\n";
    
     Student obj1(obj);
     cout<<"Age: "<<obj1.m_nAge<<"\r\n";

    
     getch();
     return 0;

}





Result:

Overloaded Constructor Called
Age: 13
Copy constructor
Age: 13

Monday, February 14, 2011

Can destructor be overloaded?

Can destructor be overloaded?

A destructor can never be overloaded. An overloaded destructor would mean that the destructor has taken arguments. Since a destructor does not take arguments, it can never be overloaded.

What is destructor?

What is destructor?

A destructor is a special function member of a class that is automatically called when an object about to be deleted. It always has the same name as the class preceded by a ‘tilt’ ~ symbol and has no return type. It is used to free allocated memory used by an object. It takes no arguments.


Example:

 
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Student
{
public:
     Student(char* szName,int nAge)
     {
          m_szName =new char[20];
          strcpy(m_szName,szName);
          m_nAge=nAge;
     }
     ~Student()
     {
          delete[] m_szName;
          m_szName=NULL;
          cout<<"Memory destroyed \r\n";
     };
     int m_nAge;
     char *m_szName;
};

int main()
{
     {
     Student obj("Raj",13);
     cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";
     }

     Student *ptr=new Student("John",12);
     cout<<"Name: "<<ptr->m_szName<<"\r\n"<<"Age: "<< ptr->m_nAge<<"\r\n";
     delete ptr;

     getch();
     return 0;
}



Result:
Name: Raj
Age: 13
Memory destroyed
Name: John
Age: 12
Memory destroyed

What is overloaded constructor?

What is overloaded constructor?
An overloaded constructor is a constructor which takes arguments. It is also called parameterized constructor.

Example:

 
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Student
{
public:
     Student()
     {
          m_nAge=0;
          strcpy(m_szName,"");
     };
     Student(char* szName,int nAge)
     {
          strcpy(m_szName,szName);
          m_nAge=nAge;
     }
     int m_nAge;
     char m_szName[20];
};

int main()
{

     Student obj;
     cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";

     Student obj2("John",12);
     cout<<"Name: "<<obj2.m_szName<<"\r\n"<<"Age: "<< obj2.m_nAge<<"\r\n";

     getch();
     return 0;
}


Result:

Name:
Age: 0

Name: John
Age: 12

Sunday, February 13, 2011

What is constructor?

What is constructor?

A constructor is a class function with the same name as the class itself. It cannot have a return type and may accept parameters. It will be called after the memory allocation to the member variables. Constructor is used to set initial values to members variables.


Example:

 

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Student
{
public:
     Student()
     {
          m_nAge=0;
          strcpy(m_szName,"");
     };    
     int m_nAge;
     char m_szName[20];
};

int main()
{
     Student obj;
     cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";
     getch();
     return 0;
}



Result:
Name:
Age: 0

Difference between C and C++?

Difference between C and C++?
C is a structured programming language. C++ is object oriented programming language.

Programmers use functions/procedures to deal with larger program in C language. In C++ programmers construct a class and related member functions to deal with large class functionality.

C++ can have member functions of structures/classes. C does not support modularization of member functions.

C++ can hide/abstract member variables/functions by private or protected keyword. In C language structure members can not be hidden from outside world. C never support abstraction.

C++ has function and operator overloading/polymorphism feature. C language does not have polymorphism features.

C also does not support inheritance.