Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/16/2016Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01a Title: C++ as Conventional Prog Lan (Review) Reference: COS240 Syllabus.

Similar presentations


Presentation on theme: "3/16/2016Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01a Title: C++ as Conventional Prog Lan (Review) Reference: COS240 Syllabus."— Presentation transcript:

1 3/16/2016Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01a Title: C++ as Conventional Prog Lan (Review) Reference: COS240 Syllabus

2 3/16/2016Assoc. Prof. Stoyan Bonev2 Lecture Contents (new): Anatomy of a C++ program –Sample source text, the #include directive, comments, names - identifiers, reserved words, modifiers, classes (methods (statements, blocks of statements )) Components of a C++ program –Data – by category –Data – by type –Expressions (incl. operands & operators) –Statements –Routines (functions) –I/O facilities –Data collections – arrays, etc Demo programs - examples

3 3/16/2016Assoc. Prof. Stoyan Bonev3 Anatomy of a C++ program C++ program contains functions and global variables. Functions are separate program units and contain statements that specify computing operations to be done. Variables store values used during computation. Variables are named by the developer. Functions are named by the developer (except one that is always present and named main ).

4 3/16/2016Assoc. Prof. Stoyan Bonev4 C++ Program as hierarchy of syntax categories Top-Down hierarchy Bottom-Up hierarchy ‘Has-a’ relation‘Part of’ relation Program Function(s) Statement(s) Expression(s) Operator(s) Operand(s) Data

5 3/16/2016Assoc. Prof. Stoyan Bonev5 Syntax for simple C++ program using namespace std; void main() { }

6 3/16/2016Assoc. Prof. Stoyan Bonev6 The “Hello, World!” program C notation #include void main( ) { printf(“\nHello, World!”); }

7 3/16/2016Assoc. Prof. Stoyan Bonev7 The “Hello, World!” program C++ notation #include using namespace std; void main( ) { cout << “\nHello, World!”; }

8 3/16/2016Assoc. Prof. Stoyan Bonev8 #include Compiler directive Includes previously written code from a library into your program E.g. #include has operators for performing input and output within the program Libraries allow for code reuse

9 3/16/2016Assoc. Prof. Stoyan Bonev9 using namespace std; Indicates to compiler that this program uses objects defined by a standard namespace called std. Ends with a semicolon Follows #include directives in the code Must appear in all programs

10 3/16/2016Assoc. Prof. Stoyan Bonev10 Comments // symbols indicate a line comment - apply to just the rest of the line Block comments start with /* and end with */ - apply to as many lines as you like Used to describe the code in English or provide non-code information –E.g. to include the name of the program or the author’s name

11 3/16/2016Assoc. Prof. Stoyan Bonev11 Function main int main ( ) { // function body return 0; }

12 3/16/2016Assoc. Prof. Stoyan Bonev12 Function main Exactly one main function per program A function is a collection of related statements that perform a specific operation int indicates the return type of the function ( ) indicates no special information passed to the function by the operating system

13 3/16/2016Assoc. Prof. Stoyan Bonev13 Types of Statements Declaration statements - describe the data the function needs: const float KM_PER_MILE = 1.609; float miles, kms; Executable statements - specify the actions the program will take: cout << “Enter the distance in miles: “; cin >> miles;

14 3/16/2016Assoc. Prof. Stoyan Bonev14 Identifiers Names for data and objects to be manipulated by the program Must begin with a letter or underscore (not recommended) Consist only of letters, digits, and underscores Cannot be reserved word Upper and lower case significant

15 3/16/2016Assoc. Prof. Stoyan Bonev15 Data within Program Data classification by category: –Literal Values –Named constants –Variables

16 3/16/2016Assoc. Prof. Stoyan Bonev16 Data Types Defines a set of values and operations that can be performed on those values Data – classification by type: –Boolean (keyword bool ) –Character (keyword char ) –Integer (keywords int, unsigned, short, long ) –Real (keywords float, double, long double )

17 3/16/2016Assoc. Prof. Stoyan Bonev17 Data Types Defines a set of values and operations that can be performed on those values integers –positive and negative whole numbers, e.g.5-52 343222 –short, int, long –represented internally in binary –predefined constants INT_MIN and INT_MAX

18 3/16/2016Assoc. Prof. Stoyan Bonev18 Data Types Floating point (real) –number has two parts, integral and fractional –e.g.2.53.66666666-.0000345.0 –float, double, long double –stored internally in binary as mantissa and exponent –10.0 and 10 are stored differently in memory

19 3/16/2016Assoc. Prof. Stoyan Bonev19 Data Types Boolean –named for George Boole –represent conditional values –bool –values: true and false

20 3/16/2016Assoc. Prof. Stoyan Bonev20 Data Types Characters –represent individual character values E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’ ’ –stored in 1 byte of memory –special characters: escape sequences E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’

21 3/16/2016Assoc. Prof. Stoyan Bonev21 Data Types Strings in C++: C-style –One-dimensional array of characters C++ STL style –Strings not built-in, but come from library string literal enclosed in double quotes E.g.: “Enter speed: “ “ABC” “B” “true” “1234”

22 3/16/2016Assoc. Prof. Stoyan Bonev22 string Class Strings not built-in, but come from library Classes extend C++ string literal enclosed in double quotes E.g.: “Enter speed: “ “ABC” “B” “true” “1234” #include –for using string identifiers, but not needed for literals

23 3/16/2016Assoc. Prof. Stoyan Bonev23 Variable Declarations Set aside memory with a specific name for the data and define its values and operations The value can change during execution type identifier-list; E.g.:float x, y; int me, you; double week = 40.0; string flower = “rose”;

24 3/16/2016Assoc. Prof. Stoyan Bonev24 Constant Declarations Memory cells whose values cannot change once set const type constant-identifier = value; E.g.: const float KM_PER_MILE = 1.609; Often identified by using all upper case name

25 3/16/2016Assoc. Prof. Stoyan Bonev25 Expressions (Operands+Operators) Operands –Literal Values –Named constants –Variables – scalar or indexed –Structure member –Function call –Sub expression like ( … )

26 3/16/2016Assoc. Prof. Stoyan Bonev26 Expressions (Operands+Operators) Operators: Survey on arithmetic, relational, logical and assignment operators. –Arithmetic*/% –+- –Relational >= –Equality==!= –Logical AND (conjunction) && –Logical OR (disjunction)|| –Assignment=

27 3/16/2016Assoc. Prof. Stoyan Bonev27 Expressions Expressions - are composed of operands and operators; 1 + 2 A – 3 * 5.6 A = B + C A = B = 1 + 2 A = (B = C / (D*E)) + F; expression terminated by ; is a statement A program is a sequence of statements

28 3/16/2016Assoc. Prof. Stoyan Bonev28 Arithmetic Expressions int data type + - * / % Integer division examples - result is integer 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined

29 3/16/2016Assoc. Prof. Stoyan Bonev29 Modulus for Integers Used only with integers Yields remainder - the result is integer Examples: 7 % 2 = 1 299 % 100 = 99 49 % 5 = 4 15 % 0 undefined 15 % -7 system dependent

30 3/16/2016Assoc. Prof. Stoyan Bonev30 Mixed-type Expressions E.g.: 4.6 / 2 evaluates to 2.3 Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type Caveat: this rule is dependent on operator precedence rules

31 3/16/2016Assoc. Prof. Stoyan Bonev31 Mixed-type Assignments If the variable on left side of assignment is of different type than the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type

32 3/16/2016Assoc. Prof. Stoyan Bonev32 Order of Operator Precedence ( )nested expressions evaluatedinside out unary +, - *, /, % binary +, - Highest Lowest Associativity Warning: watch out for the types of operands and the type of the result from evaluating each operand!

33 3/16/2016Assoc. Prof. Stoyan Bonev33 Figure 2.10 Evaluation for z - (a +b / 2) + w * -y

34 3/16/2016Assoc. Prof. Stoyan Bonev34 Figure 2.11 Evaluation tree for m = x + k / 2;

35 3/16/2016Assoc. Prof. Stoyan Bonev35 Executable Statements Assignment statements Decision/Selection statements Iteration/Repetition statements Input statements Program output The return statement

36 3/16/2016Assoc. Prof. Stoyan Bonev36 Assignment Statements variable = expression; = ; E.g.: kms = KM_PER_MILE * miles; x = 5 + y + z[5]*sqrt(144.);

37 3/16/2016Assoc. Prof. Stoyan Bonev37 Decision/Selection statements Executable statements: selection/decision if ( ) if ( ) else switch statement

38 3/16/2016Assoc. Prof. Stoyan Bonev38 Decision/Selection statements Example: fragment of linear equation solution if ( ) if ( b != 0 ) x = -c/b; if ( b != 0 ) { x = -c/b; cout << “\n Solution is =“ << x; }

39 3/16/2016Assoc. Prof. Stoyan Bonev39 Decision/Selection statements Example: fragment of triangle area problem if ( ) else if ( a+b>c && b+c>a && a+c>b) { p = (a+b+c)/2; s = sqrt( p*(p-a)*(p-b)*(p-c) ); cout << “\n Area is = “ << s; } else cout << “\nYou entered invalid data“;

40 3/16/2016Assoc. Prof. Stoyan Bonev40 Decision/Selection statements Example: Fragment of menu choice program switch statement int choice; cin >> choice; switch (choice) { case 1:Rectangle();break; case 2:Square();break; case 3:Triangle();break; case 4:Circle();break; default:cout << “\n Invalid input”; };

41 3/16/2016Assoc. Prof. Stoyan Bonev41 Iteration/Repetition statements Executable statements: iteration/repetition –for ( ; ; ) –while ( ) –do while( ) ; –Iteration based on data structures (foreach)

42 3/16/2016Assoc. Prof. Stoyan Bonev42 Iteration/Repetition statements Example: sum of first five integers s=1+2+3+4+5 –for ( ; ; ) int I, s=0; for ( I=1; I<=5; I++ ) { s = s + I; // display intermediate results cout << ‘\n’ << s; } // display final result cout << “\n\n\nFinal sum is = ” << s;

43 3/16/2016Assoc. Prof. Stoyan Bonev43 Iteration/Repetition statements Example: sum of first five integers s=1+2+3+4+5 while ( ) int I = 1, s; s=0; while ( I<=5 ) { s = s + I; // display intermediate results cout << ‘\n’ << s; I = I + 1; } // display final result cout << “\n\n\nFinal sum is = ” << s;

44 3/16/2016Assoc. Prof. Stoyan Bonev44 Iteration/Repetition statements Example: sum of first five integers s=1+2+3+4+5 do while( ) ; int I = 1; int s = 0; do { s = s + I; // display intermediate results cout << ‘\n’ << s; I = I + 1; } while (I<5); // display final result cout << “\n\n\nFinal sum is = ” << s;

45 3/16/2016Assoc. Prof. Stoyan Bonev45 Iteration/Repetition statements –Iteration based on data structures (foreach) –C++ solution based on algorithm from STL library implemented as a function named foreach(…) #include void InchToSm(double in) {cout << in*2.54 << " ";} void main() { double inches[] = { 1., 10., 100., 1000. }; for_each(inches, inches+4, InchToSm); }

46 3/16/2016Assoc. Prof. Stoyan Bonev46 Input/Output in C++ Elementary standard input/output: I/O - C notation based on library functions getchar(), putchar() - Read/Write a character gets(), puts() - Read/Write a string of chars scanf(), printf() - formatted Read/Write I/O - C++ notation based on OO streams cin - standard input stream cout - standard output stream

47 3/16/2016Assoc. Prof. Stoyan Bonev47 Input Statements Obtain data for program to use - different each time program executes Standard stream library iostream cin - name of stream associated with standard input device (keyboard by default) Extraction operator (>>) E.g.:cin >> miles; cin >> age >> firstInitial;

48 3/16/2016Assoc. Prof. Stoyan Bonev48 Program Output Used to display results of program Also standard stream library iostream cout - name of stream associated with standard output device (monitor by default) Insertion operator (<<) for each element cout << data-element(s); cout << “The distance is “ << kms << endl;

49 3/16/2016Assoc. Prof. Stoyan Bonev49 The return Statement Last line of main function is typically return 0; This transfers control from the program to the operating system, indicating that no error has occurred

50 3/16/2016Assoc. Prof. Stoyan Bonev50 Functions Intro We need a program feature (module, unit, procedure, sub program, routine) to do the job for the basic components, specified as leaves of the tree for the structure chart. Such a program feature in C++ is called a function.

51 3/16/2016Assoc. Prof. Stoyan Bonev51 Functions Why to use functions? (two important reasons) 1.Dividing a program into functions is one of the major principles of structured programming. 2.Using functions results in reduced program size.

52 3/16/2016Assoc. Prof. Stoyan Bonev52 Functions Three important topics when dealing with functions:  prototype (signature) statement void fname(void);  function call statement fname();  function definition void fname(void) { }

53 3/16/2016Assoc. Prof. Stoyan Bonev53 Functions Placement of Functions in a Program Order of Execution of Functions

54 3/16/2016Assoc. Prof. Stoyan Bonev54 Functions Functions may get input data from calling unit –Parameter passing mechanism Functions may return value to calling unit – return statement

55 3/16/2016Assoc. Prof. Stoyan Bonev55 circum = findCircum(radius); float findCircum(float r) { return (2.0 * PI * r); } 10 radius 10 r 62.8318 call findCircum 62.8318 circum

56 3/16/2016Assoc. Prof. Stoyan Bonev56 The Moral on function calling statement If a function does not return a value, it’s called in the following context as a statement: ( ); DrawHouse( ); NumInBox(35.68); If a function does return a value, it’s called in two other contexts: –As an operand of cout statement to display the value returned or as an operand of an expression cout << endl << ave(30,50); res = ave(60,70)*5 + pol/ave(56,88);

57 3/16/2016Assoc. Prof. Stoyan Bonev57 Data Collections Simple data type or scalar data type: Data type used to store a single value. Data collection or data structure: Composite of related data items stored in memory under the same name.

58 3/16/2016Assoc. Prof. Stoyan Bonev58 Data Collections - arrays Array: a collection of data items of the same type. How to define (declare) an array: int a[6]; float b[10]; char c[20]; bool d[44];

59 3/16/2016Assoc. Prof. Stoyan Bonev59 Data Collections - arrays Array: a collection of data items of the same type. How to initialize an array: // size matches the list of initializers int prime1[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; // size omitted int prime2[ ] = { 2, 3, 5, 7, 11, 13 }; // size is greater than the list of initializers int prime3[20] = { 2, 3, 5, 7 };

60 3/16/2016Assoc. Prof. Stoyan Bonev60 Data Collections - arrays Array: a collection of data items of the same type. How to reference an array element: Using subscript or index –Always integer valued int x[10], I=5; X[4]x[I]x[I+1] x[2*I-3]x[I++]

61 3/16/2016Assoc. Prof. Stoyan Bonev61 25 Arrays as Function Arguments Remember arrays are pass by reference –Passing the array address Remember these points when passing arrays to functions –The formal array argument in a function is not itself an array but rather is a name that represents an actual array argument. Therefore in the function definition, you need only inform the compiler with [] that the actual argument will be an array

62 3/16/2016Assoc. Prof. Stoyan Bonev62 26 Arrays as Function Arguments Remember these points when passing arrays to functions –Formal array arguments that are not to be altered by a function should be specified using the reserved word const. When this specification is used, any attempt to alter the contents will cause the compiler generate an error message

63 3/16/2016Assoc. Prof. Stoyan Bonev63 Data Collections - arrays Array: a collection of data items of the same type. Multidimensional arrays: One-dimensional array int a[6]; Two-dimensional array float b[10][20]; Three-dimensional array double c[10][20][30];

64 3/16/2016Assoc. Prof. Stoyan Bonev64 Pointers Pointers are variables that contain address values. Pointers are variables used to store address values. The basic concept of a pointer is: indirect access to data values

65 3/16/2016Assoc. Prof. Stoyan Bonev65 Pointers How to define (declare) pointers as variables? int *p1; double *p2; char *p3;

66 3/16/2016Assoc. Prof. Stoyan Bonev66 More on Pointers and Arrays loop to traverse all array elements using direct access based on array subscripting expressions int a[10]; for (I=0;I<10;I++) { a[I]=I; cout << endl<<a[I];} loop to traverse all array elements using indirect access based on pointers int a[10];int *pa;pa = &a[0]; for (I=0;I<10;I++) { *pa=I; cout << endl<<*pa; pa++; }

67 3/16/2016Assoc. Prof. Stoyan Bonev67 More on Pointers and Arrays char amessage[] = “Now is the time!”; // 1-dim array, unspecified size, initialized char *pmessage; pmessage = “Now is the time!”; // pointer to string

68 3/16/2016Assoc. Prof. Stoyan Bonev68 More on Pointers and Arrays char amessage[] = “Now is the time!”; int I=0; while(amessage[I] != ‘\0’) { cout << endl << amessage[I]; I++; }

69 3/16/2016Assoc. Prof. Stoyan Bonev69 More on Pointers and Arrays char *pmessage = “Now is the time!”; while(*pmessage != ‘\0’) { cout << *pmessage; pmessage++; } ================================================ char *pmessage = “Now is the time!”; char *q; q = pmessage + strlen(pmessage); while( pmessage < q ) { cout << *pmessage; pmessage++; }

70 3/16/2016Assoc. Prof. Stoyan Bonev70 More on Pointers // Array of pointers char *pname[] = { “Mon”, “Tue",... “Sun” }; // 2-dim array, 7 rows, 4 columns char aname[][4] ={“Mon”, “Tue",... “Sun” };

71 3/16/2016Assoc. Prof. Stoyan Bonev71 More on Pointers Multidimensional arrays and pointers // regular 2-dim array int a[10][20]; // 1-dim array of 10 pointers, way to build // jagged/ragged/ array int *b[10];

72 3/16/2016Assoc. Prof. Stoyan Bonev72 More on Pointers Address arithmetic: char a[10]; a≡ a + 0≡&a[0] a + I≡&a[I] *(a+I)≡*&a[I]≡a[I]

73 3/16/2016Assoc. Prof. Stoyan Bonev73 More on Pointers Pointers to functions int fact(int n) { if(n==0) return 1; return n*fact(n-1); } int (*pf)(int);// pointer to function that has // one int param and returns int Direct function call Indirect function call cout << fact(5); pf = fact; cout << (*pf)(5);

74 3/16/2016Assoc. Prof. Stoyan Bonev74 Pointers and the new Operator Pointer Variable Declarations –pointer variable of type “pointer to float” –can store the address of a float in p float*p; The new operator creates (allocates memory for) a variable of type float & puts the address of the variable in pointer p p = new float; Dynamic allocation occurs during program execution

75 3/16/2016Assoc. Prof. Stoyan Bonev75 Pointers Actual address has no meaning for us Form:type*variable; Example:double *p; ? p

76 3/16/2016Assoc. Prof. Stoyan Bonev76 new Operator Actually allocates storage Form:new type; new type [n]; Example:new double; new double[30];

77 3/16/2016Assoc. Prof. Stoyan Bonev77 Accessing Data with Pointers indirection operator * *p = 15.5; Stores floating point value 15.5 in memory location *p - the location pointed to by p 15.5 p

78 3/16/2016Assoc. Prof. Stoyan Bonev78 Pointer Statements double *p; p = new double; *p = 15.5; cout << “The contents of memory cell pointed to by p is “ << *p ; Output The contents of memory cell pointed to by p is 15.5

79 3/16/2016Assoc. Prof. Stoyan Bonev79 Pointer Operations Pointers can only contain memory addresses So the following are errors: p = 1000; p = 15.5; Assignment of pointers is valid if q & p are the same pointer type q = p; Also relational operations == and !=

80 3/16/2016Assoc. Prof. Stoyan Bonev80 Manipulating the Heap When new executes where is data stored ? Heap –C++ storage pool available to new operator Effect of p = new float;

81 3/16/2016Assoc. Prof. Stoyan Bonev81 Figure 13.1 Heap before and after execution of p - new node;

82 3/16/2016Assoc. Prof. Stoyan Bonev82 Returning Cells to the Heap Operation delete p; Returns cells back to heap for re-use When finished with a pointer, delete it Watch –multiple pointers pointing to same address –only pointers created with new are deleted Form:delete variable; Example:delete p;

83 3/16/2016Assoc. Prof. Stoyan Bonev83 Data Collections - structures Definition: A collection of simple variables. A data type for a record composed of multiple components. These components may be the same data type or may be not the same data type.

84 3/16/2016Assoc. Prof. Stoyan Bonev84 Structure definition (declaration) structure specifier – applying struct reserved word. struct Date { int day, month, year; }; struct Person { char name[20]; // or char *name, or string name char gender; int age; float grade; }; This is a template only which does not occupy storage /memory cells/.

85 3/16/2016Assoc. Prof. Stoyan Bonev85 Referencing a structure type How to define a structure as a variable to be a scalar, an array or a pointer? Date a, b; Date c[20]; Date *ptr1; How to initialize a structured variable Date today = { 30, 8, 2011 };

86 3/16/2016Assoc. Prof. Stoyan Bonev86 Referencing a structure type How to define a structure as a variable to be a scalar, an array or a pointer? Person John, Mary; Person ClassCos240[12]; Person *ptr2; How to initialize a structured variable Person peter= {“Miller”, ‘m’, 19, 3.48 };

87 3/16/2016Assoc. Prof. Stoyan Bonev87 Accessing structure members Direct component selection operator. (period) a.day = 30; b.month = 8; c[10].year = 2011; Indirect component selection operator ─> ptr1 = &a; ptr1─>month = 3;

88 3/16/2016Assoc. Prof. Stoyan Bonev88 Pointers to Structs struct electric { string current; int volts; }; electric *p, *q; p and q are pointers to a struct of type electric

89 3/16/2016Assoc. Prof. Stoyan Bonev89 Pointers to Structs p = new electric; Allocates storage for struct of type electric and places address into pointer p currentvolts p ??

90 3/16/2016Assoc. Prof. Stoyan Bonev90 struct Member Access through a Pointer p ─>current = “AC”; p ─>volts = 115; Could also be referenced as (*p).current = “AC”; (*p).volts = 115; currentvolts p AC115

91 3/16/2016Assoc. Prof. Stoyan Bonev91 Pointers and Structs q = new electric; Allocates storage for struct of type electric and places address into pointer q Copy contents of p struct to q struct *q = *p; currentvolts p AC115 currentvolts q AC115

92 3/16/2016Assoc. Prof. Stoyan Bonev92 Pointers and Structs q ->volts = 220; q = p; currentvolts AC220 q AC 115 AC220 pq->currentq->volts p->currentp->volts q

93 3/16/2016Assoc. Prof. Stoyan Bonev93 Demo Programs Demonstrate end-of-file controlled loop using standard input from the keyboard and standard output to the screen Solution to implement in two versions: as unmanaged code console application as managed code console application Problem: To accumulate sum of stream of input integers terminated by end-of-data (CTRL/Z) indicator

94 3/16/2016Assoc. Prof. Stoyan Bonev94 Demo Program – Unmanaged Code // C++ UNMANAGED CODE, standard Input (keyboard) / Output (screen) #include using namespace std; void main() { int sum=0, arg; cin >> arg; while ( !cin.eof() ) { sum += arg; cin >> arg; } cout << endl << "Final sum = " << sum << endl; }

95 3/16/2016Assoc. Prof. Stoyan Bonev95 Demo Program – Managed Code // C++ MANAGED CODE, standard Input (keyboard) / Output (screen) // managedcpp.cpp : main project file. #include "stdafx.h" using namespace System; int main( ) { String^ pom; int sum=0; while ((pom = Console::ReadLine()) != nullptr ) { sum += int::Parse( pom ); } Console::WriteLine("Final sum = {0}",sum); Console::ReadLine(); return 0; }

96 3/16/2016Assoc. Prof. Stoyan Bonev96 Practical tasks Given a function definition int mul(int pa, int pb) { return pa * pb; } Are there any syntax errors to fix in stmts below int a = 20, b = 50, x = mul(5, 6); int a = 20, b = 50, y = mul(a, b); int a = 20, b = 50, z = mul(a + 5 * 6, b – 7 * 8); int a = 20, b = 50, v = mul(a = 5 * 6, b = 7 * 8);

97 3/16/2016Assoc. Prof. Stoyan Bonev97 Practical tasks Iteration based on data structures. Demonstrate functionality of for each construct in C++ version Transform the Inches To Centimiters task (slide 45) into Kilometers To Miles

98 3/16/2016Assoc. Prof. Stoyan Bonev98 Practical tasks Pointers to functions (slide 73). Given function signatures double areaRect(double a, double b); double perimeterRect(double a, double b); Create/declare an appropriate pointer and demonstrate direct and indirect invoking the above functions with list of actual arguments (20., 30.)

99 3/16/2016Assoc. Prof. Stoyan Bonev99 Practical tasks Multi D arrays. Arrays as function arguments. Slide 61. Write a program to demonstrate passing a two D array to function Try your self or see next slides.

100 3/16/2016Assoc. Prof. Stoyan Bonev100 Practical tasks Multi D arrays. Arrays as function arguments. Slide 61. Write a program to demonstrate passing a two D array to function #define NCOLS 5 void ArrModify2D( int par[][NCOLS], int nrow); void ArrModify2D( int par[][NCOLS], int nrow) { for (int i = 0; i < nrow; i++) for (int j = 0; j < NCOLS; j++) par[i][j] += 4; }

101 3/16/2016Assoc. Prof. Stoyan Bonev101 Practical tasks int matrix2[ ][5] = { { 51,52,53,54,55 }, { 61,62,63,64,65 }, { 71,72,73,74,75 } }; for (i=0; i<3; i++) { for (j = 0; j < 5; j++) cout << " " << matrix2[i][j]; cout << endl; } // two-dimensional arrays - as function arguments ArrModify2D(matrix2, 3); cout << "\n2D array after arrModify2D function called\n"; for (i=0; i<3; i++) { for (j = 0; j < 5; j++) cout << " " << matrix2[i][j]; cout << endl; }

102 Thank You for Your attention!


Download ppt "3/16/2016Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01a Title: C++ as Conventional Prog Lan (Review) Reference: COS240 Syllabus."

Similar presentations


Ads by Google