3 Overview of C++

Jyoti Pareek

epgp books

 

Every Programming language like any language has basic character set on which the entire language is built. Language C++ has character set same as that of C.

 

Character sets

 

The basic source character set consists of 96 characters: the space character, the control characters representing horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:14

 

a b c d e f g h i j k l m n o p q r s t u v w x y z

 

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 

0 1 2 3 4 5 6 7 8 9

_ { } [ ] # ( ) < > % : ; . ? * + – / ^ & | ∼ ! = , \ ” ’

 

Trigraph Sequences

 

Some characters from the C and C++ character set are not available in all environments. We can enter these characters into a C or C++ source program using a sequence of three characters called a trigraph. The trigraph sequences are:

 

??= # pound sign
??( [ left bracket
??) ] right bracket
??< { left brace
??> } right brace
??/ \ backslash
??’ ^ caret
??! | vertical bar
??- ~ tilde

 

The preprocessor replaces trigraph sequences with the corresponding single-character representation.

 

Escape Sequences

 

Nonprintable characters also known as execution characters can be represented by an escape sequence. Escape sequences are primarily used to put nonprintable characters in character and string literals. For example, you can use escape sequences to put such characters as tab, carriage return, and backspace into an output stream.

 

The escape sequences and the characters they represent are:

Note: The line continuation sequence (\ followed by a new-line character) is not an escape sequence. It is used in character strings to indicate that the current line continues on the next line. We can use escape sequences only in character constants or in string literals. An error message is issued if an escape sequence is not recognized. In string and character sequences, when you want the backslash to represent itself (rather than the beginning of an escape sequence), you must use a \\ backslash escape sequence. For example:

 

cout << “The escape sequence \\n.” << endl;

This statement results in the following output:

The escape sequence \n.

 

Alternative representations of operators and punctuators

 

In addition to the reserved language keywords, the following alternative representations of operators and punctuators are also reserved in C and C++:

 

Alternative Operator Alternative Operator Alternative Operator
Representation /Punctuation Representation /Punctuation Representation /Punctuation
Represented Represented Represented
<% { and && and_eq &=
%> } bitor | or_eq |=
<: [ or || xor_eq ^=
:> ] xor ^ not !
%: # compl ~ not_eq !=
%:%: ## bitand &

 

Wide Characters

 

The C and C++ standard libraries include a number of facilities for dealing with wide characters and strings composed of them. The wide characters are defined using datatype wchar_t, which in the original C90 standard was defined as

 

“an integral type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales” (ISO 9899:1990 §4.1.5)

 

Both C and C++ introduced fixed-size character types char16_t and char32_t in the 2011 revisions of their respective standards to provide unambiguous representation of 16-bit and 32-bit Unicode transformation formats, leaving wchar_t implementation-defined. The ISO/IEC 10646:2003 Unicode standard 4.0 says that:  “The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers.”

 

Identifiers

 

An identifier is the user-defined name of a program element. In C++ identifier provides names for the following language elements:

 

·         Functions

·         Objects

·         Labels

·         Function parameters

·         Macros and macro parameters

·         Typedefs

·         Enumerated types and enumerators

·         C++ Classes and class members

·         C++ Templates

·         C++ Template parameters

·         C++ Namespaces

·         Struct and union names

An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). In C++there is no limit on the length of the identifer , but implementers often impose one

Keywords

 

Keywords are identifiers reserved by the language for special use. Following is the list of keywords common to both the C and C++.

Only the exact spelling of keywords is reserved. For example, do is reserved but DO is not.

 

The C++ language also reserves the following keywords:

                                                                                                            We will see their use in successive modulesData Types

 

Data type define the way you store the values used by and created by the program. Defining a data type means defining what constants (generally in terms of range) a variable of that data type can store and what operations can be performed on that value.

 

Data type can be built in or abstract. A built in data type compiler intrinsically understand. It is predefined and known to the compiler. Therefore we don’t need to define them , they are made available to us for use. As C++ is superset of C, most of the built in data types are inherited as is in C++, and they can be used as we use them in C. The basic scalar data type namely int, float and double along with their modifiers signed, unsigned, short and long can be used as we use them in C. Pointers in C++ also can be used the way we use them in C except few additions that we will discuss in next module.

 

Following is the complete list of fundamental data types in C++:

New Data types in C++

 

In addition to these built in data types inherited from C , another data type boolean was added to standard C++ . The keyword used for this data type is bool . The variables of bool data type can have two possible values – built in constants true (which converts to integer 1) and false ( which converts to integer 0 ).

 

Example :

 

bool found;

 

It can be assigned two values

 

found = true;

Or

 

found = false

Abstract Data type

 

Also C++ allows us to create our own data type, which with careful crafting can be made to look and behave quite similar to standard data type. They are also denoted as Abstract Data type. We will discuss about abstract data type in detail in modules 5.

 

Variable Declaration in C++

 

C and C++ are block structured programming languages. Scope and lifetime of the variables are determined by the block in which it is defined. The only difference being, that in C variables can only be defined in the beginning of the block , whereas in C++ variables can be defined anywhere in the block with only one restriction that they should be defined before their first use. For example

 

#include <iostream>

Using namesapce std;

void main(void)

{

int value1 ;
value1 = 10;
………………. //  some executable code here
……………….
int value2; //  this will not work in C but will work in C++
………………
{
int value 3; //  this will work in C and C++ both
………………..
……………..…
}
}

Storage Classes

 

Like C, C++ also has four storage classes namely auto, extern, register and static. Implementation of first three storage classes is similar to C. But static storage class has been extended in C++ for specific use with user defined data type. We will discuss this in detail in modules 6.

 

Operators in C++

 

Being superset of C, C++ has inherited all the operators from C. Apart from these operators, C++ has added new operators as well.

 

Here we will discuss following commonly used operators.

 

·         Scope resolution

·         New

·         delete

 

Discussion of other operators will require knowledge of other C++ constructs. We will discuss them in later modules as appropriate.

 

Scope Resolution Operator ::

 

We know that in C & C++. We can define the variables of the same name in different scope. For example have a look at the following program.

 

int value = 10;

 

void main(void)

{

int value = 20;

 

cout >> value;

}

 

Which value will be displayed ?

Yes you are right. The variable in the innermost scope will prevail and therefore 20 will be displayed. This is logical enough. But what if we want the global value to be displayed ? The answer to this is scope resolution operator ::

 

int value = 10;

 

void main(void)

{

int value = 20;

 

cout >> :: value;

}

 

Here 10 will be displayed.

 

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:

 

 

int value = 0;

 

void main(void)

{

int value = 0;

 

::   value = 1; // set global count to 1

value = 2; // set local count to

2 return 0;

 

}

 

The declaration of value declared in the main() function hides the integer named value declared in global namespace scope. The statement :: value = 1 accesses the variable named value declared in global namespace scope.

 

You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. We will discuss this in later modules.

 

New and Delete Operators

 

We have used malloc(), calloc() , realloc() and free() functions for program controlled dynamic memory allocation and deallocation . These functions can be used in C++ as well, but apart from these functions C++ also provides operators new and delete for same.

 

New Operator

 

The syntax of new operator is as follows

 

<data type>  *ptr   = new <data type>

For example

int *iptr = new int;

will allocate no of bytes needed to store integer and address of the allocated memory will be returned which we can store in a pointer to integer variable.

New operator can be used to allocate memory to scalar data type as well as float

float*fptr = new float ;

char*cptr = new char ;

We can initialize the allocated memory as follows

 

int *iptr = new int(10);

Here the memory allocated for integer will be initialized with value 10.

We can also get memory allocated for array as follows

 

int *array = new int [25];

this will allocate memory for 25 integers

int  *array = int[5][5];

 

this will allocate memory for 5 *5 two dimensional array.

 

Delete operator

 

Delete operator also has simple syntax

delete p;

this will release memory being ponted by pointer variable p.

 

The advantage of using new and delete operator over memory allocation function.

 

Use of new and delete operator is much more efficient and simple as compared to memory allocation functions

  • Function calls have executional overhead due to context switching. No such overhead is there with operators.
  • Syntax of new and delete operators is simple as need not pass size of data type and no type casting is needed.
  • Also new and delete can be overloaded to meet specific need of the application.

Conditional Structures

 

All the control structures such as

  • If and else
  • switch case
  • while loop
  • do-while loop
  • for loop

Work the same way as in C

 

you can view video on Overview of C++

Additional References

 

1) Stanley Lippmann, “C++ Primer”, Pearson Education.

2) Bjarne Stroustrup, “The C++ Programming Language”, Pearson Education.

3) Scott Mayer, “Effective C++”, Addison Wesley.

4) Bhushan Trivedi , Programming with ANSI C++, 2/e , Oxford University Press.

5) Yashavant P. Kanetkar “Let Us C++” , Bpb Publications.

6) Abhiram G. Ranade, “An Introduction to Programming through C++” , McGraw Hill

7) Ellis and B. Stroustrup “Annotated C++ Reference Manual”, http://www.stroustrup.com/arm.html

8) Herbert Schildt, “Complete Reference C++”, McGraw Hill Publications.

9) Ashok Kamthane, “Object Oriented Programming with ANSI and Turbo C++”, Pearson Education

10) E Balaguruswami, “Object Oriented Programming With C++”, Tata McGraw Hill

11) “C++ FAQs”, Pearson Education.