Loading....
Coupon Accepted Successfully!

 

Innovative Data Types

The ANSI/ISO committee introduced new data types to enable a better scope of standard data types in C++. These new data types are bool and wchart_t.

The bool data type

The bool is a keyword. The bool data type is used to hold Boolean values, that is, true and false. The true and false are also keywords. The C++ treats true and false as values. The default numeric value of false is zero, and that of true is one. All conditional expressions return values of this type. For example, the expression 5>4 is true, and it returns one. The variables of these data types are declared in the following manner:

Declaration of bool variables

bool t1; // t1 is a variable of bool data type

t1 = true // t1 contains true (1) as value

bool f1 = false // declaration and initialization are
//done with same type

These bool data-type variables can be used in mathematical expressions as per the following statement:

int m = true+2*5+t1;

When bool data types are used in expressions such as the one above, they are automatically evaluated to integers. It is also possible to change by nature the data-type pointers, floating and integer values to the bool data type.

Initialization of bool variables

bool a= 1; // Variable a contains true value

bool b=0 // Variable b contains false value

bool c=1.2 // Variable c contains true value

 

21.1 Write a program to declare bool variables and display the values.

#include<iostream>

using namespace std;

int main()

{

bool t=true;

bool f=false;

cout<<“\n t=”<<t;

cout<<“\n f=”<<f;

bool g=1;

if (g)

cout<<“\n true”;

else

cout<<“\n false”;

return 0;

}

OUTPUT

t = 1

f = 0

true

 

Explanation: In the above program, t and f are variables of the bool data type. The variable t contains true, and f contains false. Values of these variables are displayed using the cout statement. The variable g is another variable of the bool data type and contains value 1, that is,true. The if statement checks the value of the variable g and displays the appropriate message.

 

21.2 Write a program to declare variables of bool data type and use in mathematical expressions.

#include<iostream>

using namespace std;

int main()

{ bool t=true;

bool h,f=false;

cout<<“\n t=”<<t;

cout<<“\n f=”<<f;

int x= 4*t+5*f;

h=x;

cout<<“\n x=”<<x;

cout<<“\n h=”<<h;

return 0;

}

 

OUTPUT

t = 1

f = 0

x= 4

h= 1

 

 

Explanation: In the above program, t and h are variables of the bool data type and contain the values true and false. The variable x is an integer variable. The variables t and f are used in mathematical expressions, and the result obtained is assigned to the variable x. The value of the integer variable x is assigned to the Boolean variable h. The output of the program is as given above.

In case the variable of the bool type is assigned a value more than 1, for example bool x = 2; here, the value of x will be considered true, that is, 1. The variable of the bool type also supports the increment ++ (postfix or pre-fix) operation. However, it cannot support the decrement (--) operation. When a bool-type variable with the value 0 (false) is incremented, its value becomes one. However, when a variable contains Boolean value one and if any attempt is made to increase it, its value remains the same.

 

21.3 Write a program to perform increment operation with variable of bool type.

#include<iostream>

using namespace std;

int main()

{

bool x=0;

++x;

cout<<x;

return 0;

}

 

OUTPUT

1

 

Explanation: In the above program, x is a variable of the type bool. It is initialized with the value 0 (false). When the variable x is incremented, it contains one (true). The increment operator can be applied either prefix or postfix.

The wchar_t data type

The wchar_t is a character data type that holds 16-bit-long/-wide characters. The 16-bit characters are manipulated to symbolize the character sets of languages that contain more than 255 characters. ANSI C++ also defined a character literal called as wide_character literal. It occupies two bytes in memory. The wide_character literal starts with the letter L, as follows:

L ‘ab’

It is known as wide_character literal.

The declaration of wchar_t in the header file stdlib.h is as follows:

typedef char whcr_t;

The following program demonstrates the use of the wchar_t data type.

 

21.4 Write a program to use wchar_t data type.

#include<iostream.h>

#include<stdlib.h>

#include<constream.h>

void main()

{

clrscr();

wchar_t c=’A’;

cout<<c;

}

 

OUTPUT

A

 

 

Explanation: In the above program, variable c is of type wchar_t and it is initialized with “A.” The cout statement displays the same on the screen.





Test Your Skills Now!
Take a Quiz now
Reviewer Name