A field or member inside a class which allows programmers to save memory are known as bit fields. As we know a integer variable occupies 32 bits (on a 32-bit machine). If we only want to store two values like 0 and 1, only one bit is sufficient. Remaining 31-bits are wasted. In order to reduce such wastage of memory, we can use bit fields. Syntax of a bit field is as follows:
type-specifier declarator: width;
In the above syntax declarator is an identifier (bit-field name), type-specifier is the data type of the declarator and width is the size of bit field.
Following are the properties of bit-fields:
- C++ does not allow arrays of bit fields, pointers to bit fields, and functions returning bit fields.
- The declarator is optional and is used only to name the bit field.
- The address operator (&) cannot be used with bit fields.
- When a value that is out of range is assigned to a bit field, the lower-order bit pattern is preserved.
- Bit fields with a size of 0 must be unnamed.
Following program demonstrates bit field in a class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; unsigned int status: 1; //size of status field is 1-bit public: void set_status(int x) { status = x; } int get_status() { return status; } }; int main() { Student s1; s1.set_status(1); cout << "Status of student is: " << s1.get_status(); return 0; } |
This code defines a C++ class called “Student” that contains several private data members: “name”, “regdno”, “age”, “branch” and “status”. The “status” data member is defined using an unsigned int with a width of 1 bit, which means it can only take the values 0 or 1.
The class also has two public member functions: set_status() and get_status() which are used to set and retrieve the value of the status data member respectively.
In the main() function, an object of the Student class is created and its status is set to 1 by calling the set_status() function. Then the get_status() function is called to retrieve the value of the status data member and it is printed to the console using the cout statement.
The output of this program will be:
1 2 3 | Status of student is: 1 |
In the above program size of status field is only 1-bit. It can store either a 0 or 1.
Volatile Objects and Member Functions
An object which can be modified by some unknown forces (like hardware) other than the program itself can be declared as volatile. Compiler doesn’t apply any optimizations for such volatile objects. Syntax for declaring an volatile object is as follows:
volatile ClassName object-name;
A member function can be declared as volatile to make the access to member variables to be volatile. A volatile object can access only volatile functions. Syntax for creating a volatile function is as follows:
return-type function-name(params-list) volatile;
Following program demonstrates both volatile objects and volatile programs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; public: void set_name(string); void get_name(Student) volatile; }; void Student::set_name(string n) { name = n; } void Student::get_name(Student s) volatile { cout << "Name is: " << s.name; } int main() { Student s1; s1.set_name("Mahesh"); volatile Student s2; s2.get_name(s1); return 0; } |
This code defines a C++ class called “Student” that contains several private data members: “name”, “regdno”, “age” and “branch”. The class also has two public member functions: set_name() and get_name() which are used to set and retrieve the value of the name data member respectively.
The set_name() function takes a string as an input and assigns it to the private data member “name” of the class.
The get_name() function takes a Student object as an input and prints the value of the name data member of that object to the console using the cout statement.
In the main() function, an object of the Student class is created, and its name is set to “Mahesh” by calling the set_name() function. Then another object of the Student class is created with the volatile keyword and the get_name() function is called to retrieve the value of the name data member and it is printed to the console using the cout statement.
The output of this program will be:
1 2 3 | Name is: Mahesh |
In the above program volatile function is get_name() and volatile object is s2. The object s2 can access only volatile member functions.
Take your time to comment on this article.