#13 #14-Constant Data Member,Member Function and Constant Objects in C++

Constant Member Function

Constant member function is a function of a class which has been made constant by adding a keyword “const” at the end of function name.

Constant member functions of a class can use the data members of a class but can’t modify or change them.

The constant member function of class which is defined outside the class definition should also use the keyword “const” while defining itself.

Constant Data Member

The data member is said to be constant data member when a data member is added a keyword “const” in front of its name.

There is an important point to remember about initializing the constant data member. And it is that the, constant data member can only be initialized by the help of constructor.

Did you get it? That the constant data member can only be initialized with the help of constructor ONLY and this is very important.

And further again remember that; inside the constructor body, the constant data members must be initialized with the help of member initializer list and not by assignments. It means that you can’t do “data=0″ inside the constructor.

Instead, you have to do something else. And that is to use the member initializer list. The member initializer list, is represented as”data(0)”. And that’s it. You have got the 100% theory related to this constant data member. So, let’s move ahead. Let’s do programming!

So, let’s discuss a programming problem related to this. The question is as follows.

Write a program that shows the concept of constant data member.

The source code is listed as below.

Its output will come as below.

And it is given as-:

So, let’s discuss what is actually going inside the program.

As we can see that, everything started with the int main function. First of all we created an object of class “constc” called c1. And passed the value 3 into it. As we passed the value 3 into it, it means that, we are invoking the parameterized constructor.

And after that, we invoked the default constructor with c2 object.

Then, after that, we displayed the value of object c1 with the help of display function.

Then, we tried to see the output of c2 function with the help of the function ret_value through c2.ret_value().

The above thing was just a simple scenario of the program. The more is yet to come.

Inside the class constc( its name is bit confusing, it doesn’t means a constant class!!! REMEMBER THIS). And, we made the data as constant data member. After that we initialized the constant data member of a class by the help of constructor as “constc():data(0). You may be thinking that, can’t we do data=0, here. No, you can’t. because this is the special case of programming in constant data member where we only work with constructor for initializing the values, and we use the member initializer list.

photo2.png

And, I hope that from the picture above, things get clear.

If not, you can discuss those things in our facebook page named “Thomas Alva Edison”.

As we can see in the above program, we created another constructor constc where we passed num inside it. And inside it, we initialized data=num.

So, ” int data” is constant data member here.

As we passed 3 into constc, parameterized constructor, then data=3 happened inside the class.

After that we wanted to display the value of object and it was displayed as 3. Just before it, we can see that we have invoked the default constructor c2, so that means that we will be getting the value of c2 as 0.

And, that’s it.

Check the program output as listed in the blog.

Also, for better remembering of this program, write this program, at least once. And then type into the Code::Blocks if you got some time to do it.

But, never read these tutorials, without taking note copy. Although, you will learn something that way, but that will be so temporary. I want all of you to excel C++ programming language in the same the students in some colleges do. Yes you can do it, you can do it online in this 21st century.

Constant Member Function

Constant member function is a function of a class which has been made constant by adding a keyword “const” at the end of function name.

Constant member functions of a class can use the data members of a class but can’t modify or change them.

The constant member function of class which is defined outside the class definition should also use the keyword “const” while defining itself.

I think I don’t need to say that the above figure shows the output of the program.

I have got mails from my readers that the output is unclear. I am surprised to hear that. I have actually took its screenshot and kept it here. And, till now, the photo is clear. I don’t know if its quality gets degraded when published. I will see how to resolve those issues. but for time being, you can try pasting the above code in code::blocks then check the output.

So, let’s discuss about this program.

Now, when I am saying the working of this program, you first need to understand that we are working for constant member function, so I will best try to show how constant member function is implemented in the above program.

Inside a class, we had created a function with keyword “const” at last. So, that means that the show_coordinate is a constant member function.

And inside the int main, as we can see, we called show_coordinate, a constant member function, through object c1.

As we studied in the theory above,i.e non constant objects of class can invoke the constant as well as non-constant member functions; but the constant objects can only invoke the constant member function of a class.

It is a same thing we had also discussed in static member function as; ns=s+ns; here (nc)object=(c+nc)functions.

So, I will try to elaborate it a bit through a figure. I am doing so hard to make you understand it, because till you don’t get that important thing, you will get nothing about the constant member function.

c++.png

So, in the above program, in the int main function, c1, a non constant object(which can invoke (c+nc) fxns), invoked the input() function. Likewise the same c1 can also invoke the constant member function, as c1.show_coordinate.

So, that’s it.

This is how we solve a program related to constant member function.

We encounter a new thing called constant objects above.

But, we still haven’t read that theory.

So, I am going to post that theory just below.

Constant Object

Constant object is a constant of user defined type, ie the constant of class.

Syntax-:

Const class_name object_name(parameter);

Class_name const object_name(parameters);

Now, we will discuss about how to invoke the constant objects of a class.

Non-constant objects of a class can invoke the constant as well as non-constant member functions of a class, whereas the constant objects can only invoke the constant member functions of a class.

It is almost same as above.

A point to remember-:

Constant objects of a class should always be initialized with the help of constructor.

ini.png

How can questions be asked on your examinations, about what we are studying in this post. Let’s see. And, those who are studying without college, as a self taught programmer; then also, don’t leave these questions. Look at them, if you find all the answers in the program, then it’s very easy for you. Still if you don’t find the solution in the post, then first do google, (ask teacher if you are in college), then; if still there is a problem, comment in the comment section and I will post the solution.

So, the questions are as follows.

Q1) explain the relation between the constant object and constant function with example.

Q2) Explain about constant member function and constant object with examples.

Q3) what do you mean by const member function of a class? How are const cast operator and mutable members used with const member functions? Write a meaningful program to illustrate the use of const member function and const data member.

So, let’s solve a programming problem related to it.

Q) Write a program to show the use of constant object.

The source code is as follows.

Its output will come as follows.

So, now let’s discuss the working of the above program.

Here, what we did was that, we made the show_constant as constant member function.

Then inside the main function, we made c2 as constant object and it can only access with constant member functions.

If we had done c2.input() and c2.display then we would have got many errors there. So, we didn’t do it.

This is the main theme of the program.

I thought that explaining about the title is more important than explaining the program sometimes.

So, this time, I have explained about the main main points rather than the whole program.

So, I hope this helps you.

I saw something on reddit, that I hope will motivate you. I am posting it here.

So, that’s it! 

Be the first to comment

Leave a Reply

Your email address will not be published.


*