INTRODUCTION TO C++
C++ is an Object Oriented Programming language. Initially named “C with classes”, C++ was developed by Bjarne Stroustrup at AT & T Bell laboratories in Murray Hill, New Jersey, U.S.A., in the year 1980. C++ is an extension of C with a major addition of ‘class’ construct – a feature of Simula67 language.
Since the class was a major addition to the original C language, Stroustrup called the new language ‘C with classes.” However, later in 1983, the name was changed to C++. The idea of C++ comes from the C increment operator ++, thereby suggesting that C++ is an augmented (incremented) version of C.
C++ language corrects most of the deficiencies of C by offering improved compile-time type checking and support for modular and object-oriented programming. The ultimate goal of C++ is to provide a language for the professional programmer that can be used to develop OOP software without sacrificing Cs efficiency or portability.
C++ is a superset of C. Most of what we already know about C applies to C++ also. Therefore, all C programs are also C++ programs. The Object Oriented features in C++ allows programmers to build large programs with clarity, extensibility and easy of maintenance.
Fundamental elements of C++:
Character sets are the basic building blocks of any programming language. In C++, the character set consists of upper and lower case alphabets, digits, special characters and white spaces. The alphabets and digits constitute the alphanumeric set.
The smallest individual units in a program are known as tokens. Tokens are formed using one or more characters from the character set. C++ has the following tokens:
- Keywords
- Identifiers
- Constants
- Operators
- Terminals
A C++ program is written using one or more tokens, white spaces, and the syntax of the language. As mentioned earlier, C++ is a superset of C and therefore most constructs of C are legal in C++, with their meaning unchanged.
Keywords:
Keywords are special words, which are predefined in a language for a specific purpose in a program. They are explicitly reserved identifiers that can’t be used as names for the program variables or other user-defined programming elements. Some of the keywords which are newly introduced in C++ are as follows:
Keywords | Purpose |
int, float, double, char | Primitive data types |
class, new, delete | Object creation / deletion |
private, public, protected | Scope specifiers in a class |
friend, inline, virtual | Qualifier for member functions |
throw, try, catch | Exception handling keywords |
for, do, while, switch | Control structure keywords |
Table : List of Keywords and their purposes
Identifiers:
Identifiers are names given by the user for the programming elements such as variables, symbolic constants, functions, arrays and classes. Each language has its own rules for naming these identifiers. The following points must be noted while forming an identifier in C++:
- Identifiers are formed using alphabets, digits and underscore characters.
- Every identifier must begin with an alphabet or underscore character.
- The maximum number of characters used for forming an identifier must not exceed 31.
- Since C++ is a case-sensitive language, same name with different cases are not equal. For instance, names such as rate, Rate, and RATE are treated as different identifiers.
It is a general practice to use lower and mixed case letters for identifiers. But keywords are always in lowercase letters.
Constants:
Constants are values that never change during the execution of a program. For instance, the value 10 is an integer constant, ‘A’ is a character constant and “C++ Programming” is a string constant. When such constants are named using identifiers, they become Symbolic Constants.
Symbolic constants are named constants that can be referred later in a program using the symbol used for their definition. There are two ways of creating symbolic constants in C++:
- Using the qualifier const.
- Defining a set of integer constants using the keyword enum.
Any value declared as const can’t be modified by the program in any way. Consider the following constant declaration in C++:
const int size = 10;
char name[size];
The above lines of code declare a symbolic constant size with value 10, which is referred in the second line for array declaration using the name size. The default data type for constants is integer (int). For e.g.,
const size = 10;
means
const int size = 10;
The named constants are just like variables except that their values can’t be changed.
Another method of naming integer constant is as follows:
enum{x, y, z);
This defines x, y and z as integer constants with values 0, 1 and 2 respectively. This is equivalent to:
const x = 0;
const y = 1;
const z = 2;
We can also assign values to x, y and z explicitily:
enum {x=100, y=200, z=300}
Such values can be any integer vales.