18

CHAPTER

Additional About ANSI
and TURBO C++

C
H
A
P
T
E
R

O
U
T
L
I
N
E
—• 18.1 Introduction
—• 18.2 Innovative Data Types
—• 18.3 New Typecasting Operators
—• 18.4 The Keyword explicit
—• 18.5 The Keyword mutable
—• 18.6 Namespace Scope
—• 18.7 Nested Namespaces
—• 18.8 Anonymous Namespaces
—• 18.9 The using Keyword
—• 18.10 Namespace Alias
—• 18.11 The Standard Namespace STD
—• 18.12 ANSI and TURBO C++ Keywords
—• 18.13 ANSI and TURBO C++ Header Files
—• 18.14 C++ Operator Keywords

18.1 INTRODUCTION

This chapter deals with various concepts that are not discussed so far including new improvements suggested by ANSI committee in C++ language. In addition, this chapter also contains information regarding additional keywords provided by Turbo C++. List of commonly useful header files is also given.

The aim behind improvement made by ANSI is to allow the programmer to develop real world application programs in a simple and easy way. The new characteristic added allows a programmer improved achievements in complicated conditions. It is also possible to make programs without using the new characteristics. However, with additional features, the source code will be reduced for complex problems and programs can be made more efficient and short.

18.2 INNOVATIVE DATA TYPES

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

(1) The bool data type

The bool is a keyword. The bool data type is used to store Boolean values i.e. true and false. The true and false are also keywords. C++ treats true and false as values. The default numeric value of false is zero and 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 below:

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 is done in same type

These bool data type variables can be used in mathematical expressions as given in the statement below.

int m =   true+2*5+t1;

When bool data types are used in expressions like above, they are automatically evaluated to integers. It is also possible to change by nature the data type pointers, floating, and integer values to 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

18.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 <<"
 t = "<<t;
  cout <<"
 f = "<<f;

  bool g=1;

if (g)

cout <<"
 true";
else
cout <<"
 false";
return 0;
}

OUTPUT
t = 1
f = 0
true

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

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

# include <iostream>

using namespace std;

int main ( )
{   bool t=true;
    bool h,f=false;

    cout <<"
 t = "<<t;
    cout <<"
 f = "<<f;

    int x= 4*t+5*f;
    h=x;

    cout <<"
 x= "<<x;
    cout <<"
 h= "<<h;
    return 0;
}

OUTPUT
t = 1
f = 0
x= 4
h= 1

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

In case, the variable of bool type is assigned a value more than 1, for example, bool x=2; then the value of x will be considered as true i.e. 1. The variable of bool type also supports increment ++ (postfix or pre fix) operation. However, it cannot support decrement (--) operation. When a bool type variable with 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.

(2) The wchar_t data type

The wchar_t character data type stores 16 bit long characters. The 16 bit characters are manipulated to symbolize the character sets of languages which contain characters more than 255. ANSI C++ also defines a character literal called as wide_character literal. It occupies two bytes in memory. The wide_character literal starts with the letter L, as given below.

L ‘ab’

It is known as wide_character literal.

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

typedef char whcr_t;

The following program demonstrates the use of wchar_t data type.

18.3 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.

18.3 NEW TYPECASTING OPERATORS

Typecasting operation is used to change a value from one type to another. This can be applied at a place where automatic conversion of data types is not done.

Typecasting

double b= (double)j   // C typecasting style
double a =double (d) // C++ typecasting style

Both the statements given above are correct and nothing happens wrong when applied in practical applications. New typecasting operators introduced by the ANSI/ISO committee are as follows:

(1) static_cast

(2) dynamic_cast

(3) reinterpret_cast

(4) constant_cast

(1) The static_cast operator

The static_cast operator is used for the conversion of standard data types. Using this operator, base class pointer can be converted to derived class pointer. Its syntax is as given below.

static_cast <data-type> object

The data_type indicates the target data type of the cast. The specified object is converted to new data type. The new syntax of typecasting can easily be placed in the code. Hence, the above keyword is frequently used by the programmer instead of old format.

18. 4 Write a program to perform typecasting using static_cast operator.

# include <iostream.h>
 int main( )
{
int k=65;
cout <<"
 size of k="<<sizeof(k);
cout <<"
 value of k="<<k;
double d=static_cast<double>(k);
cout <<"
 size of d="<<sizeof(d);
cout <<"
 value of d="<<d;
char c=static_cast<char>(k);
cout <<"
 c = "<<c;
return 0;
}

OUTPUT
Size of k=4
Value of k=65
Size of d=8
Value of d=65
c = A

Explanation: In the above program, the integer k is initialized with 65. The variable d is of double data type. The value of k is assigned to d using static_cast operator. The value of k is assigned to char data type variable c. The output displays the contents of the variable with the number of bytes occupied by them.

(2) The const_cast operator

The const_cast operator explicitly modifies the const or volatile of a variable. It is used in the following format:

const_cast <data-type> (object)

In this type of casting, our goal is to change const or volatile nature of the variable. Hence, target and source data types are identical. It is frequently applied for removing const attribute of a variable.

18.5 Write a program to convert constant to non-constant.

# include <iostream.h>

void main( )
{
    const int x=0;
    int *p=(int *)&x;
    p=const_cast<int*>(&x);
}

class data
{   private :
    int d;

public :
void joy( ) const
{
    (const_cast<data*>(this))->d=100;
}
};

Explanation: In the above program, address of constant variable is assigned to pointer of non-constant variable and this is done using the operator const_cast.

(3) The reinterpret_cast operator

The reinterpret_cast operator is useful when programmer wants to alter one type into dissimilar type. In practical applications, it is used to modify a pointer kind of object to integer kind or vice versa. It is used in the following format:

reinterpret_cast <data-type> (object )

18.6 Write a program to convert pointers to integers using reinterpret_cast operator.

# include <iostream.h>

void main( )
{
    int b=487;
 
    int *rp=reinterpret_cast<int*> (b);
    cout <<endl<<"rp="<<rp;
    rp++;
    cout<<endl<<"rp="<<rp;

    b=reinterpret_cast<int>(rp);
    cout<<endl<<"b="<<b;
    b++;
  cout <<endl<<"b="<<b;
}

OUTPUT

rp=0x000001E7
rp=0x000001EB
b=491
b=492

Explanation: In the above program, b is an integer type of variable and initialized with 487. The variable rp is a pointer and is initialized with the address of variable. The rp is increased and the value is increased with four. The output shows the values of the variable.

18.7 Write a program to convert void pointer to char pointer using reinterpret_cast operator.

# include <iostream.h>
# include <string.h>

void *getadd ( )
{
    static char text[50];
    return text;
}

void main ( )
{
    char *p=reinterpret_cast<char*>(getadd( ));
    //char *p=getadd( );
    strcpy (p," Well Come");
    cout<<p;
}

OUTPUT
Well Come

Explanation: In the above program, the function getadd( ) returns base address of array text [50] as a void address. The return type is void *. In function main( ), the obtained address is converted to character type using operator reinterpret_cast and assigned to character pointer p. The strcpy( ) function copies a string to pointer p. Finally the cout statement displays the text stored in pointer p.

(4) The dynamic_cast operator

The dynamic_cast operator is used to change type of an object during program execution. It is frequently used to typecast on polymorphic objects i.e. when the base class has virtual function. This operator casts the base class pointer to derived class pointer. The operation with dynamic_cast is also known as type-secure downcast. It is used in the following format:

dynamic_cast <data-type> (object)

The object must be a base class object. Its type is tested and altered. This always does valid conversion. It verifies that the casting is allowable at execution time. It returns null if typecasting is defective.

(5) RTTI using typeid operator

The runtime type information (RTTI) is a new improvement made by ANSI/ISO committee. The typeid( ) operator is used to obtain exact type of object or variable during program execution. This operator returns a reference to an object maintained by the system. This object identifies the type of argument.

The typeid( ) operator is used to identify the type of object or variable during program execution. The syntax is as given below.

char *object class = typeid (object) . name ( );

18.8 Write a program using typeid( ) to identify the type of object.

# include <iostream.h>
# include <typeinfo.h>

class one
{
  public:
    virtual void say( )
    {
    }
};

class two : public one
{};

class three : public one
{};

void main( )
{
one *o;
cout <<endl<<typeid(o).name( );

two *t;
cout <<endl<<typeid(t).name( );
}

OUTPUT
class one *
class two *

Explanation: In the above program, class one has one virtual function say( ). The class two is derived from class one. In function main( ), *o and *t are pointer objects of class one and two. The typeid( ) operator identifies the type of objects and displays the class name from which they are created.

18.9 Write a program to identify the type of variable using typeid( ).

# include <iostream.h>
# include <typeinfo.h>

void main ( )
{

int i;
cout <<endl<<typeid(i).name( );

float f;
cout <<endl<<typeid(f).name( );
}

OUTPUT
int
float

Explanation: In the above program, two variables int(i) and float(f) are declared. Using typeid( ) function their types are displayed. The output is int, and float i.e. type of variables (i) and (f).

18.4 THE KEYWORD explicit

The keyword explicit is used to declare constructors of a class explicit. When constructor is invoked with single argument, implicit conversion is carried out. In this operation automatic conversion is performed. The type of argument taken is modified to an object type. To avoid such automatic type conversion explicit keyword is used followed by constructor's name. The following program explains the use of keyword explicit.

      18.10 Write a program to declare explicit constructor and avoid automatic conversion.

# include <iostream.h>

class XYZ
{

int k;
public :
    explicit XYZ (int j)
    {      k=j; }
    void show  ( )
    {
       cout <<"
 k = "<<k;

    }
};

int main( )
{
    XYZ XY(545);
    // XYZ XY=450; invalid assignment
    XY.show( );
    return 0;
}

OUTPUT
k=545

Explanation: In the above program, the class XYZ is declared with one integer argument. The class also has one argument constructor followed by explicit keyword and one member function show( ). In function main( ), the statement XYZ XY(545) creates object XY and initializes the member variable k with number 545. The statement XYZ XY=450 is invalid because the constructor is explicit and does not permit automatic conversion. This statement is valid only when the class constructor is not declared explicit.

18.5 THE KEYWORD mutable

A member function or an object can be declared as constant using the keyword const. The constant data elements are not modified and the constant member functions can alter value of any variable. The keyword mutable is used when we need to declare constant objects partly i.e. a specific data variable is to be modified. Thus, the data variable that is to be modified is declared as mutable. The declaration is given below.

mutable int k;

Here, the variable k can be modified though its class or object is declared as constant.

18.11 Write a program to use mutable keyword and modify the value of variable.

# include <iostream>

using namespace std;

class XYZ
{
private :
  mutable int k;
public :

    explicit XYZ (int d) {        k=d; }

    void value( ) const  { k+=10;  }

    void show( ) const { cout <<"
 k= "<<k; }

);

int main( )
{
const XYZ x(75);
x.show( );
x.value( );
x.show( );

return 0;
}

OUTPUT
k= 75
k= 85

Explanation: In the above program, the class XYZ has one integer mutable member. The class also has value( ) and show( ) constant member functions. In function main( ), x is a constant object of class XYZ. When x is declared, mutable member k is initialized with 75. When the value( ) function is invoked it is incremented with 10. The show( ) function displays the values of the variables.

18.6 NAMESPACE SCOPE

C++ allows variables with different scopes such as local, global etc. with different blocks and classes. This can be done using keyword namespace introduced by ANSI C++. The C++ standard library is a better example of namespace. All classes, templates and functions are defined inside the namespace std. In previous programs we have used the statement using namespace std. This tells the compiler that the members of this namespace are to be used in the current program.

(1) Namespace Declaration

The namespace can be defined in the programs. The declaration of namespace is same as class declaration except that the namespaces are not terminated by semi-colon. It is declared in the following format:

Declaration of namespace

namespace namespace_identifier
{
  // Definitions of variable functions and classes, etc.
}

Example

namespace num
{
  int n;
  void show ( int k)      {  cout <<k; }
}

In the above example, the variable n and the function show( ) are within the namespace scope num. We cannot reach the variable m directly. To access the variable and initialize it with a value the statement would be as given below:

num :: n=50;

Here, n is initialized to 50. The scope access operator is used to access the variable. This method of accessing elements becomes embarrassing.

We can also access elements directly using the following declarations:

Accessing namespace

using namespace namespace_identifier   // directive statement
using namespace_identifier :: member   // declaration method

The first statement allows access to elements without using scope access operator.

The second statement allows access to only given elements.

Accessing namespace

using namespace num
n=10;          // valid statement
show(15);      // valid statement

using namespace :: n;
m=20;          // valid statement
show(14)       // invalid statement

18.7 NESTED NAMESPACES

It is also possible to declare nested namespaces. When one namespace is nested in another namespace, it is known as nested namespace.

Nested namespaces declaration

namespace NUM

{
  statement1;

namespace NUM1
{
int k=10;
}
statement2;
}

The variable k can be accessed using following statements:

Variable accessing statements

cout <<NUM::NUM1::K

              OR

using namespace NUM;
cout <<NUM1::k;

18.8 ANONYMOUS NAMESPACES

A namespace without name is known as an anonymous namespace. The members of anonymous namespaces can access globally in all scopes. Frequent use of such namespaces is to protect global members from same name classes among files. Each file possesses separate anonymous namespace. The following program explains the use of namespaces.

18.12 Write a program to create namespace, declare, and access elements.

# include <iostream>
using namespace std;

  namespace num
 {
    int n;
    void show ( )      {  cout <<"
 n = "<<n;  }
}
  int main ( )
  {
    num::n=100;
    num::show( );
  return 0;
}

OUTPUT
n=100

Explanation: In the above program, the namespace num contains one integer member n and one member function show( ). In function main( ), the variable n is initialized to 100. The members are accessed using scope access operator and the namespace name. The function show( ) displays the value of variable.

18.9 THE using KEYWORD

(1) Using Directive

The using keyword can be used for using declarations as well as using directives. The using directive provides access to all variables declared within the namespace. When using directive method is used, we can directly access the variable without specifying the namespace name. The following example illustrates this:

18.13 Write a program to create namespace, declare, and access elements. Use using directive method.

# include <iostream>
using namespace std;

  namespace num
  {
    int n;
    void show( )      { cout <<"
 n = "<<n; }
}

int main( )
{
    using namespace num;
    n=100;
    show( );

return 0;
  }

OUTPUT
n=100

Explanation: In the above program, the namespace name is created with one integer variable n and function show( ). The statement using namespace num allows direct access to members of the namespace. Hence, in function main( ), n=100 initializes n with 100 and show( ) displays the value of n on the screen.

(2) Using Declarations

In this method, the using keyword is optional. It is also possible to allow permission to access few members of namespaces directly outside the namespace. The program given next explains the above points.

18.14 Write a program to use using declaration method of namespaces and access variables.

# include <iostream>
using namespace std;
namespace num
{
    int n;
    void show( )
    {  cout <<"
 n = "<<n; }}
int main ( )
{
     //using namespace num;
     num::n=100;

     num::show( );

     return 0;
}

OUTPUT

n=100

Explanation: The above program is same as the last one. Only difference is that the namespace num is not included here. In order to access elements of namespace num, we need to precede the variable name with scope access operator and namespace name. The statement num::n=100 accesses the variable n and initializes it with 100. In the statement num::show( ), the function show( ) is invoked using the same syntax.

18.15 Write a program to declare nested namespace and anonymous namespace.

# include <iostream>
using namespace std;

namespace num
{
    int j=200;
  namespace num1    {    int k=400;  }
}

namespace  {    int j=500;  }

void main ( )
{   cout  <<"j = "<<num::j <<"
";
    cout  <<"k = "<<num::num1::k <<"
";
    cout  <<"j = "<<j <<"
";
}

OUTPUT
j = 200
k = 400
j = 500

Explanation: In the above program, num and num1 are two namespaces. The num1 is declared inside the namespace num. The last namespace defined is unnamed namespace. In function main( ), the members of the namespaces are accessed using scope access operator. The variable j is used in two different scopes.

18.16 Write a program to declare functions in namespace. Access the function in main( ).

# include <iostream>
using namespace std;
namespace fun
{
  int add (int a, int b)  { return (a+b);   }
  int mul (int a, int b); // prototype declaration
}

int fun :: mul (int a, int b) {   return (a*b);  }

int main( )
{
    using namespace fun;
    cout <<"
 Addition       : " <<add(20,5);
    cout <<"
 Multiplication : " <<mul(20,5);
    return 0;
}

OUTPUT
Addition          : 25
Multiplication : 100

Explanation: In the above program, two functions add( ) and mul( ) are declared in namespace fun. In function main( ), the statement using namespace fun allows us to access the elements of fun namespace directly. Two integer values are passed to functions add( ) and mul( ). They return addition and multiplication of numbers respectively.

18.17 Write a program to declare class in the namespace. Access the member functions.

# include <iostream>
using namespace std;

namespace clas_s
{
class num
{
  private :
    int t;
  public :
    num (int m) {     t=m; }
    void show ( )      { cout <<"
 t = "<<t; }
};
}

void main( )
{
    // indirect access using scope access operator
    clas_s ::num n1(500);
    n1.show( );

    // direct access using directive
    using namespace clas_s;
           num n2(800);
       n2.show( );
}

OUTPUT
t = 500
t = 800

Explanation: In the above program, class num is defined in the namespace clas_s. The class has one integer variable t and a member function show( ). The member function show( ) displays the contents of the variable. In function main( ), n1 and n2 are objects of class num. The data members are initialized using constructors. The members of class num are accessed with and without scope access operator. Both methods of accessing elements are explained in previous programs.

18.10 NAMESPACE ALIAS

Alias means another name. A namespace alias is designed to specify another name to the existing namespace. It is useful if previous name is long. We can specify a short name as alias and call the namespace members. The following program explains how alias is specified.

18.18 Write a program to specify alias to existing namespace.

# include <iostream>
using namespace std;

namespace number

{
     int n;
     void show( )
     { cout <<"
 n = "<<n; }
}
int main ( )
{
     namespace num=number;
     num::n=200;
     number::show( );
     return 0;
}

OUTPUT
   n=200

Explanation: In the above program, the namespace number is defined. In function main( ), its alias name is created as follows:

namespace num=number;

The num is another name given to the namespace number. The member of namespace number can be accessed using both the names i.e. num and number. The variable n is accessed using name num and the show( ) function is accessed using name number.

18.11 THE STANDARD NAMESPACE STD

We are frequently using the statement using namespace std. This statement inserts complete library in the current program. This is same as including a header file and the contents of header file is available in the current program. We can access all classes, functions, and templates declared inside this namespace.

using namespace std;

As mentioned earlier, the above method of specifying a namespace is called using directive. The above declaration makes possible to access all members of namespace directly. All header files also use the namespace feature. If we include header files and namespace together in the same program, the variable declared in the header file will be global. Instead of the above declaration, following declaration must be followed in order to prevent serious bugs. Consider the following program.

18.19 Write a program to access members of namespace std applying using declaration method.

# include <iostream>

int main ( )
{
int age;
std::cout<<"
Enter your age : ";
std::cin>>age;
std::cout<<"
 Your age is : "<<age;
}

OUTPUT

Enter your age : 24
 Your age is : 24

Explanation: In the above program, the l member of namespace std cannot be accessed directly. Consider the following statement:

std::cout<<"
Enter your age : ";

Here, the namespace name std is preceded before object cout. In the same way, cin object is accessed.

(1) Guidelines for Defining Namespace

(1) The namespaces are not terminated by semi-colon like classes.

(2) Declarations done outside the namespaces are considered as members of global namespace.

(3) Members of namespaces are defined inside the scope of namespace.

(4) The namespace definition cannot be defined inside any function. It must be global.

18.12 ANSI AND TURBO C++ KEYWORDS

ANSI C++ has introduced various new keywords according to new characteristics of the language. The following table describes keywords of both ANSI and Turbo C++.

Table 18.1 Keywords supported by ANSI and Turbo C++

images

(1) The Keyword pascal

The pascal keyword is used to declare a variable or a function using Pascal-style naming convention

Syntax:

pascal <data definition>;
pascal <function definition>;

In addition, pascal declares Pascal-style parameter-passing conventions when applied to a function header (first parameter pushed first; the called function cleans up the stack).

Examples:

int pascal x;
void pascal show(int x, int y);

18.20 Write a program to use pascal keyword and change the calling convention of C++ to pascal style.

# include <iostream.h>
# include <constream.h>

void main ( )
{

  clrscr( );
  void pascal show (int,int);
  int x=2;
  show (++x,x);

    }
void pascal show ( int x, int y)
{
cout <<"
 x="<<x <<" y="<<y;

}

OUTPUT
x=3 y=3

Explanation: We know that in C/C++, calling convention is from right to left i.e. right most variable is first pushed in the stack. In order to change this calling convention style with pascal style, the pascal keyword can be used. The pascal keyword is given in the prototype declaration and function declarator. When we pass argument to this function, the calling convention will be left to right. The calling conventions are described in chapter “C++ and Memory”.

(2) The Keyword cdecl

The keyword cdecl is used to declare a variable or a function in C-style naming convention and pushes arguments in stack in C-style parameter passing conventions.

cdecl <data definition> ;
cdecl <function definition> ;

Example:

int cdecl l x;
void cdecl  show(int x, int y);

18.21 Write a program to demonstrate the use of cdecl keyword.

# include <iostream.h>
# include <constream.h>

void main ( )
{

  clrscr( );
  void cdecl    show (int,int);
  int x=7;
  show (++x,x);

    }
void cdecl show ( int x, int y)
{
cout <<"
 x="<<x <<" y="<<y;
}

OUTPUT

x=8 y=7

Explanation: The above program is same as the previous one. Here, cdecl keyword is used to apply calling convention to C.

18.13 ANSI AND TURBO C++ HEADER FILES

(1) ANSI / ISO (“include”) Files

ANSI/ISO committee has introduced a new style for header files. The header file is now without extension .h.

Example

# include <iostream>
# include <vector>

In previous examples we have frequently used the above header files. However, the conventional method <iostream.h> is also valid. Few header files are renamed, for example, <limit.h> with <climit>, math.h> with <cmath>, <stdio.h> with <cstdio> and so on. Only few names are listed in Table 18.2.

Table 18.2 ANSI/ISO header file names

C header files C++ header files
<cstring> <typeinfo>
<ctime>   <string>  
<cstdlib> <new>     
<cassert> <defines> 
<cctype>  <sstream> 
<cerrno>  <ostream> 
<cfloat>  <iostream>
<climits> <ios>     
<cmath>   <iomanip> 

(2) Turbo C++ Header (“include”) Files

Table 18.3 contains few names of frequently used header files of Turbo C++. The list is very exhaustive. You can view the complete list of header files through help menu (contents option) present in Turbo C++ editor.

Table 18.3 Turbo C++ header files

iostream.h complex.h strstrea.h
constream.h fstream.h bcd.h
process.h generic.h string.h
stdlib.h iomanip.h values.h
math.h new.h float.h

18.14 C++ OPERATOR KEYWORDS

The C++ committee suggested keywords for operators such as &&, || etc. The keywords can be used in expressions instead of operators. Table 18.4 describes the C++ operator keywords.

Table 18.4 C++ operator keywords

Operator Keyword
xor_eq ^=
xor   
or_eq  |=
or     ||
not_eq !=
not   
compl 
bitor 
bitand
and_eq &=
and    &&

SUMMARY

(1) The ANSI C++ has added many new characteristics to the original C++. The new characteristics added allow improved achievements in complicated conditions.

(2) The ANSI C++ has introduced new data types to make better scope of standard data types in C++. These new data types are bool and wchart_t.

(3) ANSI C++ defined new cast operators, such as static_cast, dynamic_cast, reinterpret_cast, and constant_cast.

(4) The static_cast operator is used for the conversion of standard data types.

(5) The const_cast operator explicitly modifies the const or volatile of a variable.

(6) The reinterpret_cast operator is useful when a programmer wants to alter one type into basically dissimilar type.

(7) The dynamic_cast operator is used to change type of an object during program execution. It is frequently used to typecast on polymorphic objects.

(8) The typeid operator is useful to know the types of strange objects. During program execution we can obtain their class name.

(9) The keyword explicit is used to declare constructors of a class explicit.

(10) The keyword mutable is used when we need to declare constant object partly i.e. a specific data variable is to be modified.

(11) C++ allows variables with different scopes such as local, global etc. with different blocks and classes. ANSI C++ has added a new keyword namespace to declare a scope, which has global variables. The C++ standard library is the better example of namespace.

(12) The namespace can be defined in the programs. The declaration of namespace is same as class declaration except that the namespace is not terminated by semi-colon.

(13) It is also possible to declare nested namespace. When one namespace is nested in another namespace it is known as nested namespace.

(14) A namespace without name is known as an anonymous namespace. The members of anonymous namespaces can be accessed globally in all scopes.

EXERCISES

[A] Answer the following questions.

(1) Describe the new data types introduced by ANSI C++ standard committee.

(2) How the bool data type is useful?

(3) Describe const_cast operator with its use.

(4) What do you mean by dynamic_ cast and static_cast?

(5) What is namespace? How does it work?

(6) What is typeid operator?

(7) When are the explicit and mutable keywords useful?

(8) What are anonymous namespaces?

(9) Explain alias of namespace.

(10) List the additional keywords provided by Turbo C++.

[B] Answer the following by selecting the appropriate option.

(1) An unnamed namespace is one

(a) that does not have a name

(b) compiler gives the name automatically

(c) it is pre-defined

(d) none of the above

(2) The explicit keyword is used to declare

(a) constructors explicit

(b) functions explicit

(c) destructors explicit

(d) none of the above

(3) The mutable keyword is used to modify the

(a) constant object

(b) ordinary object

(c) volatile object

(d) none of the above

(4) The typeid operator is used to know

(a) type of the object and variable

(b) type of function

(c) return value of function

(d) none of the above

(5) An alias to existing namespace means

(a) another name to the existing namespace

(b) name of nested namespace

(c) a name given by the compiler to unnamed namespace

(d) none of the above

(6) Which of the following keywords is not a part of ANSI C++?

(a) far

(b) public

(c) static

(d) none of the above

(7) Consider the statement bool x=1. The value of variable x after increment operation will be

(a) 1

(b) 2

(c) 0

(d) –1

[C] Attempt the following programs.

(1) Write a program to explain the use of mutable and explicit keywords.

(2) Write a program to declare namespace with variables and functions. Use member of the namespace in the function.

(3) Write a program to define constant member function and constant object. Modify the contents of member variables. Use mutable keyword.

(4) Write a program to demonstrate the use of static_cast operator.

(5) Write a program to declare bool variables and display the values.

(6) Write a program to perform increment operation with variables of bool type and display the value.

(7) Write a program using typeid( ) to identify the type of objects and data types.

(8) Write a program to access members of namespace std by applying using declaration method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72