#7-Write a C++ program showing the uses of constructors with theory

Let everyone know that you\'re techy!!!

FacebookRedditEmail this page


Constructors and destructors in C++-:

Constructor-:

Constructor is defined as a special member function of a class which is used to initialize the data of a class. Did you get it? Why are constructors used? They are used to initialize the data of a class!!! Remember this by heart and it will be easy while you are writing the future programs.

It is called “special member function” of a class because it’s name is same as that of class name.

Now, let’s go below and discuss about the features of a constructor. By features, I mean to say about what are its properties, how to declare etc etc and so on.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

Characteristics of a constructor-:

  1. Constructor should be declared or defined in the public section of a class.
  2. Its name is same as that of class name.
  3. Constructors don’t have return type. That means they are not represented by using int constructor,float constructor. Even, we can’t use void return type with them. They are written simply by using their name only. And this is very important.
  4. We can invoke/call the constructor to do our programming. It is done by creating a object of a class.

Did you get it? Remember that to invoke a constructor, we simply create an object of a class; what we do? We simply create an object as-:

Class_name object_name;

And that’s it!!!

5) Like other C++, functions, constructors can have default arguments.

In the above five points, I have summarized almost everything that we are going to see in our upcoming programs.

Be ready to deal with constructors. We will discuss about the types of constructors, below.

So, there are 3 types of constructors. And they are as follows-:

  1. Default constructors
  2. Parameterized constructors
  3. Copy constructor

Don’t get confused by dynamic constructor. Suppose someone asks you what is dynamic constructor? Say, it is asked in your examination, then don’t get confused thinking that it was not in our syllabus.

Dynamic constructor is so simple. It is the name of constructor which comes into appearance when dynamic memory allocation is used.

It is similar to saying a football player. What is football player? He is an athlete who, when plays football, is called football player.

Compare athlete=constructor and

Football player=dynamic constructor

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

So, now let’s leave dynamic constructor and further discuss about the different types of constructors as listed above.

  1. Default constructor-:

The constructors where all values are initialized a default value is called default constructor. i.e it doesn’t accepts any value from the user. It shows the output same under all conditions.

So, we can also define default constructor as the constructor that do not take any arguments is called a default constructor.

Obviously, as the task of constructors is to initialize the data members; so is the task of default constructors. It helps to initialize the data members of a class with the same initial values.

So, let’s us discuss a program given below-:

#1 Write a program to calculate perimeter of a triangle using the concept of default constructor.

Its output will come like this-:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

SO,  the description of the above program is as below.

So, what is actually going inside this program. Let’s discuss that thing.

Know that everything starts from within a main function.

So, let’s go to int main().

At first, inside the main function, we have made 2 objects t1 and t2. These objects are of the class triangle.

Above, I said that to invoke the constructor, you have to create an object of it(look at point 4th).

That constructor function get invoked now. i.e the t1 object and t2 object will invoke the constructor inside the class. And what is the constructor here? It is the one with no return type (notice the point number 3 above).

After getting the initial values/default values from the constructor, the program flow will go to find t1.perimeter() and t2.perimeter().

I have discussed it in further detail just below-:

And after that we actually found out the perimeter of first and  second object. For that, what we did was

t1.perimeter()

and t2.perimeter()

So, we will be calculating the perimeter of the triangle using the default constructor. And this is very much important.

Do you now remember this? That we will be calculating the perimeter of the triangle using the default constructor, because the values of default constructor gets invoked just after creating objects t1 and t2(as in first), and then we call the t1.perimeter() which will find the perimeter. And this is very much important.

Inside that void perimeter(), we find that, firstly the perimeter p is declared as the sum of 3 sides as-:

p=side1+side2+side3;

Then, it shows the perimeter of the triangle right there as-:

The perimeter of the triangle is-blah…blah…

 

So, the next task for you is to solve a programming problem related to this constructors.

Homework for you-:

Find the volume of a cylinder using the default constructor. Use code::blocks for convenience.

You can solve the program and post it in the comment section below.

If there are problems in that program, then we will see that. You can ask it to us and we will provide the solution.

2) Parameterized constructor-:

A parameterized constructor is defined as a constructor which takes parameters or arguments.

Default constructor is always used to initialize same values for different objects. However, parameterized constructors are used for a different purpose than the default constructors.

i.e the parameterized constructors are used to initialize different values for different objects.

It is better to have a default constructor whenever we are working with a parametrized constructor.

A question may arise called, why so?

It is because, whenever object is created without passing any argument, in such a case error CAN occur.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

Now, let’s discuss an example program to calculate the perimeter of a triangle using the parameterized constructor.

Now, the output of the above program will come like as just below-:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

So, let’s discuss about how the program control flows. Let’s discuss how the program is being run here.

At first, the program will create an object of class triangle named t1 by passing the three arguments in it, namely, 2.5, 3.5, 4.5.

And similarly, t2 has 4.5,5.5, and 6.5.

Now, we will go towards calculating the perimeter from object t1; then through object t2 according as-:

t1.perimeter () and t2.perimeter ().

When we created an object triangle t1(parameter_a,parmeter_b,parameter_c), the parameterized constructor triangle gets invoked, and the program control goes there to initialize the sides as-:

side1=a;

side2=b;

side3=c;

as, we have already given the input a,b,c for object t1 as 2.5,3.5,4.5 and a,b,c for object t2 as 4.5,5.5,6.5. So, these values will get initialized accordingly and NEXT the program control will go towards the perimeter function. Inside that perimeter function, what we do is that to calculate the perimeter p as

p=side1+side2+side3;

The values of side1, side2 and side3 will be 2.5,3.5,4.5 for object t1, and similar case for t2. i.e the parameters that were passed in object making will go inside the triangle constructor(parameter_a,paremeter_b,parameter_c); and side1,side2 and side3 will get the respective values of a,b,c.

And then perimeter function will calculate the sum of all sides; given above.

Hence, in this way, we are done programming the parameterized constructor.

So, now let’s discuss about the copy constructor, which is the 3rd type of constructor in programming.

3) Copy constructor-:

Copy constructor is a constructor which can have reference object as an argument.

So, we can also define copy constructor in another way as just below. Please, don’t get confused when I say below, I mean to say just below this(not at below last).

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

The constructor that takes reference objects as an argument is called as copy constructor.

As usual constructors , the copy constructor is also used to initialize data members or objects of a class from another objects.

Now, let’s make our concepts of copy constructor strong by further diving into a new program that relates us to copy constructor.

Write a program with a class that represents complex having data members as real and imaginary of type float. Write default, parameterized and copy constructor in a class and display the state of object on the screen.

 

 

 

The output of the above program will come like below-:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

The above program will run as follows-:

Firstly, here the default constructor gets invoked by complexx c1.

Then, the parameterized constructors get invoked by complexx c2(4.5,5.5);

These are done only for fun! Yes, for fun only and the question is not asking us to do. We  did it so that we can understand the concept of default and parameterized constructor well through practice.

Then we invoke the copy constructor as complexx c3(c2).

After invoking that constructor, it will take us to-:

complexx (complexx &c)

{

//body as above

}

where, c2=4.5 and 5.5 will go in the form of ADDRESS OF C. Remember this is important. How will you visualize this. Let me create a figure to relate you about it.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

So, in this way, we also discussed about the copy constructor.

The post is going to be very long, and if you are still reading this upto here, I bet that you will become an excellent programmer. I guess, till now, only 1% of people are reading this, of all who started reading this article after finding from somewhere.

I hope the ABCD of constructors is now clear. Next, we will discuss about destructors in next article. Till then, I want to say good bye to you guys. ”

We will grow ourselves, then our society will be developed and the nation will become great again

Stay conscious, be rational.

Share this and have a good day!

Facebook

Be the first to comment

Leave a Reply

Your email address will not be published.


*