1 / 38

Welcome to GETTING STARTED WITH C++

Welcome to GETTING STARTED WITH C++. Prepared By : VINAY ALEXANDER (विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand. Introduction C++ Character Set Tokens(Lexical Units) A first look at C++ Program Using I/O Operators Typing and Executing Programs. what is C++ used for?

Télécharger la présentation

Welcome to GETTING STARTED WITH C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome to GETTING STARTED WITH C++ Prepared By : VINAY ALEXANDER (विनय अलेक्सजेंड़र) PGT(CS) KV jhagrakhand

  2. Introduction C++ Character Set Tokens(Lexical Units) A first look at C++ Program Using I/O Operators Typing and Executing Programs

  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++.

  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

  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

  6. => types of token in C++- • Keywords • Identifiers • Literals • Punctuators • Operators

  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 .

  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

  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)

  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

  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)

  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.

  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.

  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

  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

  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: ! % ^ & * ( ) – + = { } | ~ [ ] \ ; ' : " < > ? , . / #

  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 .

  18. & Address operator • * Indirection operator • ++ increment operator • -- decrement operator • ! Logical negation • ~ Bitwise operator

  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)) ^ ->

  20. =>Logical operators: &&,II • Example: 6 < 9 & & 4 > 2. • Result is true because both are true. • ||- Result will be true if one is true. =>Assignment operators: +,*=,+= =>Relational operators: <, >, <=, >=,==,!= =>Conditional operators: ? , :

  21. // Program to print a string on the screen #include<iostream.h> #include<conio.h> int main() { clrscr(); cout<<“welcome to C++ Programming”; getch(); Return 0; }

  22. =>// 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<iostream.h> // I/O library Statements that begin with #(hash/pound) sign- are directives for the preprocessor. It tells the compiler’s 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.

  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.

  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.

  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

  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*/.

  27. Examples of comments /* If you saw this code float f= 128; float c= (f-32)*5/9; // Do conversion cout << f << c << endl; You might guess it was converting fahrenheit to celsius. float fahrenheit= 128; float celsius= (fahrenheit-32)*5/9; cout << fahrenheit << celsius << endl; */

  28. Output Operators “<<“: This operator (<<) applied to an output stream is known as insertion operator. It performs an output operation on a stream generally involving some sort of formatting of the data (like for example writing a numerical value as a sequence of characters). • Example: cout<<“the sum of 2+5 = ”; cout<<2+5; cout<<“\n”; cout<<“the Result of 8-2 = ”; cout<<8-2;

  29. 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<iostream.h> #include<conio.h> void main( ) { clrscr( ) ; intx,y,sum; cout<< “ enter the value of x \n ”; cin>> x; cout<< “ enter the value of y \n”; cin>> y; sum=x+y; cout<< “ the sum of given values is:\n ”; cout<<sum; getch(); }

  30. => Variable : A variable is a storage area whose contents can vary during processing. In the above example: x, y and sum are variables

  31. Cascading of I/O Operators We have used the extraction operator << repeatedly in the two statements for printing results. The statement cout<<”Total Value is =”<<total<<”\n”; First sends the string “Total Value is=” to cout and then sends the value of sum. Finally, it sends the new line character so that the next output will be in the new line. The multiple use of << in one statement is called cascading. When cascading an output operator, we should ensure necessary blank spaces between different items. Using the cascading technique

  32. 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

  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

  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

  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<iostream.h> void main() { inta,b; cin>> a>>b; cout<<a+b, }

  36. =>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;

  37. Logical error A logical error is that error which cause a program to produce incorrect or undesired output. For example : intctr=1; while (ctr>10) { cout<<n*ctr; ctr= ctr+1; }

  38. END

More Related