Download presentation
Presentation is loading. Please wait.
Published byDeborah Terry Modified over 9 years ago
2
Introduction to C++ Programming
3
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides a comprehensive set of features for handling a wide variety of applications, such as systems development and scientific computation. l C++ is an “extension” of the C language, in that most C programs are also C++ programs. l C++, as opposed to C, supports “object- oriented programming.”
4
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 3 General form of a C++ program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }
5
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 4 C++ keywords l Keywords appear in blue in Visual C++. l Each keyword has a predefined purpose in the language. l Do not use keywords as variable and constant names!! l The complete list of keywords is on page 673 of the textbook. l We shall cover the following keywords in this class: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while
6
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 5 Example 0 – adding 2 numbers l Peter: Hey Frank, I just learned how to add two numbers together. l Frank: Cool! l Peter : Give me the first number. l Frank: 2. l Peter : Ok, and give me the second number. l Frank: 5. l Peter : Ok, here's the answer: 2 + 5 = 7. l Frank: Wow! You are amazing! l after Frank says “2”, Peter has to keep this number in his mind. 257 First number: Second number: Sum: l after Frank says “5”, Peter also needs to keep this number in his mind.
7
The Corresponding C++ Program #include using namespace std; int main() { int first, second, sum; cout << "Peter: Hey Frank, I just learned how to add” << “ two numbers together."<< endl; cout << "Frank: Cool!" <<endl; cout << "Peter: Give me the first number."<< endl; cout << "Frank: "; cin >> first; cout << "Peter: Give me the second number."<< endl; cout << "Frank: "; cin >> second; sum = first + second; cout << "Peter: OK, here is the answer:"; cout << sum << endl; cout << "Frank: Wow! You are amazing!" << endl; return 0; }
8
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 7 #include using namespace std; int main() { int number_of_pods, peas_per_pod, total_peas; cout << "Press return after entering a number.\n"; cout << "Enter the number of pods:\n"; cin >> number_of_pods; cout << "Enter the number of peas in a pod:\n"; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod; Demo Example 1
9
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 8 cout << "If you have "; cout << number_of_pods; cout << " pea pots\n"; cout << "and "; cout << peas_per_pod; cout << " pea in each pod, then \n"; cout << "you have "; cout << total_peas; cout << " peas in all the pods.\n"; return 0; } Demo Example 1
10
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 9 Identifiers appear in black in Visual C++. n An identifier is a name for a variable, constant, function, etc. n It consists of a letter followed by any sequence of letters, digits, and underscores. Examples of valid identifiers: First_name, age, y2000, y2k Examples of invalid identifiers: 2000y Identifiers cannot have special characters in them. For example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers. Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers. C++ identifiers
11
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 10 C++ comments l Comments appear in green in Visual C++. l Comments are explanatory notes; they are ignored by the compiler. l There are two ways to include comments in a program: // A double slash marks the start of a //single line comment. /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */
12
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 11 C++ compiler directives l Compiler directives appear in blue in Visual C++. The #include directive tells the compiler to include some already existing C++ code in your program. l The included file is then linked with the program. There are two forms of #include statements: #include //for pre-defined files #include "my_lib.h" //for user-defined files
13
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 12 Programming Style C++ is a free-format language, which means that: l Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. l Blank lines are ignored by the compiler just like comments. l Code can be indented in any way. l There can be more than one statement on a single line. l A single statement can continue over several lines.
14
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 13 In order to improve the readability of your program, use the following conventions: l Start the program with a header that tells what the program does. l Use meaningful variable names. l Document each variable declaration with a comment telling what the variable is used for. l Place each executable statement on a single line. l A segment of code is a sequence of executable statements that belong together. n Use blank lines to separate different segments of code. n Document each segment of code with a comment telling what the segment does. Programming Style (cont. )
15
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 14 What makes a bad program? l Writing Code without detailed analysis and design l Repeating trial and error without understanding the problem l Debugging the program line by line, statement by statement l Writing tricky and dirty programs
16
PROGRAMMER'S DRINKING SONG!! 100 little bugs in the code, 100 bugs in the code, fix one bug, compile it again, 101 little bugs in the code. 101 little bugs in the code … Repeat until BUGS = 0 — The Internet Joke Book
17
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 16 Examples // my second program in C++ #include using namespace std; int main () { cout << "Hello World! "; cout << "I'm a C++ program"; return 0; } int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }
18
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 17 Oprators // defined constants: calculate circumference #include using namespace std; #define PI 3.14159 #define NEWLINE '\n‘ int main () { double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle; cout << NEWLINE; return 0; } l 31.4159
19
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 18 +addition -subtraction *multiplication /division %modulo C++ Oprators
20
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 19 expressionis equivalent to value += increase;value = value + increase; a -= 5;a = a - 5; a /= b;a = a / b; price *= units + 1;price = price * (units + 1);
21
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 20 expressionis equivalent to value += increase;value = value + increase; a -= 5;a = a - 5; a /= b;a = a / b; price *= units + 1;price = price * (units + 1); l Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
22
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 21 l // compound assignment operators l #include l using namespace std; l l int main () l { l int a, b=3; l a = b; l a+=2; // equivalent to a=a+2 l cout << a; l return 0; l }
23
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 22 Increase and decrease (++, --) c++; c+=1; c=c+1 ;
24
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 23 Relational and equality operators ( ==, !=, >, =, <= ) ==Equal to !=Not equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to (7 == 5) // evaluates to false. (5 > 4) // evaluates to true. (3 != 2) // evaluates to true. (6 >= 6) // evaluates to true. (5 < 5) // evaluates to false.
25
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 24 12341234 !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true. !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true. ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).
26
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 25 operatorasm equivalentdescription &ANDBitwise AND |ORBitwise Inclusive OR ^XORBitwise Exclusive OR ~NOT Unary complement (bit inversion) <<SHLShift Left >>SHRShift Right
27
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 26 Conditional structure: if and else If (condition) statement If (x == 100) cout << "x is 100";
28
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 27 If statement complex if (x == 100) { cout << "x is "; cout << x; } if (x == 100) cout << "x is 100"; Else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";
29
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 28 The while loop l While (expression) l statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }
30
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 29 The do-while loop l do statement while (condition);
31
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 30 int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }
32
COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 31 The for loop l for (initialization; condition; increase) statement ; l int main () l { l for (int n=10; n>0; n--) l { l cout << n << ", "; l } l cout << "FIRE!\n"; l return 0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.