C ++ Fundamentals Objectives x History of C ++ x Identifiers x Escape Sequences x Keywords x Data types x Variables and constants x Operators x Input and output x Manipulators History of C++ : x C ++ evolved from C, which evolved from 2 previous programming languages, BPCL and B. x C++ an extension of C, was developed by Bjarne Stroustrup in early 1980s at Bell Laboratories. x C++ provides capabilities for object-oriented programming. Program : Write a program in C++ to print a message – Welcome to C++ // Program to display a message on the screen #include<iostream.h> int main( ) { cout << “ Welcome to C ++ ” ; return 0 ; // indicates the program ended successfully } Let us examine the various components of the above program Comments: Any set of characters following // indicates that remainder of the line is a comment. Programmers insert comments to document programs and improve program readability. Comments also help other people read and understand your program. Comments do not cause the computer to perform any action when the program is run. The C++ compiler ignores comments. Comments that begin with // are called single line comments because the comment terminates at the end of the current line. #include<iostream.h> : is a preprocessor directive. i.e. a message to C++ preprocessor. Lines beginning with # are processed by the preprocessor before the program is compiled. This specific line tells the preprocessor to include in program contents of input/output stream header file. It contains declarations for identifier cout and << operator. Header file <iostream.h> should be included at beginning of all programs that use input/output stream. Function main( ) : Functions are one of fundamental blocks of C++. C++ programs contain one or more functions, exactly one of which must be main(). C++ programs begin executing at function main( ) even if main( ) is not the first function in the program. Keyword int to the left of main( ) indicates that main( ) returns an integer value. An opening brace ( { ) marks beginning of the function body and the corresponding closing brace ( } ) the end of the function body. Program statements: programming. Line The program statement is the fundamental unit of C++ cout << “ Welcome to C ++ ” ; instructs the computer to print on the screen the string of characters contained between quotation marks. The entire line including cout , the << operator, the string and the semicolon is called a statement. Every statement must end with a semicolon ( also known as statement terminator) Output and input in C++ is accomplished with stream of characters. The identifier cout ( pronounced as ‘C out’ ) is a predefined object that represents the standard output stream in C++. Thus when the above statement is executed , it sends the stream of characters Welcome to C ++ to the standard output stream object – cout – which is normally connected to the screen. The operator << is called the stream insertion operator. The value to the right of the operator, the right operand , is inserted in output stream. return 0 : This is included at the end of every main() function. C++ keyword return is one of several ways to exit out of a function. The value 0 indicates that the program has terminated successfully. Screen cout << Variable Output with cout Identifiers: Identifiers are names of variables, functions, arrays, classes created by the programmer. Rules for naming identifiers x An identifier is a series of characters consisting of letters, digits, and underscores( _ ) that does not begin with a digit. x C++ is case sensitive – uppercase and lowercase letters are different. x Key words or reserved words cannot be used as identifiers x C++ allows identifiers of any length, but your system and/or C++ implementation may impose some restrictions on the length of identifiers. Use identifiers of 31 characters or fewer to ensure portability. Escape Sequences Escape Sequence Description \n Newline. Position the cursor to the beginning of the next line \t Horizontal tab. Move the cursor the the next tab stop \r Carriage return. Position the screen cursorto the beginning of the current line; do not advance to the next line \a Alert. Sound the system bell \b Backspace. Move the cursor one position to the left \\ Backslash. Used to print a backslash character \” Double quotation marks. Used to print a double quote character \f Form feed Keywords in C++ Keywords common to C and C++ programming languages auto break case char const continue default do double else enum extern float for goto if int long short signed sizeof static struct switch typedef register return union unsigned void volatile while C++ only keywords asm bool catch class const_cast delete false friend inline mutable namespace new protected public static_cast template this typeid typename using virtual wchar_t throw dynamic_cast explicit operator private true try reinterpret_cast Data types Data is differentiated into various types. The type of data element restricts the data element to assume a particular set of values. The basic data types are x character (char) – A character in C character set x integer (int ) – An integer x float – a single precision floating point number x double – a double precision floating point number. In addition qualifiers short and long may be applied to same of above data types. Thus we have following additional data types. short int – abbreviated as short long int – also abbreviated as long long double - an extended double precision floating point number The qualifiers signed and unsigned may be applied to char, short, int, long unsigned variables can only have non-negative values, while signed variable can have both positive and negative values. In absence of explicit unsigned specification int , short, long are considered to be signed. Data type Description Typical memory requirements Range char Single character 8 bits -128 to 127 unsigned char Single character 8 bits 0 to 255 signed char Single character 8 bits -128 to 127 16 bits -32768 to 32767 16 bits 0 to 65535 16 bits -32768 to 32767 int unsigned int short int Positive and negative whole numbers including 0 Positive whole numbers including 0 unsigned short int 16 bits 0 to 65535 signed short int 16 bits -32768 to 32767 long int 32 bits signed long int 32 bits unsigned long int float double 32 bits Floating point number (number containing decimal point and / or exponent) Double precision floating point number (more significant figures and an exponent, which may be larger in magnitude. 32 bits 64 bits Variables and Constants Variable is an object or entity used by a program to store values in computation. Variables may change value during execution. All variables must be declared with a name and data type before they can be used in a program. Manipulators Manipulators are operators used with insertion operator << to format or modify the way data is displayed x endl : is an abbreviation for end line. This is a manipulator that causes the line feed to be inserted into the stream. endl is same as non-graphic character to generate line feed (\n). endl is an example of non parameterized stream manipulator and does not require the iomanip.h header file. // An example of endl #include<iostream.h> int main() { cout << “Hello ” << endl ; cout << “World ” << endl ; return 0 ; } Output: Hello World // An example of endl #include<iostream.h> int main() { int a = 20; cout << “a = ” << a << endl ; cout << “a = ” << a << “\n” ; return 0 ; } Output: a = 20 a = 20 x setw( n) : stands for set width. The setw manipulator is used to specify the minimum number of character positions on output field a variable will occupy. e.g. setw ( 4 ) specifies the next value output is printed in the field width of 4 spaces. If the value to be output is less than 4 character positions wide, the value is right justified in the field by default. On the other hand if the value to be displayed is more than 4 character positions wide field width is extended to accommodate entire value. // An example of setw #include <iostream.h> #include<iomanip.h> int main () { int a = 200 , b = 300; cout << setw ( 5 ) << a << setw(5) << b << endl ; cout << setw ( 8 ) << a << setw(8) << b << endl ; return 0; } Output: _ _ 200_ _ 300 _ _ _ _ _ 200 _ _ _ _ _ 300 <iomanip.h> : A header file under which declarations for manipulators are stored. x setprecision (p) : is used to control the number of digits to the right of the decimal point. The default precision is 6. // An example of setprecision #include <iostream.h> #include<iomanip.h> int main () { float a = 5 , b = 3 , c ; c = a / b; cout << setprecision ( 1 ) << c << endl ; cout << setprecision ( 2 ) << c << endl ; cout << setprecision ( 5 ) << c << endl ; return 0; } Output: 1.7 1.67 1.66667