Why do we use a Constructor?

Before we dig deep into what is a constructor, it is very important to talk about some basic terminology what are their differences. In the post, I will talk about property functions in a class and why do we want to use a constructor.

Have you ever think about what is the difference between an object and a class?

Difference between a Class and an Object

A Class is talking about the definition of an instance, a Design Blueprint of an object, where it involved data members and its assoicate set of functions.

For example, a class of a rectangle can have data members of an integer length and an integer breadth as its data member. Meanwhile, it can also have a function included such as an area, where returns the area of the rectangle.

An object is talking about an example of class, an instance of a class. You can understand it as a n instace created according to the defination of a class.

A very easy way to understand it is the photo of the car attached above where the words “car” is a design blueprint or the definition of a car, there are many cars in a different brand such as Nissan, Audi, e.t.c., they are objects of the class car.

As a software engineering mindset, we should be able to write and to design classes for anything in the world with a certain requirements. Data members in a class are talk about the properties of the object to design and the set of fucntions are representing the computation or the behavior of the object to create.

A class example of customers

When we declare variables (objects are also variable), we are declaring it in the stack, and functions in a class will not occupy any memory but data declare does. In Java, all object declarations are allocated in the Heap while C++ provides options for you to declare in the Heap or in the Stack.

Data Hiding

When we talk about data members in a class, as a user (programmer), we want to design and implement classes for another user (programmer) to use as well in a software project. It then comes a concept of data hiding that talks about what fields or functions should or should not be directly accessed.

In the most simple and general case without some special requirements, only functions should make as public and data should make as private because data should not be directly access it.

Whenever the user (programmer) is using your class as functions (API), it is your responsibility if that particular function goes wrong as the user is not allowed to access data members in your class directly. Consider the following class of a rectangle where the result leads to a wrong logical answer. In the view of a rectangle area, the value of the area should not be negative. Things might go wrong if data members can be accessed in the public declaration.

Property Functions

A better way to declare a Rectangle class is to use property functions, mutator, and accessor.

Mutator allows you to change the private data members in the class and Accessor allows you to access the private data members in the class.

Both getLength and getBreadth functions are accessor and the setLength and the setBreadth functions are Mutators.

What is a constructor? and why?

There is an OOP philosophy that is wrong if we only use the property function to get and set our data member functions. Imagine you purchase a car (consider as a class). We need to use the property function to get and set the color of the car by using the property function after we create an instance of the car object. From a design point of view, it means that we purchased a car with no color and we paint the color ourselves after we bought it back home. This does not make any sense! As the color of the car should be pre-defined in the factory when we ordered a car.

You never want to paint your own car right? It is going to be a mess.

A constructor is exactly the factory mentioned in the example above, that when an object of instance is created, it will not contains garbage values in its data member.

Default Constructor (Compiler provided)

Whenever an object is created, there must be a constructor in a class, and if we did not declare any constructor, the compiler would use a Default Constructor provided to create an object for you.

There is a constructor behind the code were provided by the compiler

Non-parameterized (User defined default) Constructor

If we defined our own Non-parameterized constructor, the default constructor will not generate by the compiler anymore.

Parameterized Constructor

Copy Constructor

What a copy constructor does is to create an object of instance by using a reference of another object. All data members will be copied from the referenced object to the new object to be created.

Conclusion

There are still many concepts we can further dig deep into it when we use a constructor. In this post, I have used a rectangle class and a car class to illustrate concepts involved in property functions and constructors, as well as the reason why we want to use a constructor instead of an accessor or mutator in some cases. In the next post, I will also mention the issue when we try to use a constructor to create an object in a case that it needs to allocate dynamic memory in heap.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store