Why do we 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.

A class example of customers

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.

Property Functions

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

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
Kevin Cheung

Kevin Cheung

𝑾𝒉𝒂𝒕 𝑰 𝒄𝒂𝒏𝒏𝒐𝒕 𝒄𝒓𝒆𝒂𝒕𝒆, 𝑰 𝒅𝒐 𝒏𝒐𝒕 𝒖𝒏𝒅𝒆𝒓𝒔𝒕𝒂𝒏𝒅. . An ordinary developer records his life and learning progress.