Default Objects for Arrays and Containers
Some Features of C++ Entities – Part 5
Forward: In this part of the series, I explain Default Objects for Arrays and Containers.
By: Chrysanthus Date Published: 26 Aug 2012
Introduction
Questions
If you create an array of ints without supplying any value to any array element, what values will the elements have? If you create a container of ints without supplying any value to any container element, what values will the elements have? If you create an array of a class without supplying any instantiated object to any array element, what objects will the elements have? If you create a container of a class without supplying any instantiated object to any container element, what objects will the elements have? The answer is that such an array or container will have default value or default object for each element.
The default value of an int is 0. The default value of a float is 0.0 (or just 0). The default object for an instantiated object is got from the default constructor (see below).
An Array and Container with Default Value of ints
The following program demonstrates that for an array of ints where no assignment has been made to any of the elements, the default value of each element is zero.
#include <iostream>
using namespace std;
int myArr[15];
int main()
{
cout << myArr[3];
return 0;
}
The following program demonstrates that for a vector of ints where no assignment has been made to any of the elements, the default value of each element is zero.
#include <iostream>
#include <vector>
using namespace std;
vector<int> myVtor(15);
int main()
{
cout << myVtor[3];
return 0;
}
If you declare an array with the object type of a class name, then all the objects in the array will be the default instantiated object. In this case the default instantiated object should be usable: meaning that the data members (properties) should have values. The following program illustrates this (read and try it):
#include <iostream>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
Calculator(int ident1 = 3, int ident2 = 4)
{
num1 = ident1;
num2 = ident2;
}
int add ()
{
int sum = num1 + num2;
return sum;
}
};
Calculator myArr[15];
int main()
{
int result = myArr[3].add();
cout << result;
return 0;
}
Note how the method (member function) of the class has been used in main with the array element.
#include <iostream>
#include <vector>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
Calculator(int ident1 = 3, int ident2 = 4)
{
num1 = ident1;
num2 = ident2;
}
int add ()
{
int sum = num1 + num2;
return sum;
}
};
vector<Calculator> myVtor(15);
int main()
{
int result = myVtor[6].add();
cout << result;
return 0;
}
Note how the method of the class has been used in main with the container (vector) element. Also note: A class name is an instantiated object type name. In a similar way, the (object) type name for 5 is int; the type name for 6.3 is float.
The default class constructor produces the default object in situations like those above. At this point you may be wondering how to determine the default constructor of a class. As for default fundamental objects, you already know some of them: you know for example that the default int is 0 and the default float is 0.0. The precise problem you may have is how to know the default instantiated object from a class.
Now, if there is only one constructor (function) in a class definition (description) then that constructor will produce the default object. So if the properties (data members) of the constructor are not initialized during instantiation, then the object instantiated will not be usable. In the situation where an array or a container has to be filled with default instantiated objects, there would be a problem with unusable instantiated objects. In my system, the program is not compiled and the compiler issues an error message. For an instantiated object to be usable as a default object, its data members (properties) should have been initialized.
It is possible to have overloaded constructors in a class. The default instantiated object is the one that results from the default constructor. The default constructor is the constructor that results in the initialization of all its data members (properties) during instantiation. Read and try the following code:
#include <iostream>
#include <vector>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
int num3;
Calculator(int ident1, int ident2, int ident3)
{
num1 = ident1;
num2 = ident2;
num3 = ident3;
}
Calculator(int ident1 = 3, int ident2 = 4)
{
num1 = ident1;
num2 = ident2;
}
int add ()
{
int sum = num1 + num2;
return sum;
}
};
vector<Calculator> myVtor(15);
int main()
{
int result = myVtor[6].add();
cout << result;
return 0;
}
This program has two overloaded constructors. The default constructor is the second constructor with initialization of all its data members. The initialization here uses default values.
If you are new to C++, then read this article all over again in order to properly understand it.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT