welcome to getting started with c++ prepared by prepared by : vinay alexander ( विनय...

38
Welcome to Welcome to GETTING STARTED WITH C++ Prepared By Prepared By : VINAY ALEXANDER VINAY ALEXANDER ( ( वववव वववव ववववववववववव ववववववववववव ) ) PGT(CS) PGT(CS) KV jhagrakhand KV jhagrakhand

Upload: jeffery-jenkins

Post on 26-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1
  • Welcome to GETTING STARTED WITH C++ Prepared By Prepared By : VINAY ALEXANDER ( ) PGT(CS) KV jhagrakhand
  • Slide 2
  • Introduction C++ Character Set Tokens(Lexical Units) A first look at C++ Program Using I/O Operators Typing and Executing Programs
  • Slide 3
  • what is C++ used for? C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The creator of C++, Bjarne Stroustrup, has put together a partial list of applications written in C++.Bjarne Stroustrup
  • Slide 4
  • =>Character set is a set of valid characters that a language can recognize. A character represent any letter, digit or any other sign =>Letters : A-Z, a-z => Digit : 0-9 => Special Symbol: Space + - * / ^ \ ( ) [ ] { } = !=. $, : ; % ! & ? _ # = @ =>White Spaces - Blank space, Horizontal tab(->), carriage return, newline, form feed
  • Slide 5
  • Token(Lexical Unit ) The smallest individual unit in a program is known as token. Tokens are usually separated by "white space." White space can be one or more: Blanks Horizontal or vertical tabs New lines Form feeds Comments
  • Slide 6
  • => types of token in C++- Keywords Identifiers Literals Punctuators Operators
  • Slide 7
  • Keywords Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. In addition, identifiers containing a double underscore(_ _) are reserved for use by C++ implementations and standard libraries and should be avoided by users.
  • Slide 8
  • Identifiers Identifier are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program. =>names given to Object or variable name Class, structure, or union name Enumerated type name Member of a class, structure, union, or enumeration Function or class-member function typedef name Label name Macro name Macro parameter
  • Slide 9
  • =>Identifier forming rules of C++ state the following 1.An identifier must start with a letter, an underscore, or a dollar sign. 2.Followed by any number of characters, digits 3.They must not be a keyword or Boolean literal or null literals. 4.They must not begin with a digit. 5.They can be of any length. 6.An identifier cannot contain operators, such as +, -, and so on. 7.Identifiers are case sensitive. itemsOrdered is not the same as itemsordered. 8.Identifiers cannot include spaces (note, no blanks)
  • Slide 10
  • Literals Literals(referred to a constant) are data items that never change their value during a program a run. A literal may be any of the following: =>Integer-constant =>Character-constant =Floating-constant => String-literal
  • Slide 11
  • INTEGER CONSTANT: An integer constant must have at least one digit and must not contain any decimal point. It may contain either + or sign. Default is a positive. Commas cannot appear in an integer constant. C++ allows three types of integer literals. =>Decimal(base 10) : An integer literal consisting of a sequence of digits. It contains( 0 to 9).example:12349 => Octal (base 8): A sequence of digits starting with 0(digit zero) is taken to be an octal integer. example:01234 It must not contain 8 and 9. It contains( 0 to 7) =>Hexadecimal base (16) : A sequence of digits preceded by 0x or 0X is taken to be an Hexadecimal integer. Hexadecimal integer only 0-9 and A-F.(10 A,11 B, 12 C,13 D,14 E,15 F)
  • Slide 12
  • Character constant A character is one character enclosed in single quotes.example: 's'. =>A character literal in C++ must contain one character and must be enclosed in single quotation marks. C++ allows nongraphic characters in character constants. Nongraphic characters are those characters that cannot be typed directly from keyboard. That is backspace, tabs etc Nongraphic characters can be represented by using escape sequence. escape sequence denoted by a backslash(\) followed by one or more characters.
  • Slide 13
  • Floating constant (are also called real literals) Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. A real literal in fractional form must have at least one digit before decimal point(.) and at least digit after decimal point. It may also have either + or - sign preceding it. A real literal with no sign is assumed to be positive. Floating-point constants have a "mantissa," which specifies the value of the number, an "exponent," which specifies the magnitude of the number, and an optional suffix that specifies the constant's type. The mantissa is specified as a sequence of digits followed by a point followed by an optional sequence of digits representing the fractional part of the number.
  • Slide 14
  • For example Fractional form 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example: 18.46e0 // 18.46 18.46e1 // 184.6
  • Slide 15
  • String literals 'Multiple character' constants are treated as string-literals. The rule for writing string- literal is given below : A string is a sequence of zero or more character surrounded by double quotes( ). Each character may be represented by an escape sequence. Each string literal is by default(automatically) added with a special character \0 which makes the end of a string. Example : abc size is 4 \ab size is 3
  • Slide 16
  • Punctuators Punctuators in C++ have syntactic and semantic meaning to the compiler but do not, of themselves, specify an operation that yields a value. Some punctuators, either alone or in combination, can also be C++ operators or be significant to the preprocessor. Any of the following characters are considered punctuators: ! % ^ & * ( ) + = { } | ~ [ ] \ ; ' : " ?,. / #
  • Slide 17
  • Operators Operators are special symbols used for specific purposes. Unary operators: Operators that act on one operand are referred to as Unary Operators. 1.Unary + : It operator unary ' + ' precedes an operand. of the unary + operator must have arithmetic type and the result is the value of argument. 2.Unary - : It operator unary ' - ' precedes an operand. The operand of the unary - operator must have arithmetic type and the result is the value.
  • Slide 18
  • & Address operator * Indirection operator ++ increment operator -- decrement operator ! Logical negation ~ Bitwise operator
  • Slide 19
  • =>BINARY OPERATOR: it required two operands to operate upon. Following are binary operators. =>Arithmetical operators: +,-,*,/,% =>Relational operators: > =>Bitwise operators: & -> (Bitwise AND) | -> (Bitwise OR) -> (Bitwise exclusive OR(XOR)) ^ ->
  • Slide 20
  • =>Logical operators: &&,II Example: 6 2. Result is true because both are true. ||- Result will be true if one is true. =>Assignment operators: +,*=,+= =>Relational operators:, =,==,!= =>Conditional operators: ?, :
  • Slide 21
  • // Program to print a string on the screen #include int main() { clrscr(); cout
  • =>// Program to print a string on the screen It is a comment line beginning with //.compiler does not execute comments line. it is used for programmers for short explanations, => #include // I/O library Statements that begin with #(hash/pound) sign- are directives for the preprocessor. It tells the compilers preprocessor to include the header file iostream in the program. The I/O library predefines a set of operations for handling reading and writing of built in data types.
  • Slide 23
  • => int main(): it indicates the beginning of the main function. All C++ program begin their execution started from main() function. It is always the first to be executed when a program starts. Curly braces { } -> every program in c++ has its code included in a pair of curly braces. every statement ends with a semicolon character(;). It is used after every executable instruction( # instructions are not executable instructions). return : The return instruction makes the main( ) to finish and it returns a value. returning 0 -> telling that program has terminated normally that is it has not found any errors during its execution.
  • Slide 24
  • The function of I/O Library-iostream.h 1.At the lowest implementation level. where the notion of data types is missing and files are treated as stream of bytes, the I/O library manages the transfer of these bytes. 2. At user level,where the notion of data types is present, I/O library manages the interface between these two level. Between user level and the lowest implementation level. 3. The I/O library predefines a set of operations for handling reading and writing of built in data types.
  • Slide 25
  • Predefined Stream in I/O Library The predefined stream object for input,output and error are as follows:- cin, as istream class object tied to standard input.cin stands for console input. cout, as ostream class object tied to standard output. cout stands for console output Cerr, as ostream class object tied to standard error.cerr stands for console error
  • Slide 26
  • Comments in a C++ Program =>comments are pieces of code that the compiler discards or ignores or does not execute. The purpose of comments is only to allow the programmer to insert some notes or descriptions to enhance readability of the program. There are two types comments are used in c++ 1.single line comments with // (double forward slashes end ): The comments that begin with // are single line comments. 2. Multiline or block comments with /* */:The block comments mark the beginning of comment with /* and end with*/.
  • Slide 27
  • Examples of comments /* If you saw this code float f= 128; float c= (f-32)*5/9; // Do conversion cout
  • Input Operators >>: This operator (>>) applied to an input stream is known as extraction operator. It performs an input operation on a stream generally involving some sort of interpretation of the data (like translating a sequence of numerical characters to a value of a given numerical type). =>Example: #include void main( ) { clrscr( ) ; int x,y,sum; cout> x; cout> y; sum=x+y; cout
  • => Variable : A variable is a storage area whose contents can vary during processing. In the above example: x, y and sum are variables
  • Slide 31
  • Cascading of I/O Operators We have used the extraction operator
  • Using Borland C++ Step 1: How to edit a filename.cpp file using Notepad application Launch the Notepad text editor as follows: Start > Programs > accessories>Notepad Write the C++ code for your application in the opened file Step 2: How to save the file In order to save the data, choose File-> Save option and indicate the name of the file (e.g. hello.cpp) as well as the folder of you choice in your home directory (e.g. h:\ee105\) Step 3: How to open a command prompt window for running the Borland C ++ Compiler In order to compile and run the C++ source code from the hello.cpp file, you have to launch the Borland C++ compiler as follows: Start > Programs > Borland C++ 5.5 > bcc55 or double click on the shortcut option on desktop A command prompt window will appear and will point to the temp directory on the C
  • Slide 33
  • Step 5: How to compile the hello.cpp file using Borland C++ Compiler You can compile a file with a C++ source code by typing XI filename.c at the command prompt. This should result in the following output if everything has worked Step 6: How to run a C++ program(press ctrl+F9) After the program was compiled(Alt+F9) and no errors were listed by the Borland C++ Compiler, an executable file is generated (e.g. hello.exe). This file is located in the same directory as the file with the source code (hello.cpp). You can run the executable file by typing filename.exe. (i.e. hello.exe) and the output of the program (if any) is listed on the screen. => To see your program output, press the key combination Alt+F5
  • Slide 34
  • Role of compile To show all the error in the programming These error are of five types 1 Syntax error 2 - Type error 3 Run time error 4 Logical error
  • Slide 35
  • Syntax error Syntax errors occurs rules when rules of a programming language are misused that is when a grammatical rule of C++ is violated. A syntax error refers to an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages syntax errors occur strictly at compile-time. A program will not compile until all syntax errors are corrected. Example: #include void main() { int a,b; cin>> a>>b; cout
  • =>Semantics Error: Semantics Error occur when statements are not meaningful. Example: x*y=z; =>Type Error: If the integer argument was expected but actually string was given in its place, it is a type error. =>Run Time Error: If enough memory is not available or an expression is trying to divide a number by zero are run time error. Example: int x,y=0,z; Z=x/y;
  • Slide 37
  • Logical error A logical error is that error which cause a program to produce incorrect or undesired output. For example : int ctr=1; while (ctr>10) { cout