1 / 33

CSE 5311 Fundamentals of Computer Science

CSE 5311 Fundamentals of Computer Science. C++ Programming. C++ Profile. C++ was developed by Bjarne Stroustrup in the early 80’s at Bell Labs. C++ is a high-level language hybrid language. It supports both object-oriented programming and procedural-based (structured) programming.

bela
Download Presentation

CSE 5311 Fundamentals of Computer Science

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. CSE 5311 Fundamentals of Computer Science C++ Programming

  2. C++Profile • C++ was developed by Bjarne Stroustrup in the early 80’s at Bell Labs. • C++ is a high-level language hybrid language. • It supports both object-oriented programming and procedural-based (structured) programming. • C++ grew out of C, which contributed to its rapid growth in popularity. • C++ is a compiled language (as opposed to an interpreted language such as visual basic or Java). • C is a subset of C++. • A C++ compiler can compile any C program.

  3. C++ Compiler Object file The C++ Compilation Process C Library Linker C++ Source Code C++ Library Programmer Library Executable

  4. C/C++ Primitive Data Types • Primitive data types have a predefined meaning. • The compiler has an intuitive understanding of the attributes of each primitive type. • Primitive data types can be broadly categorized as either character or numeric. • Numeric data types can be categorized as either integer or floating point.

  5. C/C++ Primitive Data Types • Integer data types • unsigned long int • long int • unsigned int • int • unsigned short int • short int • Signed integer values are stored in 2’s complement format

  6. C/C++ Primitive Data Types • Floating point data types • long double • double • float • Floating point values are usually stored in IEEE format

  7. IEEE Floating Point Formats • The sign is represented by a single bit. A 1 bit represents a negative number and a 0 bit represents a positive number • IEEE Short Real exponents are stored as a 8-bit unsigned integer with a bias of 127 decimal (01111111 binary)

  8. 2’s Complement Representation • Most modern computers use 2’s complement representation to store signed integers. • The high-order bit of an integer stored in 2’s complement representation will contain a 0 if the number is positive and a 1 if the number is negative. • By taking the 2’s complement of an integer you are effectively multiplying the number by -1. • The 2’s complement of a positive integer results in its negative representation. • The 2’s complement of a negative integer results in its absolute value.

  9. 2’s Complement Representation • Taking the 2’s complement of an integer is a 2-step process. • Step 1 - Flip all the bits in the number (0’s become 1’s and 1’s become 0’s). This is also known as taking the 1’s complement of the number. • Step 2 - Add 1 to the 1’s complement representation of the number). • In the process of adding 1, any carry out of the high-order position is lost. This is normal and not a problem in the world of computer arithmetic.

  10. 2’s Complement Representation • Assume we are storing a positive 9 in a one-byte (8-bit field) using 2’s complement representation. In binary it would appear as 00001001. • Now, take the 2’s complement of the number. • Step 1 - flip the bits. 11110110 • Step 2 - add 1. + 1 11110111 • The result, 11110111, is the 2’s complement representation of -9. • If we were to take the 2’s complement of 11110111 (-9), we would get 00001001 (+9).

  11. 2’s Complement Arithmetic • To confirm that 11110111 really does represent -9, we should be able to add it to +9 and get a result of 0. Remember we are doing binary addition: 1 + 1 = 10. 11110111 (-9) + 00001001 (+9) 00000000 (0) • As pointed out earlier, a carry out of the high-order position is lost.

  12. C/C++ Primitive Data Types • Character data types • char • Note: Neither C or C++ has a primitive string type. • Character values are stored in ASCII

  13. C/C++ Primitive Data Types • One of the frustrating things about the C/C++ primitive data types is that the number of bytes allocated to each type may vary from platform to platform. • This can be a major hindrance to portability. • Use the sizeof operator to find out the storage allocation for a particular platform. • For example • cout << sizeof(int); • cout << sizeof(float); • cout << sizeof(double);

  14. C++ Operators OperatorAssociativityType ( ) [ ] left to right (highest pres.) ++ -- + - ! & * right to left unary * / % left to right multiplicative + - left to right additive << >> left to right shift = = != left to right equality && || left to right logical and/or ?: right to left conditional = += -= *= /= %= right to left assignment , left to right comma

  15. Program Structure • A C program is made up of a main function and optionally one more additional functions that work together to carry out some task. • A C++ program is made up of a main function and optionally one or more additional functions and/or classes that work together to carry out some task.

  16. Function Structure returnType functionName (parameter list) { // function body } • Neither C or C++ allow function definitions to be nested within other function definitions. However, blocks { } may be nested within other blocks. • Every C/C++ statement must be terminated by a semicolon.

  17. Our First C++ Program #include <iostream.h> //This program prompts the user for two integers and then //displays the sum of the two integers. void main( ) { int value1, value2; cout << “Input an integer and press return: “; cin >> value1; cout << “Input another integer and press return: “; cin >> value2; cout << “The sum of “ << value1 << “ and “ << value2 << “ is “ << value1 + value2 << endl; }

  18. Our First C++ Program • Program components: • iostream.h - header file • main - function name • value1 and value2 - user defined variables. • Variables are symbolic names that refer to areas of memory reserved for holding data. • cout - output stream object. • << - stream insertion member function defined in cout. • cin - input stream object. • >> - stream extraction member function defined in cin. • ; (semicolon) - statement terminator. • Value1 + value2 - arithmetic expression. • { } - block delimiters.

  19. Control Structures • Control structures determine the flow of control through a program. • Flow of control refers the the order that instructions within a program are executed. • Control structures may be categorized as either sequential structures, selection structures or repetition structures.

  20. Control Structures • Sequential structures • Represented by the absence of any specific selection or repetition structure. • Selection structures • if • if/else • switch • ?: (conditional operator - same as if/else) • Repetition structures • while • do/while • for

  21. Selection Structures • if if (conditional test) { statement(s) } • if/else if (conditional test) { statement(s) } else { statement(s) }

  22. Repetition Structures • while while (conditional test) { statement(s) } • do/while do { statement(s) while (conditional test); • Note: braces { } may be omitted from around the body of a selection or repetition structure if the body only contains a single statement.

  23. The For Loop • Of the repetition structures, the for loop is the most complex. Initialize control variables Modify control variables Conditional test for (expression1; expression2; expression3) statement; • The for and while loops test for entry to the loop body at the top of the loop. The do/while tests for re-entry at the bottom of the loop.

  24. Conditional Expressions • Conditional expressions are primarily composed of relational and logical operators. For example: if (a < b && c != j) { statement(s) } while (c = = k) { statement(s) } do { statement(s) } while (b >= (j+n)); for (int i = 0; i < 100; i++) { statement(s) }

  25. Conditional Expressions Watch out!!! - A common mistake is to use the assignment operator (=) where where the equality operator (= =) was intended. For example: if (j = 100) { } This is syntactically correct but logically wrong. 100 will be assigned to j then the value in j will be used to determine if the condition is true or false. A value of 0 means false; a non-zero value means true.

  26. Pass By Value 5 number #include <iostream.h> int cubeByValue(int); //function prototype void main() { int number = 5; cout << ”Number equals " << number << endl; number = cubeByValue(number); cout << ”Number now equals " << number << endl; } int cubeByValue(int n) { return n * n * n; } n 5

  27. Pass By Reference 5 number #include <iostream.h> void cubeByReference(int &); void main() { int number = 5; cout << ”Number equals " << number << endl; cubeByReference(number); cout << ”Number now equals " << number << endl; } void cubeByReference(int &n) { n = n * n * n; } n (n is a constant pointer)

  28. Pass By Reference with Pointer 5 number #include <iostream.h> void cubeByPointer(int *); //function prototype main() { int number = 5; cout << ”Number equals " << number << endl; cubeByPointer(&number); cout << ”Number now equals " << number << endl; } void cubeByPointer(int *n) { *n = *n * *n * *n; } n (n is a non-constant pointer)

  29. Pointers • Pointers are a major component of C/C++ programming. • Many persons consider pointers the most difficult aspect of C/C++ to master. • A pointer is a variable that contains an address. • There are two operators that are commonly used when working with pointers. • * - the indirection operator which also doubles as the multiplication operator. • & - the address operator.

  30. Pointer Examples #include <iostream.h> void main() { int i = 20; int *iptr = &i; cout << i << endl; //20 cout << &i << endl; //0x8f47fff2 cout << iptr << endl; //0x8f47fff2 cout << &iptr << endl; //0x8f47fff0 cout << *iptr << endl; //20 }

  31. Pointer Examples void main() { int *iptr, iarr[5] = {10,20,30,40,50}; iptr = iarr; cout << iarr << endl; //0x8f47ffe6 cout << iptr << endl; //0x8f47ffe6 cout << iarr[0] << endl; //10 cout << *iptr << endl; //10 cout << *iarr << endl; //10 cout << iptr[0] << endl; //10 cout << iarr[2] << endl; //30 iptr += 2; cout << *iptr << endl; //30 cout << *(iptr-1) << endl;//20 }

  32. Pointer Examples void main() { char *cptr, *csptr = "abcde"; char carr[6] = "abcde"; cptr = carr; cout << carr << endl; //abcde cout << cptr << endl; //abcde cout << carr[0] << endl; //a cout << *cptr << endl; //a cout << *carr << endl; //a cout << cptr[0] << endl; //a cout << carr[2] << endl; //c cptr += 2; cout << *cptr << endl; //c cout << *(cptr - 1) << endl; //b }

  33. Pointer Examples void main() { char sarr[3][10] = {"Fred", "Susan", "Mary"}; cout << sarr << endl; //0x8f43ffba cout << *sarr << endl; //Fred cout << *(sarr + 1) << endl; //Susan cout << sarr[2] << endl; //Mary cout << sarr[0][1] << endl; //r char *parr[3] = {"Fred", "Susan", "Mary"}; cout << parr << endl; //0x8f43ffd8 cout << *parr << endl; //Fred cout << *(parr + 1) << endl; //Susan cout << parr[2] << endl; //Mary cout << parr[0][1] << endl; //r }

More Related