Basics of c++

2,314 views

Published on

Published in: Art & Photos, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,314
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
267
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Basics of c++

  1. 1. PerfectBEM HSS, Palakkad 1
  2. 2. C++ BASICS C++ Character Sets Tokens 1. Keywords 2. Identifiers 3. Literals i. Integer constant ii. Floating constant iii. Character constant iv. String literal 4. Punctuators 5. Operators BEM HSS, Palakkad 2
  3. 3. C++ Data types1. Fundamental data types a) int Data type b) char Data type c) float Data type d) double Data type e) void Data type2. Derived data types a) Arrays b) Function c) Pointer d) Reference e) Constant3. User-Defined Derived data types a) Class b) Structure c) Union4. Data Type Modifiers 1. Signed 2. Unsigned 3. Long 4. Short BEM HSS, Palakkad 3
  4. 4. Operators and Expression1. I/O Operators2. Arithmetic Operators3. Increment / Decrement Operators4. Relational Operators5. Logical Operators6. Conditional Operators7. Some other Operators 1. Sizeof() 2. Comma Operator BEM HSS, Palakkad 4
  5. 5. Flow of Control1. Compound Statement2. Selection statements 1. The if statement 2. The switch statement3. Iteration Statements 1. The for loop 2. The while loop 3. The do..while loop 4. Nested Loops4. Jump Statements 1. The goto statement 2. The break statement 3. The continue statement BEM HSS, Palakkad 5
  6. 6. Console I/O OperationsI. Unformatted Console I/O functions 1. Single Character functions(getchar(), putchar() 2. String functions (gets(), puts())II. Unformatted Stream I/O functions I. Character functions(get(), put()) II. String functions (getline(), write()) ARRAY FUNCTION BEM HSS, Palakkad 6
  7. 7. When? Where? Who? C++ is a language developed at AT & T Bell laboratories by Bjarne Stroustrup in the 1980s.Wednesday, October 13, 2010 BEM HSS, Palakkad 7
  8. 8. C++ CHARACTER SET It is a set of valid characters that a language can recognize. It represents any letter, digit or any other sign.Wednesday, October 13, 2010 BEM HSS, Palakkad 8
  9. 9. The C++ has the following CHARACTER SET 1. LETTERS - A-Z, a-z 2. DIGITS - 0-9 3. SPECIAL SYMBOLS 4. WHITE SPACES 5. OTHER CHARACTERSWednesday, October 13, 2010 BEM HSS, Palakkad 9
  10. 10. TOKENS C++ provides following tokens (smallest individual unit in a program ) KEYWORDS IDENTIFIERS LITERALS PUNCTUATORS OPERATORSWednesday, October 13, 2010 BEM HSS, Palakkad 10
  11. 11. KEYWORDS These are the words that convey a special meaning to the language compiler. These are reserved for special purpose and must not be used as normal identifier names.Wednesday, October 13, 2010 BEM HSS, Palakkad 11
  12. 12. Some of the KEYWORDs are… auto, break, case, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, inline, integer, long, new,…..Wednesday, October 13, 2010 BEM HSS, Palakkad 12
  13. 13. IDENTIFIERS These are FUNDAMENTAL BUILDING BLOCKS OF A PROGRAM. C++ is case sensitive as it treats upper and lower-case characters differently.Wednesday, October 13, 2010 BEM HSS, Palakkad 13
  14. 14. Some of the valid IDENTIFIERS are… MYFILE Myfile Stud1 stud23 Emp_no Reg_NoWednesday, October 13, 2010 BEM HSS, Palakkad 14
  15. 15. Some of the invalid IDENTIFIERS are… MY.FILE 29Myfile break Emp-no Reg NoWednesday, October 13, 2010 BEM HSS, Palakkad 15
  16. 16. LITERALS These are data items that never change their value during a program run. It is also referred to as constantsWednesday, October 13, 2010 BEM HSS, Palakkad 16
  17. 17. Different types of LITERALS are… 1. Integer-constant 2. Character-constant 3. Floating-constant 4. String-constantWednesday, October 13, 2010 BEM HSS, Palakkad 17
  18. 18. INTEGER CONSTANT These are WHOLE NUMBERS WITHOUT ANY FRACTIONAL PART. C++ allows three types of integer constant Decimal – Eg. 123, +97 Octal - Eg. 0123, 010 Hexa-Decimal – Eg. 0x14, 0XCWednesday, October 13, 2010 BEM HSS, Palakkad 18
  19. 19. CHARACTER CONSTANT It is one character enclosed in single quotes. Eg. „a‟, „1‟ Escape sequence represents a single character (Eg. „n‟, „t‟, „‟, etc.)Wednesday, October 13, 2010 BEM HSS, Palakkad 19
  20. 20. FLOATING CONSTANTIt is also called real constantsThese are numbers having fractional partsThese may be written in one of the two forms called Fractional form Exponent formEg. 2.0, 17.23, - 45.23Eg. 172E05, 1.72E07Wednesday, October 13, 2010 BEM HSS, Palakkad 20
  21. 21. STRING CONSTANTIt is a sequence of characters surrounded by double quotes.Eg. “a”, “123”, “hello welcome”Each string literal is by default (automatically) added with a special character „0‟ which makes the end of a stringWednesday, October 13, 2010 BEM HSS, Palakkad 21
  22. 22. PUNCTUATORSThe following characters are used as punctuators (also known as separators). [] () {} , ; : * = #Wednesday, October 13, 2010 BEM HSS, Palakkad 22
  23. 23. Basic Datatypes. Operators. Control Structures. Arrays and FunctionsWednesday, October 13, 2010 BEM HSS, Palakkad 23
  24. 24. What is Data type ? Data types are the means to identify the type of data and associated operations of handling it.Wednesday, October 13, 2010 BEM HSS, Palakkad 24
  25. 25. C++ data types are of two types 1. Fundamental (atomic or dynamic or primitive) Datatype 1. Derived (Non-primitive) DatatypeWednesday, October 13, 2010 BEM HSS, Palakkad 25
  26. 26. 1. Fundamental Data type The data items that cannot be decomposed of. There are five such data types : int for integers float for real numbers char for characters double for real numbers with higher precision void for null set of dataWednesday, October 13, 2010 BEM HSS, Palakkad 26
  27. 27. integer Data type Approx. sizeType Minimal range ( in bytes)Short 2 -32768 to 32767Unsigned short 2 0 to 65535Signed short 2 Same as shortint 2 -32768 to 32767Unsigned int 2 0 to 65535Signed int 2 Same as intlong 4 -2147483648 to 2147483648Unsigned short 4 0 to 4294967295Signed short 4 Same as long BEM HSS, Palakkad 27
  28. 28. float Data type Approx. size Type Minimal range ( in bytes) Float 4 3.4 X 10 -38 to 3.4 X 10 38 - 1 Double 8 1.7 X 10 -308 to 1.7 X 10 308 - 1 Long double 10 3.4 X 10 -4932 to 3.4 X 10 4932 – 1Wednesday, October 13, 2010 BEM HSS, Palakkad 28
  29. 29. char Data type Approx. size Type Minimal range ( in bytes) char 1 -128 to 127 Unsigned char 1 0 to 255 Signed char 1 Same as charWednesday, October 13, 2010 BEM HSS, Palakkad 29
  30. 30. 2. Derived data types The data items that are composed of the fundamental data types They are of two types : 1. Predefined derived data types like array, pointer, reference, constant etc. 1. User defined derived data types like structure, class, union, etc.Wednesday, October 13, 2010 BEM HSS, Palakkad 30
  31. 31. Uses of Data types As the type specifier for the data and functions in the program To declare variables and functions For the explicit type conversion of dataWednesday, October 13, 2010 BEM HSS, Palakkad 31
  32. 32. Data Type Modifiers The keywords used along with the basic data types to extend the range of data handled by the basic data types. This is achieved by excluding the negative numbers in the range Also by allocating more number of bytes for the data. Type modifiers of C++ are signed, unsigned, long and short.Wednesday, October 13, 2010 BEM HSS, Palakkad 32
  33. 33. 1. Arrays It refers to a named set of finite number of same type of data, Each of which can be referenced by mentioning its position (called subscript) in the list. There are one-dimensional and multi- dimensional arrays.Wednesday, October 13, 2010 BEM HSS, Palakkad 33
  34. 34. 2. Function It is the named unit of a set of instructions to perform a particular task. A function is invoked from other parts of the program. A function should be declared before it is defined if the function calling statement appears before its definition.Wednesday, October 13, 2010 BEM HSS, Palakkad 34
  35. 35. 3. Pointer A variable that can hold the address of a memory location. The declaration of the pointer consists of a data type, an *(asterisk) and the variable name. The data type of the pointer depends upon the type of the data that is pointed to by it.Wednesday, October 13, 2010 BEM HSS, Palakkad 35
  36. 36. 4. Reference It is an alternative name of an object. A reference variable provides an alias name to a previously defined variable. Its declaration consists of the data type, an &(ampersand) and the variable. The data type of reference variable will be same as that of its original variable name.Wednesday, October 13, 2010 BEM HSS, Palakkad 36
  37. 37. 5. Constant It makes a variable symbolic constant. The keyword const is used to make an object constant. The syntax is: const DataType VarName = Value;Wednesday, October 13, 2010 BEM HSS, Palakkad 37
  38. 38. 6. Enumeration A small group of related objects may be put in a set and given a name. A user can define a set of integer type identifiers in the form of new data types called enumerated data types.Wednesday, October 13, 2010 BEM HSS, Palakkad 38
  39. 39. It is an alternative method for naming integer constants. The keyword used is enum. Syntax: enum variable {enumerator’s list}; Example: enum week {sun,mon,tue,wed,thur,fri,sat};Wednesday, October 13, 2010 BEM HSS, Palakkad 39
  40. 40. VARIABLES Variable are identifiers of memory locations That is, Variables are the names given to storage locations by which the contents of those locations can be referenced. There are two values associated with a variable:  lvalue which is the address of the memory location  rvalue which is the content of the location This is to be declared in the program prior to its use in it.Wednesday, October 13, 2010 BEM HSS, Palakkad 40
  41. 41. Varible declaration DataType VarName; m Variable Name Example : 100 RValue  int num;  float p,q,root; 100 101 LValue  int m=100;  const float pi=3.14;Wednesday, October 13, 2010 BEM HSS, Palakkad 41
  42. 42. OPERATORS Operators UNARY BINARY TERNARY 1. I/O 2. Arithmetic 1. Increment 3. Relational 2. Decrement 4. Logical Conditional Operator 3. Unary minus 5. Bitwise 4. Sizeof() operator 6. ShiftWednesday, October 13, 2010 BEM HSS, Palakkad 42
  43. 43. Increment / Decrement Two special operators of C++ The increment operator(++) adds 1 to its operand The decrement operator(--) subtracts 1 from its operand The result will be stored in the same operand.Wednesday, October 13, 2010 BEM HSS, Palakkad 43
  44. 44. Consider the code snippet: int a=5, b=3; a++; b--; After execution a = 6 and b = 2 i.e., a++; is equivalent to a = a + 1 and b--; is equivalent to b = b - 1 Two versions: postfix (a++) & prefix(++a)Wednesday, October 13, 2010 BEM HSS, Palakkad 44
  45. 45. sizeof() OperatorIt is a unary compile-time operatorIt returns the number of bytes occupied by the variable or data type specified within the paranthesesFor example : sizeof(float) returns 4 BEM HSS, Palakkad 45
  46. 46. I / O OperatorsThe operators for input and output operations are : >> called extraction or get from << called insertion or put toEg: cin >> a; cin >> b >> d; cout << “Hello”; cout << a; BEM HSS, Palakkad 46
  47. 47. RELATIONAL OPERATORS• It compare two quantities, i.e., itdetermine the relation between thetwo data.The result of the operation will beeither TRUE (1) or FALSE(0).They are : <, <=, >, >=, ==, != BEM HSS, Palakkad 47
  48. 48. Arithmetic operatorsThe symbols that trigger computations in the program are called arithmetic operators. + for Addition, - for subtraction, * for multiplication, / for division, % for remainder of division ( it works only on integer operands.) BEM HSS, Palakkad 48
  49. 49. Logical OperatorsIt is used to combine existing relational expressions.The logical AND(&&), logical OR(||) and logical NOT(!) are the operators. BEM HSS, Palakkad 49
  50. 50. Arithmetic Vs Relational Vs logical Arithemetic  Relational  Logical Performs  Performs  Combines relational expressions computational comparision operation operation Operands are  Numeric and  The operands are numeric and character operands boolean character data only are allowed values(TRUE/FALSE) Result will be  Result will be  Result will be a boolean values numeric either true or false Both unary and  All the operators  Both binary and unary binary operators are are binary operators are available available BEM HSS, Palakkad 50
  51. 51. Conditional Operator(? :)It is a ternary operator, which requires three operands to operate upon.This operator returns depending on a condition.The syntax : expression1?expression2:expression3 BEM HSS, Palakkad 51
  52. 52. Precedence of OperatorsPrecedence Operator Meaning () Function call or bracket1. [] Array subscript ! Logical negation(NOT) + Unary plus - Unary minus2. ++ Pre increment or post increment -- Pre decrement or post decrement Sizeof Returns size of operand, in bytes * Multiply3. / Divide % Remainder(Modulus) + Binary plus4. - Binary minus BEM HSS, Palakkad 52
  53. 53. Precedence of OperatorsPrecedence Operator Meaning < Less than <= Less than or equal to5. > Greater than >= Greater than or equal to == Equal to6. != Not equal to7. && Logical AND8. || Logical OR9. ?: Conditional Operator10. = Simple assignment BEM HSS, Palakkad 53
  54. 54. ExpressionsOperators and operands are combined to form expressions.An expression represents an operation and normally it returns a value as the result of the operation.Since there are different types of operators, expressions are also of different types. BEM HSS, Palakkad 54
  55. 55. Type conversionWhen constants and variables of different types are mixed in an expression proper type conversion methods are applied to get the correct results.It is the process of converting one predefined type into another in a particular expression.Type conversions are of two types: BEM HSS, Palakkad 55
  56. 56. 1. Implicit type conversionIt also known as type promotionConversion is done by the compiler without programmer’s intervention.In this case the compiler converts all operands up to the type of the largest operand. BEM HSS, Palakkad 56
  57. 57. 2. Explicit type conversionIt also known as type castingConversion is done by the programmer.Example,Ans = (float)a/b; BEM HSS, Palakkad 57
  58. 58. FLOW OF CONTROLNormally, the instructions contained in a program are executed in a sequential fashion and one instruction will be executed only once.In some situations, some of the statements need to be skipped without executing them,While in some other situations, Some of the statements need to be executed more than once.C++ provides two facilities : 1. Selection 2. Iteration BEM HSS, Palakkad 58
  59. 59. STATEMENTSIt is the instructions given to the computer to perform any kind of action.It is terminated with a semicolon(;).A sequence of statements enclosed by a pair of braces({ }) is called Compound statement. { statement-1; block statement-2; } BEM HSS, Palakkad 59
  60. 60. Selection StatementsIt is also called conditional or decision statementsC++ provides two types of Selection stmts 1. if statements – Single Selection 2. switch statement – Multiple Selection BEM HSS, Palakkad 60
  61. 61. The if statementsThe if statement has four forms:1. Simple if2. If…else3. If…else if ladder4. Nested if BEM HSS, Palakkad 61
  62. 62. Syntax of simple if if (boolean expression) { statement(s) } If expression evaluates to true, the statement will be executed; otherwise it will be skipped. BEM HSS, Palakkad 62
  63. 63. Syntax of if…else If the expressionif (bool. expression) evaluates to true, the{ statement1 will be statement1(s) executed and} statement2 will be skipped.else If the expression{ evaluates to false, the statement2(s) statement1 will be} skipped and statement2 will be executed. BEM HSS, Palakkad 63
  64. 64. Syntax of nested ifif (boolean expression){ An if statement can if(boolean expression) contain another if { statement, which is statement1(s) } called nesting of if.} The nesting will beelse complex when there are{ more and more ifs if(boolean expression) { within the else of each if. statement2(s) This can be simplified by } the if..else if ladder} BEM HSS, Palakkad 64
  65. 65. Syntax of if…else ladder if (boolean expression1) statement1 else if(boolean expression2) statement2 else if(boolean expression3) … else statement BEM HSS, Palakkad 65
  66. 66. ExerciseGiven the following set of identifiers:char ch;short sh;int intval;long longval;float fl;Identify the datatype of the following expression :(i) ‘a’ – 5 (ii) intval *longval – ch (iii) fl + longval / sh BEM HSS, Palakkad 66
  67. 67. Exercise What output will the following code fragment produce?int val, res, n=1000;cin >>val;res = n + val >1750 ? 400 : 200;cout << res; (i) if the input is 2000 (ii) if the input is 1000 (iii) if the input is 500 BEM HSS, Palakkad 67
  68. 68. Exercise Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. BEM HSS, Palakkad 68
  69. 69. Exercise Write the output of the following code segment when I is an integer variable with value 10.int x, I=10; (a)I+= I + I; cout << I; (b)x = I++ + ++I; cout << x; BEM HSS, Palakkad 69
  70. 70. ProblemWrite a program to display the grade corresponding to the mark given as input(using if…else ladder) BEM HSS, Palakkad 70
  71. 71. The conditional operator(? :) alternative to ifC++ has a conditional operator that can be used as an alternative to if statement.This operator can be used to replace if…else statements of the following general form: BEM HSS, Palakkad 71
  72. 72. if (expression) Example statement1 if (a>b)else big=a; statement2 else big=b;Alternative methodExpression ? Statement1 : statement2 big = (a>b) ? a : b; BEM HSS, Palakkad 72
  73. 73. The switch statementIt is a multiple-branch selection statementThis selection statement successively tests the value of an expression against a list of integer or character constants.When match is found, the statements associated with that constant are executed. BEM HSS, Palakkad 73
  74. 74. Syntax : switch(integer or character expression) { case constant1: statements; break; case constant2: statements; break; case constant3: statements; break; ……. default: statement; } BEM HSS, Palakkad 74
  75. 75. Example : switch(dow) { case 1: cout << “nSunday”; break; case 2: cout << “nMonday”; break; case 3: cout << “nTuesday”; break; case 4: cout << “nWednesday”; break; case 5: cout << “nThursday”; break; case 6: cout << “nFriday”; break; case 7: cout << “nSaturday”; break; default: cout << “nWrong number of day”; } BEM HSS, Palakkad 75
  76. 76. LOOPSIt is a group of instructions the computer executes repeatedly until some terminating condition is satisfied.Generally there are two typesEntry controlled loops For loop While loopExit controlled loops Do while loop BEM HSS, Palakkad 76
  77. 77. for loopSyntax:for(initialization_expression;conditional_expression; update_expression){ body of the loop;} BEM HSS, Palakkad 77
  78. 78. while loopSyntax:Initialization statatement;while(conditional_expression){ body of the loop; updating statement;} BEM HSS, Palakkad 78
  79. 79. do while loopSyntax:Initialization statementdo{ body of the loop; updating statement;} while(conditional_expression); BEM HSS, Palakkad 79
  80. 80. Nested LoopA loop containing another loop is known as nested loop.The inner and outer loop need not be of the same type. BEM HSS, Palakkad 80
  81. 81. ExerciseWrite a C++ program to find the decimal equivalent of a given binary number. BEM HSS, Palakkad 81

×