Constant Data Members and Objects
Submitted by moazkhan on Tuesday, July 29, 2014 - 02:10.
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.- #include<iostream>
- #include<conio.h>
- using namespace std;
- 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
- }
- 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";
- }
Add new comment
- 356 views