1 / 157

Programming in C++

Programming in C++. The Turbo C++ Environment C++ Program Structure Modular Programming with Functions C++ Control Structures Advanced Data Types Classes. Turbo C++ Environment. Windows based product Integrated Development Environment (IDE) editor compiler linker debugger.

dusty
Télécharger la présentation

Programming in C++

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. Programming in C++ • The Turbo C++ Environment • C++ Program Structure • Modular Programming with Functions • C++ Control Structures • Advanced Data Types • Classes

  2. Turbo C++ Environment • Windows based product • Integrated Development Environment (IDE) • editor • compiler • linker • debugger

  3. Structure of a C++ Program preprocessor directives main function header { declare statements statements }

  4. Using the Turbo C++ IDE • tool bars • menu • editor

  5. Using the Turbo C++ IDE (2) • compiling • linking • executing

  6. Developing Programs • Understand the problem • Design a solution to the problem • including test cases and the solutions to the test cases • Implement and document • Translate the solution to a programming language

  7. Developing Programs (2) • Verify • Test and debug the solution • using test cases from design phase • Maintain

  8. Problem Solving (1) consider a trapezoid -- 4 sided figure in which two sides are ||, the area is 1/2 the product of the height and the sum of the lengths of the two bases. b1 h b2 Area = (b1 + b2)h/2

  9. Problem Solving -- Trapezoid Pseudocode input b1 input b2 input height bases = b1 + b2 area = bases * h /2 output area

  10. Problem Solving (2) consider finding the area and circumference of a circle pi = 3.14159 area = pi * radius2 circumference = 2 * pi * radius

  11. Problem Solving -- Circle Functions Pseudocode pi = 3.14159 input radius circum = 2 * pi * radius area = pi * radius*radius output area output circum

  12. Problem Solving (3) consider converting temperatures from Centigrade to Fahrenheit (or vice versa) where c = 5/9(f-32) f = 9/5c + 32

  13. Problem Solving --Temperature Conversion Pseudocode input temp input scale if scale = = ‘f’ newtemp = 5/9 (temp-32) else newtemp = 9/5 temp + 32 output newtemp

  14. Problem Solving (4) consider sales commissions based upon the number of sales made during the time period $8 per sale for < 15 sales $12 per sale = 15 sales $16 per sale > 15

  15. Problem Solving -- Commission Pseudocode • quota = 15 • input number_sales • if number_sales < quota • rate = 8 • else if number_sales == quota • rate = 12 • else rate = 16 • com = rate * number_sales • output com

  16. Problem Solving -- Commission Pseudocode Multiple Salespeople quota = 15 input number_salespeople

  17. Problem Solving -- Pseudocode Multiple Salespeople (2) • loop number_salespeople times • input number_sales • if number_sales < quota • rate = 8 • else if number_sales == quota • rate = 12 • else rate = 16 • com = rate * number_sales • output com

  18. Exercise -- GO Develop a series of problems for the students to do using each of the statement types

  19. Introduction to the C++ Language • keywords • C++ is case-sensitive • identifiers • can not be keywords • comments • enclosed in /* */ multi-line • start with // single line

  20. Preprocessor Statements library header files -- #include < > -- system library #include <iostream.h> “ “ -- personal library #include “apstring.h”

  21. Data Types and Declarations • declare statement • allocates memory and assigns “name” • data type name [= initial value]; • int -- 2 bytes • float -- 4 bytes • double -- 8 bytes • char -- enclosed in ‘ ‘

  22. User Defined Data Types • class -- mechanism to establish new data types • ap classes • string • apstring.h apstring.ccp • bool • bool.h

  23. int a; int a,b,c; float x, y; double average = 0.0; char answer = ‘Y’; bool another; bool more = false; apstring name; apstring class = “C++”; Example Declare Statements

  24. Input and Output Statements #include <iostream.h> cout -- output << insertion character cin -- input >> extraction character

  25. Using APSTRING Class • #include “apstring.h” • entire path • create project • place apstring and program in project • you will need a different project for each program

  26. Input and Output Statements (2) • COUT -- control codes • way of inserting placement control • \n -- new line • \t -- tab • iomanip.h • contains more formatting methods

  27. Arithmetic in C++ • operator precedence ( ) *, /, % (left to right) +, - (left to right) • integer arithmetic • operations involving integers yielding integer results • truncation on integer division • % -- modulo operator

  28. Arithmetic in C++ (2) • mixed mode arithmetic • operands of different data types • hierarchy double/float/int • highest mode is used • determined on a operation by operation basis

  29. Assignment Statements • assignment operator = • operator precedence and mixed mode arithmetic hold • combination operators +=, -=, *=, /=, %= • variable = expression;

  30. Increment and Decrement Statements • special operators which add or subtract one from a variable • more efficient (generates inc, dec) • a++; ==> a = a+1; • a--; ==> a = a -1; • postfix (a++;) (a--;) • done after the expression is evaluated • prefix (++a;) (--a;) • done prior to evaluating the expression

  31. Type Casting • changes the evaluation data type of the expression • does not change the data type of the variable • (data type) • (int) • (float) • (apstring)

  32. Programming Problems • convert distance in miles to distance in kilometers and meters • 1 mile = 1.61 km, 1 km = 1000 meter • convert a temperature in Celsius to Kelvin • -273.15oC = 0oK • convert a temperature in Fahrenheit to Kelvin • -459.67oF = 0oK

  33. Mathematical Functions (math.h) • code reuse • sqrt, pow, exp, log, log10 • abs, ceil, floor • trigonometric functions

  34. Programming Problems • determine the volume of a sphere with an input radius • volume = (4 * pi * radius3)/3 • determine the area of a triangle when given length of two sides and the included angle in degrees • degrees = 180 * radians / pi • area = side1 * side2 * sin (radians) / 2

  35. Programming Problems (2) • determine the distance from a point on the Cartesian plane to the origin • distance = sqrt (x2 + y2)

  36. Exercise -- GO Implement the sequential problems developed in the first exercise

  37. Modular Programming with Functions • designed in small segments • each segment implemented as a function • sqrt, pow, sin • self contained • requires input through parameters • sends output through name • Abstraction • know what the function does and what it needs to do its task • not how the function works

  38. Modular Programming with Functions (2) • allows for reuse • eliminates redundancy • allows for team development • simplifies logic

  39. Form of a Function [return data type] Function name (parameter list) { [declarations] statements [return ] }

  40. Types of Functions • no input, no return value • void • input but no return value • both input and output • no input but returns a value

  41. Example -- Circle Functions • calculate the area and circumference of a circle of an input radius • input radius • calculate area • calculate circumference • output results • invoke the functions • use name and parameters in an expression • functions must be defined before they can be used

  42. Example -- Pythagorean Triples • Pythagorean Triple are the three sides of a right triangle a,b,c • a2 + b2 = c2 • given m and n, such that m>n we can generate the triples • a = m2 - n2 • b= 2mn • c = m2 + n2

  43. Call by Value • on invocation the value of the actual parameter is copied into the formal parameter • when the function terminates the value IS NOT copied back to the actual parameter • can not change the value of a parameter within the function

  44. Example Call by Value #include <iostream.h> int test (int n) { int i = 5; n +=i; return (n); } void main (void) { int n=1, i; i = test (n); cout << i << “ = “ << n << endl; }

  45. Example Call by Value (2) main test 1 n 1 6 n i 6 i 5

  46. Functions -- Pass by Reference • returns 0 or 1 value through name • need to return more than 1 • swap the values of two variables • change the values of parameters • bank deposit or check • pass the “name” of the parameter rather than its value so that the function uses the same memory location as the actual parameter

  47. Reversing Order -- Swap if (num1 < num2) { temp = num1; num1 = num2; num2 = num1; }

  48. Reference Parameters • Parameter which shares the memory of the actual parameter rather than declare new memory and copy the actual’s value into it • Parameter declaration int & x; • x is an alias for the actual integer parameter double & y • y is an alias for the actual double parameter

  49. Function Swap void swap (int & num1, int & num2) { int temp; temp = num1; num1 = num2; num2 = temp; } if a > b swap (a,b); to invoke the function

  50. Call by Value vs Reference • Use reference vs return type • all input • when need to return more than 1 value • always have return type void • Use value • all other cases • no side effects

More Related