Constructors - Constructors Nancy Victor Assistant...

This preview shows page 1 - 10 out of 53 pages.

Constructors Nancy Victor Assistant Professor SITE VIT University Vellore
Image of page 1

Subscribe to view the full document.

Constructors 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.
Image of page 2
Constructor - example class add { int m, n ; public : add (void) ; ------ }; add :: add (void) { m = 0; n = 0; } When a class contains a constructor, it is guaranteed that an object created by the class will be initialized automatically. add a ; Not only creates the object a of type add but also initializes its data members m and n to zero.
Image of page 3

Subscribe to view the full document.

Constructors There is no need to write any statement to invoke the constructor function. If a ‘normal’ member function is defined for zero initialization, we would need to invoke this function for each of the objects separately. A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A : : A ( )
Image of page 4
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.
Image of page 5

Subscribe to view the full document.

Characteristics of Constructors They cannot be inherited, though a derived class can call the base class constructor. Like other C++ functions, Constructors can have default arguments.
Image of page 6
Characteristics of Constructors 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. When a constructor is declared for a class initialization of the class objects becomes mandatory.
Image of page 7

Subscribe to view the full document.

Parameterized Constructors It may be necessary to initialize the various data elements of different objects with different values when they are created. This is achieved by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
Image of page 8
Parameterized Constructors class add { int m, n ; public : add (int, int) ; ------ }; add : : add (int x, int y) { m = x; n = y; } When a constructor is parameterized, we must pass the initial values as arguments to the constructor function when an object is declared.
Image of page 9

Subscribe to view the full document.

Image of page 10

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern