1.02k likes | 1.21k Views
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++)
E N D
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++) • 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++
People & Programs • User: an individual who runs, or executes, a program • Programmer: an individual who creates, or writes, a program
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
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
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
Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Main function
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)
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
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
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 “;”
Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • This program doesn’t do anything!
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
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
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 “>>”
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!
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!
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”
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
Assignment • When a variable is assigned a value, the value is placed into the variable’s memory location 10 Total = 2 + 3 + 5; Total
Arithmetic Operations • Addition: 2 + 3 • Subtraction: 5 - 2 • Multiplication: 10 * 4 • Division: 12 / 3
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
Parenthesis • Parenthesis are used to alter the order with which operations are evaluated • Ex. 4 + 5 * 2 equals 14 (4 + 5) * 2 equals 18
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!
Computer Science I Functions
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
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; }
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
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;}
Pseudocode • Looks like a programming language • Has all the structure of a programming language • Has a verrrrrry loose syntax
Pseudocode • Example:function foo (x) result x2 + 5x + 7 return result • That’s it! • Sloppy, ain’t it?
Computer Science I Decision Statements
Q: What is a decision? • Something that represents a branching point in a solution • Outcomes are often dependent on initial conditions
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
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
Logical Operations: And • AB • Expression is True iffA and B are both true
Logical Operations: Or • AB • Expression is True if either A or B are True • Note: Also True when A and B are both True
Logical Operations: Exercises A = True, B = True, C = False 1. AB 2. AC 3. ABC 4. (AB) (AC)
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”
Relational Operations: Exercises A = 5, B = 3, C = -7 1. A < B 2. AC 3. (A < C) (B < C)
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
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
Know? • Movie costs $8.00 • Soda costs $2.50 • Popcorn costs $4.50 • How much money I have in my pocket
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!)
Do? • Option (a) costs $10.50 • Option (b) costs $15.00 • Option (c) costs nothing • What next?
How about a diagram? • This is called a flowchart Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Boxes represent actions Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Diamonds represent decision points Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda
How about a diagram? • Arrows show flow Money < $10.50 Money < $15.00 Stay home Movie, soda & popcorn Movie & soda