8.3: Class Contructors
- Page ID
- 128183
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
What is a class constructor and what is it for?
To answer those questions, we have to go back to "variable" first. Given a main() function as following:
int main()
{
int a;
a = 5;
........
}
In the program, the statement "int a;" declares an integer variable a, and the statement "a = 5;" assigns an initial value 5 to the variable a. We use two statements to declare and initialize an integer variable in this program. Actually, we can use a single statement to declare and initialize a variable at the same time, such as "int a = 5;"
Similarly, in the following program:
int main()
{
Rectangle rectangleA;
rectangleA.setLength(7);
rectangleA.setWidth(5);
.....
}
the statement "Rectangle rectangleA;" declares an instance of the class Rectangle, and then the statements : "rectangleA.setLength(7);" and "rectangleA.setWidth(5);" set the initial values for the length and the width of the rectangleA. Now the question is "Can we use a single statement that declares and initializes the values of the length and the width of an object of rectangle like we did with the integer variable?
The answer is "Yes", such statement should be looked like "Rectangle rectangleA(7,5);" However, we can't use such statement in our program now unless we add a special member function in the definition of the class Rectangle. We specify such function in the definition of the class Rectangle by using following statement:
Rectangle(double l, double w);
and then code this function in implementation section as following:
Rectangle::Rectangle(double l, double w)
{
length=l;
width=w;
}
The reason why we say it is a special member function is because of
- The name of this function must be the same as the name of the class
- Even this function has no return value, we do not place void in front of the name of the function.
- This function can only be invoked when we declare an object of Rectangle.
After add this member function to the header file Rectangle.h we can use following statement to declare (create) an object of Rectangle/an instance of the class Rectangle and initialize the length and the width of this rectangle at the same time:
Rectangle rectangleA(7,5);
This special member function is called "Class Constructor" To be more specific, we call this function "Non-default Constructor" In the following program, we use this statement at Line 7 to declare an instance of the class Rectangle called rectangleA and initialize the length and the width of ractangleA to 7 and 10,
Following is the screen shot of a test run of the program:

We can see the length and the width of the object rectangleA were initialized by 7 and 11 without using setLength() and setWidth() functions.
But once we add this member function to the header file, we can't use following statement in our program anymore:
Rectangle rectangleB;
which means we can't just declare an object without provide the initial values for the length and the width of the rectangle, and sometime it is inconvenient.
If we do want the statement "Rectangle rectangleB;" to be a valid statement, in other words, if we do want to allow to declare an object of Rectangle without providing the initial values for the length and the width of the rectangle, we can add another special member function, which is called "Default Constructor". This function will allow the the length and the width of a rectangle to take default values.
We specify such function in the definition of the class Rectangle by using following statement:
Rectangle();
and add following function definition into the header file in the implementation section:
Rectangle::Rectangle()
{
length=5;
width=5;
}
where 5 is the default value for the length and the width, and it can be set by you with any value you prefer.

In the following program, we can declare two instances of the class Rectangle at Line 7:

We declared rectangleA without providing initial value for its length and its width, thus rectangleA used the default constructor for setting its length to 5 and its width to 5. On the other hand, we declared rectangleB with initial values of its length and its width provided, thus rectangleB used it non-default constructor to set the length and the width to 10 and 7 respectively.
Following is a screenshot of a test run of this program:



