oops unit ii

30
CS2203-OBJECT ORIENTED PROGRAMMING UNIT-II Constructors default constructor Parameterized constructors Constructor with dynamic allocation – copy constructor – destructors – operator overloading – overloading through friend functions – overloading the assignment operator – type conversion – explicit constructor. Constructors Definition: The constructor is a special member function whose task is to initialize the objects of its class. It takes class name as function name and with no return types. The constructor is invoked whenever an object of its class is created. What is the use of Constructor? The main use of constructors is to initialize objects. The process of initialization is automatically carried out by the use of a special member function called a constructor. It is a function used to initialize the data of an object of a class. 1. Its name same as class name. 2. It cannot return anything, even void. 3. A class can have more than one constructor with different parameter list. 4. Default constructor has no parameters. Constructors called automatically by new operator when class object is declared. General Syntax for Constructor Constructor is a special member function that takes the same name as the class name and it wont have any return type even void. The constructor is automatically invoked when an object is created. Constructors used to initialize the elements of a class at the time of object declaration itself. The various types of constructors are 1. Default constructors 2. Parameterized constructors 3. Copy constructors Default Constructor: ** This constructor has no arguments in it, called default constructor. Default Constructor is also called as no argument constructor. The constructor is automatically invoked when an object is created. We cannot invoke constructors with objects of the class. Default constructors are used to set initial value for the corresponding class elements. 1 Syntax: classname(argument_lis Syntax: classname();

Upload: gracia

Post on 07-Nov-2015

22 views

Category:

Documents


1 download

DESCRIPTION

OOPS

TRANSCRIPT

Constructors

CS2203-OBJECT ORIENTED PROGRAMMING

UNIT-II

Constructors default constructor Parameterized constructors Constructor with dynamic allocation copy constructor destructors operator overloading overloading through friend functions overloading the assignment operator type conversion explicit constructor.ConstructorsDefinition:

The constructor is a special member function whose task is to initialize the objects of its class. It takes class name as function name and with no return types. The constructor is invoked whenever an object of its class is created.What is the use of Constructor?

The main use of constructors is to initialize objects. The process of initialization is automatically carried out by the use of a special member function called a constructor.

It is a function used to initialize the data of an object of a class.1. Its name same as class name.2. It cannot return anything, even void.3. A class can have more than one constructor with different parameter list.

4. Default constructor has no parameters.

Constructors called automatically by new operator when class object is declared.General Syntax for Constructor

Constructor is a special member function that takes the same name as the class name and it wont have any return type even void.

The constructor is automatically invoked when an object is created. Constructors used to initialize the elements of a class at the time of object declaration itself. The various types of constructors are

1. Default constructors

2. Parameterized constructors

3. Copy constructors Default Constructor: **

This constructor has no arguments in it, called default constructor. Default Constructor is also called as no argument constructor. The constructor is automatically invoked when an object is created. We cannot invoke constructors with objects of the class.

Default constructors are used to set initial value for the corresponding class elements.

Special Characteristics of Constructors:

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, not even void.

4. They cannot be inherited.

5. They can have default arguments.

6. Constructors cannot be virtual.

7. We cannot refer their addresses.

8. They make implicit calls to new and delete operators when memory allocation and deallocation is required.For example: #include

#include

class complex

{

float real,imag;

public:

complex()

//Default Constructor

{

real=10;

imag=0.5;

}

void display()

{

cout