Using Appropriate Constructor and Avoiding Programming Errors

Written by Raza on. Posted in C++

In Object Oriented Programming, some Programming Errors happen very often. They include the inappropriate call to the constructor, illegal initialization state of objects and unintentional assignments and errors, to name a few. Eliminating these programming errors is very important to proceed in the right direction in the program. I shall explain them step by step to you in this post. Kindly Have a look at the following code.  

</p>
<p>class Fraction<br />
{<br />
.  private :<br />
.   int num ;<br />
.   int den ;<br />
.   void Print()<br />
.   {<br />
.    cout &lt;&lt; num &lt;&lt; "/" &lt;&lt; den &lt;&lt; endl ;<br />
.   }<br />
.  public :<br />
.   Fraction (int n, int d)<br />
.   {<br />
.    if (n = d)<br />
.    {<br />
.     den = num = n ;<br />
.    }<br />
.    else<br />
.    {<br />
.     num = n ;<br />
.     den = d ;<br />
.    }<br />
.  }<br />
} ;</p>
<p>&nbsp;</p>
<p>int main ()</p>
<p>{<br />
Fraction f1 ;<br />
Fraction f2 (31, 4) ;<br />
f2.Print() ;<br />
}</p>
<p>

 

Now the typical question…Do you see any error in this code? Well you may figure out this time as there are many programming errors as well. I shall explain them to you one by one.

 

Appropriate Constructor:

In the first line of main function, you create an object f1. There is nothing mentioned after f1, so naturally compiler would look to call default constructor. But wait!! You have defined an overloaded constructor for your class but you have not defined a default constructor. Compiler would be unable to find the appropriate constructor to call and give an error. Compiler generates the  code for default constructor only when you do not define any constructor in the class yourself.

To avoid this programming error, either do not declare f1 like this or define a default constructor yourself.

Unintentional Assignment:

The overloaded constructor gets called when you create the second object f2. Inside the constructor,  you have placed a check for the condition when both denominator and numerator are same. Instead of writing the boolean operator of double equals (==), you have written a single assignment operator. Which means that the this condition would always be true. Such unintentional assignment is a stupid but a very common programming error. Always make sure that you check all those instances in your program where you have used this operator.

Secondly, you should always make the incoming parameters as const if they are not meant to be changed. In this case if you had made them constant, this assignment would have resulted in an error. Thirdly this check is unnecessary which you have placed. If the denominator and numerator are equal, let them be equal. Don’t fool yourself.

Illegal State Programming Errors:

This one is not a compiler or run-time error but this is a very serious logical error. You are passing the numerator and the denominator to the overloaded constructor which assigns them to the values of that object. Observe that you are passing ‘zero’ as the denominator. In real life mathematics, zero can never be the denominator of any mathematical fraction. But in your program, it is! Object Oriented Programming should match the real world exactly or as close as possible. This design of your program is not correct. You should ensure that your fraction does not assume an illegal state. Same is the case with the default constructor. To avoid an illegal fraction programming error, its pretty safe to make the denominator equals to one when the incoming denominator is zero.

Private Functions:

In the third line, you are calling the print function on f2. This again gives a compile time programming error because the print function is in the private interface. In the first place, you should move this function to the public part. A function is not made private except usually for one case. Its the case when that function is called from another member function which is public. Such a function is called a helper function and is designed for a specific task. Either call this private function from another public function or make it public itself.

After making all these corrections, this would become the code now.

</p>
<p>class Fraction<br />
{<br />
.  private :<br />
.   int num ;<br />
.   int den ;<br />
.  public :<br />
.   void Print()<br />
.   {<br />
.    cout &lt;&lt; num &lt;&lt; "/" &lt;&lt; den &lt;&lt; endl ;<br />
.   }<br />
.   Fraction ()<br />
.   {<br />
.    num = 0 ;<br />
.    den = 1 ;<br />
.   }<br />
.   Fraction (const int n, const int d)<br />
.   {<br />
.    num = n ;<br />
.    if (d == 0)<br />
.     den = 1 ;<br />
.    else<br />
.     den = d ;<br />
.   }<br />
} ;</p>
<p>int main ()<br />
{<br />
Fraction f1 ;<br />
Fraction f2 (31, 0) ;<br />
f2.Print() ;<br />
}</p>
<p>

I have found only these number of intentional and unintentional programming errors. These cause you undue head ache and waste your time. If you can find some more, it would be great if you post them in the comments.

Tags: , , , , ,