Constant members and Objects

Submitted by: 
Language: 

Constant members and Objects

In this part you will learn:
1. What are constant data members and functions
2. How to use them
3. Syntax in c++
4. Program
5. Output

Constant data members
Constant data members are the data members of a class which once initialized, cannot be changed during any part of the program. Constant data members are declared just like normal data member declaration, the only difference is that we add a keyword ‘const’ before it. Constant data members help the programmers to perform checking of a program, they declare the members constant which they don’t want to change during program execution.
Syntax

  1. class set
  2. {
  3. const int max_size;
  4. }

Constant Objects
Constant objects are the objects whose values cannot be changed after initialization. Like declaring a data member constant makes that particular data member constant, whereas declaring the whole object makes every member of the object constant.
Sometimes programmers need a class whose value shouldn’t change during the whole execution of the program.
One important thing about constant object is that constant objects can only be used in constant data functions. i.e. if we try to use constant objects in non-constant member functions, compiler will generate an error.
Syntax

  1. int main()
  2. {
  3. const Class_Name Object_Name(parameters_if_any) ;
  4. //everything else
  5. }

Program with constant data member
In this example I have defined the member function of a Class.
Basic Step:

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions.
Class

  1. class set
  2. {
  3. public:
  4. int *ele;
  5. const int max_size = 10;
  6. int size;
  7.  
  8. set(){
  9. cout << endl << "Default constructor called" << endl;
  10. size = 0;
  11. ele = new int[max_size];
  12. }
  13.  
  14. set(int m):max_size(m){
  15. cout << endl << "Overloaded constructor called" << endl;
  16. size = 0;
  17. ele = new int[max_size];
  18. }
  19.  
  20. ~set(){
  21. cout << endl << "Destructor called" << endl;
  22. if (size > 0)
  23. delete[]ele;
  24. ele = NULL;
  25. }
  26. };

In the above class “set “ I have made three member variables in which “ele” is a pointer to integer, size is the current size of array and max_size is a constant integer to store the maximum size of array made by pointer to integer “ele”.
In the default and overloaded constructors, an array is being allocated of size (max_size).
The point to observe is that the overloaded constructor is changing the value of max_size from default(10) to the value that will be passed in parameter. Later array will be allocated of that passed size.
Let us take a look at the main program
Main

  1. int main()
  2. {
  3. set a(5);
  4. set b;
  5.  
  6. //a.max_size = 10; //can't change the const value
  7. cout << endl << "Max size of a is: " << a.max_size;
  8. cout << endl << "Max size of b is: " << b.max_size << endl;
  9. system("pause");
  10. }

In the main, first we are declaring an object “a” of class set and passing 5 as a parameter, i.e. we are giving the value of max_size at the time of initialization. After that we are making another object of same class named “b”, this object will have the max_size 10 which is set as default.
In the commented part, we are trying to change the value of max_size of object “b” which is not possible as it is a constant data member. So the compiler won’t allow us to do so.
Finally we are printing the values of data member “max_size” of class “a” and “b”, as a output we will see their values.
Output
picture viewer


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.