C++

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Slide1:

1 PRESENTATION ON GETTING STARTED WITH C++

Slide2:

2 C++ CHARACTER SET A character denotes any alphabet, digit or special symbol used to represent information. Every language has its own character set.

Definition of Tokens (Lexical Units):

3 Definition of Tokens (Lexical Units) The smallest individual unit in a program is known as Token or lexical units

C++ has followings tokens: :

4 C++ has followings tokens: C++ has five types of tokens :- KEYWORDS IDENTIFIERS LITERLS OPERATORS PUNCTUATORS

Definition of keywords:

5 Definition of keywords Keywords are the words that convey a special meaning to the language compiler. These are the reserved words and must not be used as identifier Examples: int, char, float, class, if, else

List of keywords in c++:

6 List of keywords in c++

Identifiers/Variables Names:

7 Identifiers/Variables Names In any program we typically do lots of calculations. The results of these calculations are stored in computer memory. The calculated values are stored in the memory. To make the retrieval and usage of these values easily, these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names .

Rules for forming Identifier:

8 Rules for forming Identifier An identifier as a long sequence of letters & digits. Identifier forming rule of c++ states the following:- First letter should be an alphabet No special character is allowed except underscore It is case sensitive, Upper & lower case letter treat differently. No blank space is allowed Keywords can not be used as identifiers/ variables names. .

Some Valid & Invalid Identifiers:

9 Some Valid & Invalid Identifiers Some Valid Identifiers Myfile N1 _h13 Some Invalid Identifiers Wi-fi // Contain Special char - Int // reserved keywords 29n // starting with digit

Definition of Literals:

10 Definition of Literals Literals (often referred as constant) are data items that never change their value during a program run. C++ allows several kind of literals: Integer –Constant Floating constant Character constant String constant

Integer Constants:

11 Integer Constants Rules for constructing integer constants :- An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. Examples:- 1234, 41, +97, -45

Floating/ Real Constants:

12 Floating/ Real Constants An real constant are numbers having fractional parts. These may be written in the form of the two forms called fractional form or the exponent Examples:- 2.0, 17.5, -13.05 7, 7. are invalid real constant

Floating/ Real Constants:

13 Floating/ Real Constants Rules for constructing real constants :- A real constant must have at least one digit after decimal. It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant . . examples 2.0, 17.5, -13.05 7, 7. are invalid real constant

Character Constants:

14 Character Constants A Character constant in c++ must contain one character & must be enclosed in single quotation marks. Examples: - ‘A’, ;4’, ‘+’

String Constants:

15 String Constants A string constant is a sequence of character quoted in double quotes. Examples: - “abc”, “\ab”; “A”

OPERATORS:

16 OPERATORS Operators are the symbols that define the actions that computer can perform on various operands. All executable statements involve the use of operators and operands. Operands are the items of data--- variable or constant Examples:- A + B A & B are operand + is an operator :

Classification :

17 Classification The various operators in C++ are as follows :- Arithmetic operators Relational operators Logical operators Assignment operators Conditional operators Increment and decrement operators Unary. Binary, ternary operators Special operators

Arithmetic Operators:

18 Arithmetic Operators Arithmetic operators- These are the operators used for performing various arithmetic operations on operands. It is a binary operator i.e it requires two operands to operate upon. There are five arithmetic operators available in C++

Arithmetic Operators:

19 Arithmetic Operators Operator Purpose + Addition - Subtraction * Multiplication / Division % Modulus (Remainder after division) The operands for arithmetic operators can be integers, float-point or mixed mode. The result of arithmetic operators is always a numeric value

Integer Arithmetic :

20 Integer Arithmetic If operand contains integer values then Operation is integer arithmetic E.g. If a =14 and b=4 a-b=10 a+b=18 a/b=3 a%b=2 a*b =56

Real Arithmetic:

21 Real Arithmetic When operand/variable contains real/float values, it is known as real arithmetic . If x,y are floats x=6.0/7.0=0.857143 y=-2.0/3.0=-0.666667

Mixed –mode Arithmetic:

22 Mixed –mode Arithmetic One of the operands is real and the other is integer For example 19/10.0=1.9

Relational Operators:

23 Relational Operators Relational operators are used for comparing values of two operands used in a relational expression. These operands can be of any type. The result of relational operators is only TRUE or FALSE . TRUE is numerically stored in C++ as value 1 and FALSE is stored as a value 0.The various relational operators present in C++ are:

Relational Operators:

24 Relational Operators Operator Meaning < is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to

Examples:

25 Examples if a=10, b=5 a>b (a greater than b) a<b ( a less than b) a==b ( a equal to b) a<=b (a less than equal to b) a>=b ( a greater than equal to b)

Logical operators :

26 Logical operators Logical operators are used to group two or more relational operations together in various ways. The logical operators are:

Logical operators :

27 Logical operators Operator Meaning && Logical AND || Logical OR ! Logical NOT

Examples:

28 Examples Logical AND (&&) a > b && x = = 10 , a>b && a>c Logical OR (||) a < m || a < n Logical NOT (!) ! (x >= y)

Assignment Operators :

29 Assignment Operators These operators are used to assign values to variables in C++. The left hand side of the operator contains a variable which is assigned a value given on right hand side of the operator. The value can be a constant, another variable or an expression. The syntax of assignment operator is: A=b, a=10, c=a+b C++ contains five additional assignment operators: +=, -= , /= , *= , %=

Conditional operators:

30 Conditional operators The conditional operator consists of 2 symbols the question mark (?) and the colon (:) Syntax: exp1 ? exp2: exp3

Example:

31 Example a=16 b=25; x=(a>b) ? a : b; if (a>b) x=a; else x=b;

Increment and Decrement:

32 Increment and Decrement syntax: 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – x= 5; y = ++x; (prefix) In this case the value of y and x would be 6 x= 5; y = x++; (post fix) Then the value of y will be 5 and that of x will be 6.

Increment and Decrement:

33 Increment and Decrement syntax: 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – x= 5; y = --x; (prefix) In this case the value of y and x would be 4 x= 5; y = x--; (post fix) Then the value of y will be 5 and that of x will be 4.

Unary Operators:

34 Unary Operators Which operators that require only one operand to act upon. Such operators are known as unary operators. Unary operators usually precede their single operand though some unary operators are written after their operand. These unary operators include : (-)Unary minus- for negating a particular value (+)Unary add (++)Increment operator- for incrementing the value by 1 ( -- ) decrement operator- for decrementing the value by 1 ++4, -20, +10, etc

Binary Operators:

35 Binary Operators Which operators that require two operand to act upon. Such operators are known as binary operators. Examples:- A+b A-b A*c A<b A==b

Binary Operators:

36 Binary Operators Which operators that require three operand to act upon. Such operators are known as ternary operators. Examples:- Conditional operators Expression 1? Expression 2: expression 3 If a=10, b=20 X= (a>b) ?a:b

Comma operator:

37 Comma operator Link related expressions together Expressions are evaluated left to right for e.g. value = (x = 10, y = 5, x + y); for (n=1, m=10, n <=m; n++, m++)

The sizeof Operator:

38 The sizeof Operator Gives of bytes occupied in the memory. To determine the lengths of arrays and structures when their sizes are not known to the programmer for e.g. x = sizeof (sum); y = sizeof (long int); z= sizeof (235L);

Precedence and Associativity :

39 Precedence and Associativity Precedence rules decides the order in which different operator are applied. Associativity rule decides the order in which multiple operators are applied.

PUNCTUATORS:

40 PUNCTUATORS The following characters are used as punctuators:- { }, [ ], ( ), comam(,), colon :, etc

Slide41:

41

authorStream Live Help