OODP UNIT 02
1. CONSTRUCTOR & TYPES:
• A constructor is a special member function whose task is to initialize the objects of its class.
• It is special because its name is same as the class name.
• The constructor is invoked whenever an object of its associated class is created.
• It is called constructor because it constructs the values of data members of the class.
• The syntax generally is as given below :
<class name> { arguments} ;
CHARACTERISTICS OF CONSTRUCTORS:
• They should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types, not even void and they cannot return values.
• They cannot be inherited, though a derived class can call the base class constructor.
• Like other C++ functions, Constructors can have default arguments.
• Constructors can not be virtual.
• We can not refer to their addresses.
• An object with a constructor (or destructor) can not be used as a member of a union.
• They make ‘implicit calls’ to the operators new and delete when memory allocation is required.
TYPES OF CONSTRUCTORS:
• There are several forms in which a constructor can take its shape namely:
Default Constructor
Parameterized Constructors
Copy constructor
DEFAULT CONSTRUCTOR:
• A constructor that accepts no parameters is called the default constructor.
• The default constructor for class A is A : : A ( )
• When a constructor is declared for a class initialization of the class objects becomes mandatory.
Example:
class add
{
int m, n ;
public :
add (void) ;
//code
};
add :: add (void)
{
m = 0; n = 0;
}
1. CONSTRUCTOR & TYPES:
• A constructor is a special member function whose task is to initialize the objects of its class.
• It is special because its name is same as the class name.
• The constructor is invoked whenever an object of its associated class is created.
• It is called constructor because it constructs the values of data members of the class.
• The syntax generally is as given below :
<class name> { arguments} ;
CHARACTERISTICS OF CONSTRUCTORS:
• They should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types, not even void and they cannot return values.
• They cannot be inherited, though a derived class can call the base class constructor.
• Like other C++ functions, Constructors can have default arguments.
• Constructors can not be virtual.
• We can not refer to their addresses.
• An object with a constructor (or destructor) can not be used as a member of a union.
• They make ‘implicit calls’ to the operators new and delete when memory allocation is required.
TYPES OF CONSTRUCTORS:
• There are several forms in which a constructor can take its shape namely:
Default Constructor
Parameterized Constructors
Copy constructor
DEFAULT CONSTRUCTOR:
• A constructor that accepts no parameters is called the default constructor.
• The default constructor for class A is A : : A ( )
• When a constructor is declared for a class initialization of the class objects becomes mandatory.
Example:
class add
{
int m, n ;
public :
add (void) ;
//code
};
add :: add (void)
{
m = 0; n = 0;
}