Constant Data Members and Objects

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.
  1. #include<iostream>
  2. #include<conio.h>
  3. 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.
  1. class Distance {
  2. int feet; float inches;
  3. public:
  4. Distance(int ft, float in): feet(ft), inches(in) {}
  5. void setDist() { cin>>feet>>inches;}
  6. void showDist() const { cout<<"Feet:"<<feet<<"-"<<"Inches:"<<inches<<"\n";}
  7. };
  8. void main() {
  9. int a,b;
  10. cout<<"Please Enter inches\n";
  11. cin>>a;
  12. cout<<"Please Enter feet\n";
  13. cin>>b;
  14. const Distance football(b, a);
  15. // football.setDist(); //Error; setDist not const
  16. football.showDist(); //OK
  17. }
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.
  1. Output:
  2. <img src="http://www.sourcecodester.com/sites/default/files/download/moazkh60/tutorial6_output1.png" width="550" height="300" alt="output" />
  3.  
  4. <strong>Second Example</strong>
  5. <c>
  6. #include<iostream>
  7. #include<conio.h>
  8. using namespace std;
  9. class aClass {
  10. int alpha;
  11. public:
  12. void nonConstFunc() {
  13. alpha=99; //OK to do so
  14. }
  15. /* void constFunc() const {
  16. alpha=99; //error; cant modify data
  17. }*/
  18. //if the above function is uncommented it will give an error
  19. };
  20. void main()
  21. {
  22. cout<<"The const member functions can not modify the nonconst data members\n";
  23. cout<<"So it has been proved that const member functions can modify only const data members\n";
  24.  
  25. }
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 output

Add new comment