Why enums are used in C++ programming?
An enum variable takes only one value out of many possible values. Example to demonstrate it,
An enum variable takes only one value out of many possible values. Example to demonstrate it,
It's because the size of an integer is 4 bytes.;
This makes enum a good choice to work with flags.
You can accomplish the same task using C++ structures. However, working with enums gives you efficiency along with flexibility.
This makes enum a good choice to work with flags.
You can accomplish the same task using C++ structures. However, working with enums gives you efficiency along with flexibility.
How to use enums for flags?
Let us take an example,
enum designFlags {
ITALICS = 1,
BOLD = 2,
UNDERLINE = 4
} button;
Suppose you are designing a button for Windows application. You can set flags ITALICS, BOLD and UNDERLINE to work with text.
Let us take an example,
enum designFlags {
ITALICS = 1,
BOLD = 2,
UNDERLINE = 4
} button;
Suppose you are designing a button for Windows application. You can set flags ITALICS, BOLD and UNDERLINE to work with text.
There is a reason why all the integral constants are power of 2 in above pseudocode.
// In binary
ITALICS = 00000001
BOLD = 00000010
UNDERLINE = 00000100
// In binary
ITALICS = 00000001
BOLD = 00000010
UNDERLINE = 00000100
Since, the integral constants are power of 2, you can combine two or more flags at once without overlapping using bitwise OR | operator. This allows you to choose two or more flags at once. For example,
When the output is 5, you always know that bold and underline is used.
Also, you can add flag to your requirements.
if (myDesign & ITALICS) {
// code for italics
}
Here, we have added italics to our design. Note, only code for italics is written inside the if statement.
You can accomplish almost anything in C++ programming without using enumerations. However, they can be pretty handy in certain situations. That's what differentiates good programmers from great programmers.
Also, you can add flag to your requirements.
if (myDesign & ITALICS) {
// code for italics
}
Here, we have added italics to our design. Note, only code for italics is written inside the if statement.
You can accomplish almost anything in C++ programming without using enumerations. However, they can be pretty handy in certain situations. That's what differentiates good programmers from great programmers.
C++ Classes and Objects
In this tutorial, we will learn about objects and classes and how to use them in C++ with the help of examples.
In previous tutorials, we learned about functions and variables. Sometimes it's desirable to put related functions and data in one place so that it's logical and easier to work with.
Suppose, we need to store the length, breadth, and height of a rectangular room and calculate its area and volume.
To handle this task, we can create three variables, say, length, breadth, and height along with the functions calculateArea() and calculateVolume().
However, in C++, rather than creating separate variables and functions, we can also wrap these related data and functions in a single place (by creating objects). This programming paradigm is known as object-oriented programming.
But before we can create objects and use them in C++, we first need to learn about classes.
In this tutorial, we will learn about objects and classes and how to use them in C++ with the help of examples.
In previous tutorials, we learned about functions and variables. Sometimes it's desirable to put related functions and data in one place so that it's logical and easier to work with.
Suppose, we need to store the length, breadth, and height of a rectangular room and calculate its area and volume.
To handle this task, we can create three variables, say, length, breadth, and height along with the functions calculateArea() and calculateVolume().
However, in C++, rather than creating separate variables and functions, we can also wrap these related data and functions in a single place (by creating objects). This programming paradigm is known as object-oriented programming.
But before we can create objects and use them in C++, we first need to learn about classes.
C++ Class
A class is a blueprint for the object.
We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
A class is a blueprint for the object.
We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
Create a Class
A class is defined in C++ using keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
class className {
// some data
// some functions
};
A class is defined in C++ using keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
class className {
// some data
// some functions
};
//For example,
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
return length * breadth;
}
double calculateVolume(){
return length * breadth * height;
}
};
//Here, we defined a class named Room.
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
return length * breadth;
}
double calculateVolume(){
return length * breadth * height;
}
};
//Here, we defined a class named Room.
The variables length, breadth, and height declared inside the class are known as data members. And, the functions calculateArea() and calculateVolume() are known as member functions of a class.
C++ Objects
When a class is defined, only the specification for the object is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, we need to create objects.
When a class is defined, only the specification for the object is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, we need to create objects.