Broad Network


Alternative Constructor Initialization in C++

Some Features of C++ Entities – Part 7

Forward: In this part of the series, I show you an alternative way to do initialization for contractor functions in C++.

By: Chrysanthus Date Published: 26 Aug 2012

Introduction

This is part 7 of my series, Some Features of C++ Entities. In this part of the series, I show you an alternative way to do initialization for contractor functions in C++.

Normal Initialization for Fundamental Objects
The following program shows the normal initialization for the int fundamental object, myInt:

#include <iostream>
using namespace std;

int myInt = 3;

int main()
    {
        cout << myInt;

        return 0;
    }

In this program, the assignment operator has been used for the initialization. You can initialize a fundamental object without using the assignment operator. In this case, you type the value in parentheses next to the identifier of the object. Read and try the following program that illustrates this:

#include <iostream>
using namespace std;

int myInt(3);

int main()
    {
        cout << myInt;

        return 0;
    }

In this program, the expression that does the initialization is:

    int myInt(3)

Normal Initialization for Constructor Function
The following program shows the normal way to do initialization for a constructor function of a class:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {
                num1 = ident1;
                num2 = ident2;
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };

int main()
    {
        Calculator myObject(4,5);
        int result = myObject.add();
        cout << result;

        return 0;
    }

Alternative Initialization for Constructor Function
The following program does exactly what the above program does. However, the initialization for the constructor is done differently. Read and try the code:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator() : num1(4), num2(5)
            {
                //no code needed
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };

int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

The expression for the initialization is:

    : num1(4), num2(5)

Here there are two data members (properties); you can have one or more than two data members; just separate them with commas (if there is more than one). The expression begins with a colon. The expression is typed between the function interface and the opening brace of the function definition. The names of the objects (identifiers) are the names of the public, protected or private members of the class.

Note: the parentheses of the function constructor in the class description are empty. In the instantiation of the object, the parentheses are not used.

That is it for this part of the series. We stop here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message