You are on page 1of 23
Introduction to C++ Languages High level language Instructions using English like keywords. Low level language Machine language Assembly language Instructions in the form of 0’s and 1’s. Instructions are written using symbolic names called pnemonics. Translator Software • Assembler – Converts Assembly language to Machine code. • Interpreter – Compiles one line at a time. If an error exist in a particular line, it stops and waits for the correction to be done and then only proceeds further. • Compiler – Compiles all lines together and shows the error at the end. Programming Paradigm Programming paradigm defines the methodology of designing and implementing programs using the key features and building blocks of a programming language. Structured / Procedural programming Object Oriented Programming • Procedures are given more priority. Data is given a back seat. • Data is given more priority and procedures take a back seat. • Follows top down approach in program design. • Follows bottom up approach in program design. • Donot support OOP concepts like data encapsulation , data abstraction ,polymorphism and inheritance. • Supports OOP concepts like data encapsulation , data abstraction , polymorphism and inheritance.  Founder of C++ - Bjarne Stroustrup (AT and T Bell Labs in 1980’s)  The design of C++ is based on 2 languages – C and Simula 67  Why C++ ?  Supports multiple programming styles.  Is portable.  Provision for creating user defined data types.  Allows compartmentalization of data (Hiding certain data and showing only necessary details.) C++ character set  Set of characters that a language can recognize.  C++ character set consists of the following:  Upper case letters (A to Z)  Lower case letters (a to z)  Decimal  Special  White  C++ digits (0 to 9) characters (Comma, Dollar , Brackets , Symbols etc) space characters (Blank space , Horizontal tab, Newline etc) is a case sensitive language. Tokens (Lexical units)  Smallest unit in a program.  Tokens are classified as  Keywords  Identifiers  Constants  Operators  Punctuators Keywords  Also called Reserved words .  Predefined words which has a specific meaning in the language.  Cannot be used as variable names.  They are case sensitive.  Examples of keywords  if  while  for  switch  do Identifiers  User defined name given to various programming elements like variables.  Rules for naming an identifier   Must start with an alphabet or underscore(_).  No special characters and white space characters allowed.  It can include uppercase letters , lowercase letters , digits and an underscore.  Keywords should not be used .  Case sensitive.  Identifiers starting with double underscore or single underscore followed by a capital letter should be avoided. C++ takes only the first 32 characters of a variable name. Constants / Literals  Data items that never change their value during program execution. Punctuators  C++ allows some special characters in its coding like brackets [], braces {}, parenthesis() ,semicolon ; , colon : etc . Operators  Predefined symbols which perform some computational works between variables, constants etc.  Some examples of operators are * , + , - etc. Types of constants Numeric constants Character Constants Boolean Constants Numeric constants Integer Constants Real / Floating point Constants • Numbers without any fractional part. • Numbers preceded by a + or – sign. • Divided into 3 types • Decimal • Octal • Hexadecimal • Numbers with a fractional part. • Numbers preceded by a + or – sign. • Must have one digit before and after the decimal point. • Can be expressed in exponential form. Character Constants Single Character Constants String Constants • Single printable or non printable character enclosed within single quotes. • Sequence of characters enclosed in double quotes. • Escape sequences • Non graphical characters which cannot be typed using a keyboard • Any character interpreted with a \ (backslash) is called an escape sequence. • Some examples of escape sequence • \n – newline • \t – tab • \b – backspace • \a - alarm • String constants always end with a delimiter ‘\0’ (null terminator). • Size of a string will be no of characters +1 (for ‘\0’) 1. State if the identifiers are valid or not:a) 29CLT b) _ABC c) My.Apple d) File23 e) Data-Rec f) zA23pe g) Break h) student_name 2. What kind of programming elements are the following:a) 13 b) A c) 4.324 d) SAM e) for f) sname g) { h) \’ i) * a) b) c) d) e) f) g) h) i) Integer Numeric Constant Character Constant Real Numeric Constant String Constant Keyword Identifier Punctuator Escape Sequence Operator 3. What kind of constants are the following:a) 14 b) O11 c) OX2A d) 70.654 4. State if the following character constant is valid or not a) ‘c’ b) ‘abc’ c) ‘/a’ d) “abc” e) ‘“‘ f) ‘\b’ g) ‘\’ 5. State the size of each string:a) “Apple” b) “DPS’s Score” c) “Wel\acome” d) “Ha/pp\ty” e) Pro12g’s is f) C++ a) b) c) d) e) f) 6 bytes 12 bytes 9 bytes 8 bytes 12 bytes 4 bytes Structure of C++ program 1 3 #include void main() 2 { C++ statements; } Preprocessor directives / Header files 4 • Programmers use various programming elements predefined in the C++ library. In order to include these elements in the program , appropriate header files need to be included in the program. • These statements are called as preprocessor directive , because they are processed by the preprocessor before the program compilation. • #include tells the compiler to insert another file into your source file. In effect the #include directive is replaced by the contents of the file specified. • #include is a file which contains declarations needed for statements used for C++ input and output. 2. void main() • First executable statement in C++ . Hence called driver function. • Only function recognizable by C++ compiler. All other functions are directly or indirectly called from main(). • The parenthesis after main indicate that main() is a programming element called function. • void is a data type (empty) and it refers to the type of value returned by the main() function. 3. Opening and closing braces delimit statements within it. 4. All C++ statements end with a semicolon(;). Comments  Pieces of code inserted by the programmer to enhance readability and understanding.  Ignored by the compiler.  Can be inserted anywhere in the program.  Not a C++ statement.  2 types of comments  Single line comment – Comments starting with // and ending in one line. Eg: // This is a C++ program  Multiline comment – Comments starting with /* and ending with */ Eg: /* This is a C++ program */ Input / Output Operations  Output Operator (<<)  Extraction operator/Put to operator/ Bit wise shift operator (<<) is used with cout.  cout treat data as a stream of characters and direct the value to the standard output device (screen/monitor) .  cout is included in the header file iostream.h , so #include should be included in the program.  Syntax: cout<<“Welcome to C++”;  Input Operator (>>)  Insertion operator/Get from operator is used with cin.  cin treat data as a stream of characters and reads a value from the standard input device (keyboard) .  cin is included in the header file iostream.h , so #include should be included in the program.  Syntax: cin>>a; where a is an identifier name. Cascading of input / output operators  Multiple use of extraction(<<) and insertion(>>) operation in one single cout or cin statement is called cascading.  Advantages of cascading   Improves readability  Reduces program size Example cout<<“Welcome to C++”<<“This is my first program”<<“I am learning to code”; This statement is the same as cout<<“Welcome to C++”; cout<<“This is my first program”; cout<<“I am learning to code”; Compilation of C++ program Source code (.cpp file) Preprocessor phase Source code with no header files Translation Object code (.obj file) • The program that you type in Edit window is called Source code. • Compilation takes place in 2 phases: • The preprocessor phase prepares the source code for translation by removing all header files and replacing with appropriate contents. • Translation phase converts HLL to machine language which can be executed by the computer. Linking and Execution of C++ program  Certain predefined programming elements like library routines are not present in the program.  They are stored in the form of object codes and must be linked with user written program ( program object code). This process is called Linking.  After the linking phase the executable file is created(.exe file). Types of errors  Errors are of 4 types  Compiler errors  Linker errors  Run time errors  Logical errors  Compile errors - Errors encountered during compilation phase.  Compile errors are of 2 types  Syntax error – Errors which occur when we violate the grammatical rules of the programming language. Eg: Missing semicolon , Using wrong operator with cout etc  Semantic error – Errors which occur when statements are meaning less. Eg: Wrong usage of assignment operator a+b=c  Linker errors – Errors which occur during linking phase. They occur when linker is not able to link various object files together. Eg: Missing main() function in a program , Unable to open a file etc.  Run time errors – Errors which occur during run time. It causes abnormal termination during execution. Eg: Division by 0 , No enough memory (Array exceeds array size) etc.  Logical errors – Errors which occur due to incorrect translation of program logic or algorithm. Eg : You need to do (A+B)/C and if you write the statement as A+B/C the output will be different from the expected answer.  Write a program to display the message “Programming in C++”.  Write a program to display your name ,school name , address and class with section in different lines.  Write a program to display the following on screen |----------------------------------------------| | Name : Your name | | Class : XI A | | Gender : Female | |----------------------------------------------|  Find the output of the following statements: cout<<“Welcome\btoC++’;  cout<<“Welcome\\btoC++”;  cout<<“Welcome\\toC++”;