developer9
Product Launch Expert
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2
1000 XP
Constant Data Members and Objects
In this tutorial you will learn:
1. What are Constant Data Members?
2. What are Constant Objects?
3. Why to use Constant Data Members and Objects?
4. How to use Constant Data Members and Objects?
5. C++ syntax
What are Constant Data Members?
The data members that are once initialized and can never be changed are called constant member functions. Constant member functions are just like normal data members, only ‘const’ key word is added before writing the data type of the data member of the class. Constant data members allow the compiler to optimize code and error checking. Even a constructor cannot change the value of constant data member. Constructors and destructors can never be constant.
What are Constant Objects?
Constant objects are those which are initialized using constructor and constant member functions can bring change in the constant objects. Only constructor is a non-constant member function that can affect the object. If a non-constant member function is called by a constant object the compiler gives an error.
Why to use Constant Data members and Objects?
When we declare data members in the class, we may need to have some data members (in all the objects) to have a predefined value that can never be changed, so for this purpose we use constant data member, which cannot be changed throughout the scope of the program.
In object oriented programming as many of the objects are made, sometimes we want that the object should not change the values from which it was initialized. So to prevent any change we make that object a constant object by using keyword ‘const’.
How to use Constant Data Members and Objects?
If we want to declare a constant data member we simply add a key word ‘const’ before the data type of the data members as shown here in example:
For Example:
Here I am making a constant data member ‘football’ so we write its as
Class sports{
private:
const int football;
//here we write other data members
Now to declare the constant objects , for this purpose we use the ‘const’ keyword before writing the class of the object.
Example:
Int main{
const sports obj;
//all other main here.
First Example:
strong>Constant Data Members
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
I have made this program for the demonstration that the data members of constant objects cannot be changed hence this program gives an ERROR if we uncomment the second last line of the code.
These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.
In this program we have made a class named ‘Distance’ with two data members feet and inches.Then we made the constructor of the class. The setter and getter are defined by the names ‘setDist’ and ‘showDist’ respectively. It must be noted here that showDist() function is a constant function because it has a key word ‘const’ in it.
In the main of the program we have declared a constant object ‘football’ and in the next line(that is commented) we have tried to set the values of the data members of the object football but it gives error because once a constant object is formed,then its values cannot be changed.
In the above code it has been demonstrated if the non const member function acts on the non constant member function it is fine to do so, but if a non constant data member is being changed by the constant member function , an error pops up.
strong>Constant Data Members
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
Execute > compile
then Execute > run
Output
Book traversal links for Constant Data Members and Objects
Download
In this tutorial you will learn:
1. What are Constant Data Members?
2. What are Constant Objects?
3. Why to use Constant Data Members and Objects?
4. How to use Constant Data Members and Objects?
5. C++ syntax
What are Constant Data Members?
The data members that are once initialized and can never be changed are called constant member functions. Constant member functions are just like normal data members, only ‘const’ key word is added before writing the data type of the data member of the class. Constant data members allow the compiler to optimize code and error checking. Even a constructor cannot change the value of constant data member. Constructors and destructors can never be constant.
What are Constant Objects?
Constant objects are those which are initialized using constructor and constant member functions can bring change in the constant objects. Only constructor is a non-constant member function that can affect the object. If a non-constant member function is called by a constant object the compiler gives an error.
Why to use Constant Data members and Objects?
When we declare data members in the class, we may need to have some data members (in all the objects) to have a predefined value that can never be changed, so for this purpose we use constant data member, which cannot be changed throughout the scope of the program.
In object oriented programming as many of the objects are made, sometimes we want that the object should not change the values from which it was initialized. So to prevent any change we make that object a constant object by using keyword ‘const’.
How to use Constant Data Members and Objects?
If we want to declare a constant data member we simply add a key word ‘const’ before the data type of the data members as shown here in example:
For Example:
Here I am making a constant data member ‘football’ so we write its as
Class sports{
private:
const int football;
//here we write other data members
Now to declare the constant objects , for this purpose we use the ‘const’ keyword before writing the class of the object.
Example:
Int main{
const sports obj;
//all other main here.
First Example:
strong>Constant Data Members
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
I have made this program for the demonstration that the data members of constant objects cannot be changed hence this program gives an ERROR if we uncomment the second last line of the code.
- #include<iostream>
- #include<conio.h>
- using namespace std;
These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.
- class Distance {
- int
feet;
float
inches;
- public:
- Distance(
int
ft,
float
in)
:
feet(
ft)
,
inches(
in)
{
}
- void
setDist(
)
{
cin>>
feet>>
inches;
}
- void
showDist(
)
const
{
cout<<
"Feet:"
<<
feet<<
"-"
<<
"Inches:"
<<
inches<<
"\n
"
;
}
- }
;
- void
main(
)
{
- int
a,
b;
- cout<<
"Please Enter inches\n
"
;
- cin>>
a;
- cout<<
"Please Enter feet\n
"
;
- cin>>
b;
- const
Distance football(
b,
a)
;
- // football.setDist(); //Error; setDist not const
- football.showDist
(
)
;
//OK
- }
In this program we have made a class named ‘Distance’ with two data members feet and inches.Then we made the constructor of the class. The setter and getter are defined by the names ‘setDist’ and ‘showDist’ respectively. It must be noted here that showDist() function is a constant function because it has a key word ‘const’ in it.
In the main of the program we have declared a constant object ‘football’ and in the next line(that is commented) we have tried to set the values of the data members of the object football but it gives error because once a constant object is formed,then its values cannot be changed.
- Output:
- <
img src=
"http://www.sourcecodester.com/sites/default/files/download/moazkh60/tutorial6_output1.png"
width=
"550"
height=
"300"
alt=
"output"
/>
- <
strong>
Second Example</
strong>
- <
c>
- #include<iostream>
- #include<conio.h>
- using namespace std;
- class aClass {
- int
alpha;
- public:
- void
nonConstFunc(
)
{
- alpha=
99
;
//OK to do so
- }
- /* void constFunc() const {
- alpha=99; //error; cant modify data
- }*/
- //if the above function is uncommented it will give an error
- }
;
- void
main(
)
- {
- cout<<
"The const member functions can not modify the nonconst data members\n
"
;
- cout<<
"So it has been proved that const member functions can modify only const data members\n
"
;
- }
In the above code it has been demonstrated if the non const member function acts on the non constant member function it is fine to do so, but if a non constant data member is being changed by the constant member function , an error pops up.
strong>Constant Data Members
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
Execute > compile
then Execute > run
Output

Book traversal links for Constant Data Members and Objects
- ‹ Static Data Members in C++
- Up
- Arrays as Class Data Member ›
Download
You must upgrade your account or reply in the thread to view the hidden content.