1 / 99

Introduction to C++

Introduction to C++. Computer Science I. Quote. “Language is the only instrument of science….” Samuel Johnson. Q: What is C++. C++ is a compiled, object-oriented language It is the “successor” to C, a procedural language (the “++” is called the successor operator in C++)

enan
Télécharger la présentation

Introduction to 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. Introduction to C++ Computer Science I

  2. Quote... • “Language is the only instrument of science….” • Samuel Johnson

  3. Q: What is C++ • C++ is a compiled, object-oriented language • It is the “successor” to C, a procedural language • (the “++” is called the successor operator in C++) • C was derived from a language called B which was in turn derived from BCPL • C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs • C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell Labs. • Most of C is a subset of C++

  4. People & Programs • User: an individual who runs, or executes, a program • Programmer: an individual who creates, or writes, a program

  5. C++ Program Consists of… • Declarations • Define the use of various identifiers, thus creating the elements used by the program (computer) • Statements • Or executable statements, representing actions the computer will take on the user’s behalf

  6. Identifiers • Names for various entities used in a program; used for... • Variables: values that can change frequently • Constants: values that never changes • Functions: programming units that represents complex operations • Parameters: values that change infrequently

  7. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Compiler directive: Tells the compiler what to do before compiling • This one includes source code from another file

  8. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Main function

  9. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Header for main function • States… • data type for the return value • identifier for function • list of arguments between parenthesis(none for this function)

  10. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Braces enclose the body of the function • They represent the start and end of the function

  11. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Declarations and statements • Main body of function (or main part) • “//” represents the start of a comment

  12. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Return statement • specifies the value the function returns • All (almost) declarations and statements end with a semi-colon “;”

  13. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • This program doesn’t do anything!

  14. Sample C++ Program #include <iostream.h> void main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; } • Variable declaration • The identifier number is declared as being of data type int, or integer

  15. Sample C++ Program #include <iostream.h> void main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; } • coutthe output statement for C++ • Note the direction of “<<“ • endl represents an end-of-line

  16. Sample C++ Program #include <iostream.h> void main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; } • cinthe input statement for C++ • Note the direction of “>>”

  17. Sample C++ Program #include <iostream.h> void main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; } • Did you copy this down? • You had better, since this will be the first program you’ll try!

  18. Sample C++ Program #include <iostream.h> void main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; } • That means right now!

  19. Assignment • Assignment is an operation that assigns the value of an expression to a variable • Ex. Total = 2 + 3 + 5 • First, the expresssion “2 + 3 + 5” is evaluated • Then, this value is assigned to the variable “Total”

  20. Assignment • When a variable is declared, space is allocated in the computer’s memory for the variable • Each data type requires a different number of bytes in memory for storing a variable int - 2float - 4double - 8char, bool - 1

  21. Assignment • When a variable is assigned a value, the value is placed into the variable’s memory location 10 Total = 2 + 3 + 5; Total

  22. Arithmetic Operations • Addition: 2 + 3 • Subtraction: 5 - 2 • Multiplication: 10 * 4 • Division: 12 / 3

  23. Order of Operations • Arithmetic expressions are evaluated according to the following order of operations • At each level, operations are evaluated left to right • (1) Parenthesis, Functions(2) Multiplication, Division(3) Addition, Subtraction

  24. Parenthesis • Parenthesis are used to alter the order with which operations are evaluated • Ex. 4 + 5 * 2 equals 14 (4 + 5) * 2 equals 18

  25. Here we go! • Problem: To determine the average of three numbers • Task: Request, from the user, three numbers, compute the average and the three numbers, and print out the original values and the computed average • Do it! • You have 20 minutes!

  26. Computer Science I Functions

  27. Q: What is a function? • A programming unit • Similar to mathematical functions • Example: • f(x) = x2 + 5x + 7 • For x = 2, f(2) = (2)2 =5(2) + 7 = 21

  28. Q: What is a function? • It has... • ... arguments • ... a name (identifier) • ... a value it returns • ... a body int foo(int x) {int result;result = x*x + 5*x + 7;return result; }

  29. Procedural Abstraction • Think “Black Box” ! • When using a function, you only need to be concerned with what it does, nothow it does it • When writing a function, you need to be concerned with the how

  30. Example: Cube it! int cubeIt(int x){ int result; result = x*x*x; return result;} int cubeIt(int x){ int result; result = x; result = x*result; result = x*result; return result;}

  31. Pseudocode • Looks like a programming language • Has all the structure of a programming language • Has a verrrrrry loose syntax

  32. Pseudocode • Example:function foo (x) result  x2 + 5x + 7 return result • That’s it! • Sloppy, ain’t it?

  33. Computer Science I Decision Statements

  34. Q: What is a decision? • Something that represents a branching point in a solution • Outcomes are often dependent on initial conditions

  35. Decisions in Programs • Without decision statements (or other dynamic control structures), programs are static • Static programs do exactly the same things each time they are executed • Dynamic programs do not

  36. Boolean Algebra • Based on values that are either True or False • True and False values are often represented by 1’s and 0’s, respectively

  37. Logical Operations: And • AB • Expression is True iffA and B are both true

  38. Logical Operations: Or • AB • Expression is True if either A or B are True • Note: Also True when A and B are both True

  39. Logical Operations: Exercises A = True, B = True, C = False 1. AB 2. AC 3. ABC 4. (AB)  (AC)

  40. Relational Operations • A < B “A less than B” • A > B “A greater than B” • A = B “A equal to B” • A  B “A less than or equal to B” “A not greater than B” • A  B “A greater than or equal to B” “A not less than B” • A  B “A not equal to B” “A less than or greater than B”

  41. Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. AC 3. (A < C)  (B < C)

  42. Boolean Operations: C++ • A  B • A  B • A < B • A > B • A = B • A  B • A  B • A  B • A && B • A | | B • A < B • A > B • A = = B • A > = B • A < = B • A < > B

  43. Try this! Problem: • You’d like to go see a movie. • The movie costs $8.00, a soda costs $2.50 and a large popcorn costs $4.50. • Based on the amount of money in your pocket, determine whether you could...(a) See the movie and buy a soda, (b) See the movie, and buy soda and popcorn, or(c) Stay home

  44. Know? • Movie costs $8.00 • Soda costs $2.50 • Popcorn costs $4.50 • How much money I have in my pocket

  45. Need? • Cost of movie and soda • Cost of movie, soda and popcorn • Way to select one of the three options(that is, make a decision!)

  46. Do? • Option (a) costs $10.50 • Option (b) costs $15.00 • Option (c) costs nothing • What next?

  47. How about a diagram? • This is called a flowchart Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  48. How about a diagram? • Boxes represent actions Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  49. How about a diagram? • Diamonds represent decision points Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

  50. How about a diagram? • Arrows show flow Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda

More Related