Common Errors and Mistakes in Object Oriented Programming C++

Written by Ali on. Posted in C++

Hello everyone. This is our second post to the common C++ errors and mistakes series. Today, we’ll get to explore some more mistakes that many programmers make, rather unintentionally! These common errors would be in the object oriented paradigm as programmers use it more frequently and get used to it with more effort and hard work. 

Class Implementation Common Errors

Many times, when using classes in program, one tends to write the headers to the functions in the class first and implement them later in the .cpp file. Usually, one may come across very odd and unexplainable yet common errors. As in this case it happens to be the syntax error and the program wouldn’t compile either, adding further difficulty of debugging the program, without actually using the debugger! It is a common mistake declaring a header to a function in the class but not writing the implementation of it in the .cpp file. The compiler locates the header but when it couldn’t find the implementation, it continues its search throughout the .cpp file containing implementations, and when it can’t find it, then comes the cryptic error. Thus it is necessary to provide the implementations of all the headers in the class with the exact function prototypes as in the header file.

Const Class Object Error

In some programs, that requires a large entry of information in class objects, you’d normally tend to make a function of it to increase the programming efficiency and decrease the programming effort. As we have learnt, object orientated paradigm is a very useful way of organizing and collecting data and can prove to be very useful if carefully administered. In this case, suppose you have made the following function, passing only the object as a parameter.

<p>. void entry (const data &amp; obj) ;</p>

Well, according to any ordinary programmer, this might seem to be a good prototype to the function; in reality IT ISN’T! A function that inserts data into a class object is receiving a constant object as a parameter. This is a syntax error. Doing so, you’ll not at all be able to write the content in the object. This’d lead to a read only situation, and generation of such a code that is really hard to debug, as the debugging process has to be done manually since the code can’t be compiled as it is a syntax error. For small programs, one might find it after a little effort, but in codes where there are multiple classes and polymorphism taking place at a higher level, it can become hell to get to the root of the error source.

The correct way to do it would be:

<p>. void entry(data &amp; obj)</p>


Public Keyword in Class

This can be one serious error in programming as by just forgetting to insert one keyword, you can get almost all the errors there are In C++! Forgetting to enter the public keyword, the class recognizes the whole data to be private and deals with it accordingly. You are required to make getters and setters of the data as only the class will be able to deal with the private data members, not anything outside. Be wary of this error as it can really send you off knocking with that myriad list of errors you’ll get!

So these were some of the most common errors in object oriented programming in C++. Be prepared next time for some more errors with their solutions!!!

Tags: , , , , ,