74 subscribers
41 photos
13 links
C++ theory and programs

Owner :- @MistarAV

Instagram :- https://www.instagram.com/mistarav
Visit :- http://Codeskiduniya.blogspot.com
Download Telegram
In this program, a pointer variable ptr and normal variable d of type structure Distance is defined.

The address of variable d is stored to pointer variable, that is, ptr is pointing to variable d. Then, the member function of variable d is accessed using pointer.
Notes:

🔘Since pointer ptr is pointing to variable d in this program, (*ptr).inch and d.inch are equivalent. Similarly, (*ptr).feet and d.feet are equivalent.
🔘However, if we are using pointers, it is far more preferable to access struct members using the -> operator, since the . operator has a higher precedence than the * operator.

Hence, we enclose *ptr in brackets when using (*ptr).inch. Because of this, it is easier to make mistakes if both operators are used together in a single code.

ptr->feet is same as (*ptr).feet
ptr->inch is same as (*ptr).inc
C++ Enumeration
In this article, you will learn to work with enumeration (enum). Also, you will learn where enums are commonly used in C++ programming.

An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
Here, the name of the enumeration is season.

And, spring, summer and winter are values of type season.

By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if necessary).
enum season
{ spring = 0,
summer = 4,
autumn = 8,
winter = 12
};
Enumerated Type Declaration
When you create an enumerated type, only blueprint for the variable is created. Here's how you can create variables of enum type.

enum boolean { false, true };

// inside function
enum boolean check;


Here, a variable check of type enum boolean is created.
Here is another way to declare same check variable using different syntax.

enum boolean
{
false, true
} check;
Example 1: Enumeration Type
#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Output

Day 4
Example2: Changing Default Value of Enums
#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};

int main() {

seasons s;

s = summer;
cout << "Summer = " << s << endl;

return 0;
}
Output

Summer = 4
Why enums are used in C++ programming?
An enum variable takes only one value out of many possible values. Example to demonstrate it,
#include <iostream>
using namespace std;

enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;

int main()
{
card = club;
cout << "Size of enum variable " << sizeof(card) << " bytes.";
return 0;
}
Output

Size of enum variable 4 bytes.
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.
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.