Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++

Similar presentations


Presentation on theme: "Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++"— Presentation transcript:

1 Introduction to C++ Part II Version 1.3

2 Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++ Arrays -- char arrays -- vectors

3 C++ Functions What we called methods in C# are referred to as functions in C++.

4 The C# compiler is a multipass compiler, and so it is not necessary to “declare” a method before it is used. This is not true in C++. Function Prototypes In C++, we must declare a function by writing a function prototype in the code someplace before the function is ever called. For stand-alone functions we normally write the function prototypes before main( ).

5 #include using namespace std; void printMe(int); int main( ) { int a = 5; printMe(a); system("PAUSE"); return 0; } void printMe(int num) { cout << "\nNumber = " << num << endl; } The function prototype Calling the function The function implementation

6 We usually include the function prologue with the function prototype. #include using namespace std; // The printMe function // Purpose: outputs the parameter // Parameter: The value to be output as an integer // Returns: nothing void printMe(int); int main( ) { int a = 5; printMe(a); system("PAUSE"); return 0; } void printMe(int num) { cout << "\nNumber = " << num << endl; }

7 Passing Parameters by Value C# void swap(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } Passing parameters by value is basically the same in C++ and C# C++ void swap(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } What is the problem here? void swap(x,y); //function call

8 Passing Parameters by Reference C# void swap( ref int num1, ref int num2) { int temp = num1; num1 = num2; num2 = temp; } Passing parameters by reference is slightly different in C++ and C# C++ void swap(int& num1, int& num2) { int temp = num1; num1 = num2; num2 = temp; } void swap(x,y); //function call

9 Passing Parameters by Address C# void swap( ref int num1, ref int num2) { int temp = num1; num1 = num2; num2 = temp; } Passing parameters by address is completely different in C++ and C# doesn’t allow it, normally C++ void swap(int* num1, int* num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void swap(&x,&y); //function call

10 In C++, there are no reference data types as there are in C#. Everything is passed by value, unless you explicitly tell the compiler to pass by reference.

11 Passing by const Refernce C++ has a notion of constantness that does not exist in C#. When passing a parameter by reference, it is possible to have a side effect that changes the data in the calling function. Generally side effects are not wanted. To avoid side effects, we pass by constant reference.

12 void printMe( const Employee& joe); This is a common idiom in C++. It allows us to pass an object without having to make a copy of the object (which is expensive), but avoids any changes to the object by the function.

13 If a member function does not change anything (for example, a “getter”), then you should make the function a constant function. string Employee::getAddress( ) const;

14 Arrays in C++

15 There is a major difference in how arrays are defined in C# and C++. In C#, arrays are true objects. In C++ they are not objects.

16 examScores 89 94 78 93 75 99 82 77 53 87 An array is a list of values … not an object All values must be of the same type Values are stored in consecutive memory locations The position where a value is stored in an array is given by its index. We sometimes refer to this as the subscript. Indexing always begins with zero To access an element of an array, we use the array name, followed by the index inside of square brackets 0 1 2 3 4 5 6 7 8 9

17 Declaring an Array examScores 0 1 2 3 4 5 6 7 8 9 int examScores[10]; the declaration data type of array elements array size Good programming style uses a constant for the array size. For example const int SIZE = 10; int examScores[SIZE];

18 examScores 89 94 78 93 75 99 82 77 53 87 0 1 2 3 4 5 6 7 8 9 index value of examScores[4] Accessing array elements in C++ is identical to C#

19 Out of Bounds Errors When a C++ program is executing, it does not notice when a calculation results in an array index that is out of bounds. The address of the element is calculated and that place in memory is accessed, even if it does not belong to the array! This is much different than what happens in C#.

20 someInts 0 1 2 3 4 0 1 2 3 4 index int someInts[5], badNum; badnum = 100; for ( int indx = 0; indx <=5; indx++) { someInts[indx] = indx; } someInts +4 someInts +8 someInts +12 someInts +16 badNum 1005 Notice that we over-wrote the variable badNum with the value of 5! This destroys the original value in badNum.

21 Initializer lists int examScores [ ] = { 87, 83, 94, 99, 74, 66, 88 }; the array size is automatically determined by the number of items in the initializer list int examScores [10] = { 0}; //intialize all values to 0

22 Since an array is not an object, there is no member data nor any methods that return the size of the array. When passing an array as a parameter, it is common to pass the size of the array as an additional parameter.

23 Two Dimensional Arrays rows columns How we think of a two dimensional array

24 examScores student 1 student 2 student 3 exam 1exam 2exam 3exam 4 788965 97 76798285 83899190

25 An Array of Arrays student 1 student 2 student 3 exam 1exam 2exam 3exam 4 788965 97 76798285 83899190

26 int main( ) { // declare an array 3 x 4 int examScores[ ][4]= { {78, 89, 65, 97}, {76, 79, 82, 85}, {83, 89, 91, 90} }; const int STUDENT = 3; const int EXAMS = 4; for ( int i = 0; i < STUDENT; i++ ) { int sum = 0; for ( int j = 0; j < EXAMS; j++ ) sum = sum + examScores [ i ][ j ]; float avg = ((float)sum)/EXAMS; cout << “Student # “ << (i+1) << “: “ << avg << “\n”; }

27 Multi-dimensional array as a parameter… We do not give the size of the first dimension of an array when passing it, but we do give the sizes of the remaining dimensions. The size of the first dimension is passed as a separate argument. void getPage (char p[][100], int sizeOne);

28 We have been using the C++ String class to represent strings of characters. Although this is the most convenient way to represent character strings, the C++ language also represents character strings as arrays of type char. Char Arrays

29 someText 0 1 2 3 4 5 h e l l o \0 The terminal character in the array is the null terminating character, \0. This is called a null terminated string, or a C-string (this is the only way that a character string could be represented in the C language). Functions that operate on C-strings look for the null terminating character to know where the end of the string is. When creating an array to store a character string, always be sure that there is room for the null terminating character.

30 Note that is possible to have an array of characters that is not a C-String. char firstName[20] = “John”; firstName 0 1 2 3 4 5 J o h n \0 ?... ? ? when initialized this way, the null terminating character is automatically added at the end. char lastName[20] = {‘S’,’m’,’i’,’t’,’h’}; lastName 0 1 2 3 4 5 S m i t h ?... ? ? when initialized this way, no null terminating character is added. This is just a simple array of characters. It is not a C-String!

31 Char Arrays and Loops You can treat a char array exactly like any other array. You can use index notation to access individual array elements lastName[n] = ‘ t ’; You can use loops to manipulate arrays elements. for (int n = 0; n < SIZE; n++) { lastName[n] = ‘ - ’; } But … be careful not to accidentally replace the null terminating character with some other character.

32 Assignment Although you can use the assignment operator when initializing an array, you cannot use the assignment operator anywhere else with a character array. For example, the following is illegal: char aString[10]; aString = “Hello”; Make sure that you do not leave an array with garbage values, i.e. char aString[10]; memset(aString,’\0’,sizeof(aString)); // intializes the array to all nulls as does: aString[10] = {‘\0’}’

33 strcpy Function The easiest way to assign a value to a C-String is to use the strcpy function. To use strcpy you must use the include directive #include No using statement is required, the definitions in are in the global namespace.

34 Security Issues Incorrect use of string functions has caused many security problems because of buffer over-runs. To prevent these problems use: “n” versions of functions, like strncpy – you must ensure that string is null-terminated. If it is not, do it manually after the copy. “l” versions of functions, like strlcpy – these are not part of the standard library, but source code is available from BSD “_s” versions of functions, like strcpy_s – available with Microsoft development tools, might be included in standard libraries later.

35 strcpy (aString, “Hello”); copies the char string Hello into aString. includes a null terminating character. Examples strcpy (aString, bString); copies the contents of bString into aString. strncpy (aString, bString, 9); copies at most 9 characters from bString into aString. by making the last parameter one less than the size of aString, you can make the copy safe … i.e. it will not over-run the array.

36 Equality Comparing two C-Strings using the equality operator will compile without errors and will execute, but will not give you the results you expect. char aString[ ] = “abc”; char bString[ ] = “abc”; if ( aString == bString ) { …

37 strcmp Function To compare two C-Strings, use the strcmp function. You must #include to use this function. strcmp(strng1, strng2); returns a value of zero if the strings are equal returns a negative value if strng1 < strng2 returns a positive value if strng1 > strng2 comparison is done in lexicographic order.

38 Other functions strcat (strng1, strng2); concatenates strng2 to the end of strng1. strlen (aString); returns the length of aString does not include the null terminating character

39 C-String Input and Output You can use >> and << operators to input and output C-Strings. To input a string containing blanks, you must use cin.getline (a, n); where a is a char array and n is an integer that indicates the max number of characters to read. Note that the null terminating character fills one of these character positions.

40 These techniques work on files as well as standard input and output.

41 Character I/O Sometimes it is useful to input and output one character at a time. cin.get(aChar); reads one character into aChar. cout.put (aChar); writes the character in aChar to cout. These work on any character, including spaces and new-line characters.

42 Example The following code will read one character at a time from standard in and write it to standard out, until a new-line character is encountered. char symbol = ‘ ‘; do { cin.get (symbol); cout.put (symbol); } while (symbol != ‘\n’);

43 Character Manipulation Functions The following functions operate on characters. To use any of these you must #include No using statement is required. The definitions in are in the global namespace.

44 The ASCII Code Table

45 toupper (aChar); returns the upper case value of aChar as an integer. tolower (aChar); returns the lower case value of aChar as an integer. islower (aChar); returns true of the value in aChar is lower case. isalpha (aChar); returns true if the value in aChar is a letter.

46 isdigit (aChar); returns true if the value in aChar is a digit 0 through 9 isspace (aChar); returns true if the value in aChar is white space.

47 Vectors Vectors can be thought of as arrays that grow as required, while a program is executing. Vectors are formed from a template class in the Standard Template Library (STL).

48 Declaring a Vector #include using namespace std;... vector v; the notation defines the type of vector. In this case, we have declared a Vector of integers.

49 Accessing Vector Elements You can use the square bracket notation [ ] to access elements of a vector, just as you do for an array. Note however, that you can only access existing Vector elements this way, you cannot initialize them!

50 Initializing Vector Elements To add an element to a Vector for the first time, you must use the push_back function. push_back adds an element in the next available position. v.push_back ( 23 );

51 The size of a Vector As noted, the size of a Vector changes as needed, to accommodate new elements. The current size of a Vector ( the number of elements in the Vector ) can be found by unsigned int n = v.size( ); note that the size( ) function returns an unsigned int. This can be automatically converted to an int by most compilers. To be safe, do an explicit cast or use the unsigned int data type, as shown above.

52 Example

53 #include using namespace std; int main ( ) { vector v; cout << “Enter a list of positive numbers.\n”; cout << “End the list with a negative number.\n”; int next; cin >> next; while ( next > 0 ) { v.push_back( next ); cout << next << “ added to the Vector\n.”; cout << “v.size( ) = “ << v.size( ) << endl; cin >> next; } cout << “You entered:\n”; for ( unsigned int i = 0; i < v.size( ); i++ ) cout << v[ i ]; return 0; }


Download ppt "Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++"

Similar presentations


Ads by Google