; data structures part-6/1
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

data structures part-6/1

VIEWS: 10 PAGES: 12

  • pg 1
									UNIT VI
                   constructors
• A constructor is a ‘special’ member function whose
  task is to initialize the objects of its class.

• The name of the constructor is same as its class name.

• The constructor is invoked when an object of its class
  is created.
       Example using constructor
Class sample
{                    Main()
   int x ,y;         {
  public:              sample s;
                     }         Constructor is invoked
  sample();
}
sample:: sample()
{ x=12;y=10;
}
    Characteristics of constructor
1. They should be declared in the public section

2. They are invoked automatically when the objects
   are created.

3. They do not have return types

4. They can not be inherited
           Types of constructors
 Default constructor:
A constructor that accepts no parameters is called
  Default constructor.

 Parameterized constructor
 Copy constructor
         Parameterized constructors
- Constructor that takes arguments
- Initial values are passed as arguments
Ex:                              Sample:: sample(int a , int b )
class sample                     {
                                     x=a;
{                                    y=b;
     int x ,y;                     }
    public:
     sample( int a , int b);
}
Parameterized constructor is invoked using two ways:
1. By calling explicitly
Syntax:
Class name object =constructor name (values);
sample s1 = sample(10 ,20);

2 .By calling implicitly
 Class name object (values);
sample s( 10 , 20);
       Passing objects as function arguments

Like any other data type, an object may be used as a function
    argument.
Class sample                       Void sample::show(sample s)
                                   { x=s.x; }
{
     int x;
  public: void read();             Main()
            void show( sample s); {
 };                                  sample s1,s2;
                                      s2.read();
Void sample :: read()
                                    s1.show(s2);
Cin>> x; }                         }
                Copy constructors
• A copy constructor takes a reference to an object of the same
  class as itself as an argument.

Ex:
 class sam
{ ……..
    public : sam( sam & );
};
• A copy constructor is used to declare and initialize an object
   from another object.
sam s1(s2); or sam s1=s2;
Class sample
{
    int x , y ;                Main()
   public:                     {
     sample()                  Sample s1;
        {
           x=y=0; }            Sample s2( 3 ,4);
     sample ( int a , int b)
       {                       Sample s3(s2);
              x=a;
             y=b;
        }
      sample( sample & s)
        {
            x=s.x;
            y=s.y;
        }
};
Constructors with default values :
Default values may be passed to constructors

Sample (int , int =5);

To invoke this constructor
Sample s(10);

Constructors as inline:

The constructor functions can also be defined as inline functions

Class sam
{
int x;
   public:
    sam(int a)   // inline constructor
     {
   x=a;    }
};
                    destructors
It is used to destroy the objects that have been created by
constructor.
Like a constructor, the destructor is a member function whose
name is same as the class name but it is preceded by tilde
operator.

        ~sample() {               }

A destructor never takes any argument nor does it returns any
values.

The destructor will be invoked implicitly by the compiler upon
exit from program

								
To top