1 / 56

Chapter 2.01

Chapter 2.01. C++ An Introduction. Control Unit & ALU : Integrated Circuits Central memory : Integrated Circuits Diversified computers: Low cost Personal Workstations Multi-processor supercomputers Embedded systems Networking and distributed processing Software :

slone
Download Presentation

Chapter 2.01

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. Chapter 2.01 C++ An Introduction

  2. Control Unit & ALU : Integrated Circuits Central memory : Integrated Circuits Diversified computers: Low cost Personal Workstations Multi-processor supercomputers Embedded systems Networking and distributed processing Software : Graphical User-interfaces Application oriented programming. Object oriented design & programming. Fourth Generation Systems1970-now

  3. C and C++ 1971-1973: Dennis Ritchie and Brian Kernighan design the C language to port the UNIX operating system from a PDP7 to a PDP11 1985 : Bjarne Stroustrup creates a new version of C taking into account sound rules of software engineering and enabling the object oriented programming style. He calls it C++

  4. A first example #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); }

  5. Upper- and Lower-case letters a b c d e ... A B C D E ... Digits 0 1 2 3 4 5 6 7 8 9 Special signs ; , . | + - * / = > < ( ) [ ] { } Composed signs /* */ // == != >= <= Reserved Words void double const do while Terminal Symbols

  6. Source A Source B Source C Source D Compiler X Compiler Y Assembler Reloc. A Reloc. B Reloc. C Reloc. D LINKER Object Code A+B+C+D Role of a Linker #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); }

  7. Comments Comments are parts of a program text that are not taken into consideration by compilers and interpreters. They are used to facilitate understanding of the program by human readers or to give general information such as the name of the author. Comments in C: /* This program computes the area of a circle */ Comments in C++: /* C style comments are used for comments that expand over several lines */ // Single line comments are also authorized.

  8. voidmain() { ... } The main function Every C or C++ program must contain a function called main Execution starts with that function. A normal function has arguments and computes a value. In this example the function main has no arguments()and computes no value ’void’. The entire code of the function is surrounded by { and } and constitutes a “block”

  9. PROGRAM = Description of data + Actions to perform upon these data Data Declarations #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } radius: dat 8 ;double variable pi: equ 3.1416 ;double constant area: dat 8 ;double variable

  10. Constants (Name) Value The value of a constant belongs to a type. Variables Name Type Type = set of all values the variable can have Constants and Variables const double pi = 3.1416; double radius, area; The type determines the internal representation of data as well as the operations that can be performed on the data

  11. Literal Constants Only a value: Named Constants A name and a value: The value can be a constant expression: ConstantsData whose value can not change during program execution 3.1416 "The area of this circle is " double pi = 3.1416; double twopi = pi * 2.0;

  12. Why named constants ? area := 6 * length*length; ... tax := (price * 6) / 100; area := 21 * length*length; ... tax := (price * 21) / 100; const int VAT = 6; ... area := 6 * length*length; ... tax := (price * VAT) / 100; const int VAT = 21; ... area := 6 * length*length; ... tax := (price * VAT) / 100;

  13. Simple Types: valuescan’t be decomposed Integer numbers : short int, int, long int, unsigned int Real numbers: float, double Characters : char Enumerated values : enum Boolean values (true,false) : boolean Addresses in data memory : pointer Structured Types: Values have several components All components have same type : array Components can have # types : struct More elaborated structures (only C++) : class Types in C and C++

  14. two’s complement integer numbers Number of bits: implementation dependant In most modern computers: 32 or 64 bits In embedded systems, often 16 bits (-32678 <= x < 32768) Operations: Addition + Substraction – Multiplication * Integer division /5 / 2 gives 2 Modulo (remainder) %5 % 2 gives 1 Relational operators==,!=,>=,<=,>,< The type int

  15. Floating point real numbers Number of bits: implementation dependant In almost all modern computers: IEEE754, 64 bit format Operations: Addition + Substraction – Multiplication * Real division /5.0 / 2.0 gives 2.5 Relational operators==,!=,>=,<=,>,< The type double

  16. Arithmetic expressions in C and C++ are evaluated according to the traditional precedence rules of arithmetic, from left to right, in a lazy way Aritmetic Expressions (x+z)*(x-y)*(x+z/(x-y)) When integers and reals are mixed in an arithmetic expression, the resulting value belongs to the real type The value of 5.0 / 2 is 2.5

  17. Quite often the same variable appears on the left and on the right side of an assignment.To reduce the typing effort and to help somewhat the compilation process, a shorthand that avoids typing twice the name of the variable has been introduced: Concatenated Operators x = x+1;can be written as x++; or ++x; x = x–1; x--; or --x; x = x+y; x =+ y; or x += y; For the precise interpretation of these operators, the reader should consult a reference book on C++ .

  18. Values are defined by the programmer enum day {mon,tue,wed,thu,fri,sat,sun}; day today; Enumerated types are represented by integers: By default: {0,1,2,3,…} Programmer can specify his/her own representations {mon=1,tue=2,wed=3,thu=4,fri=5,sat=-6,sun=-7} Operations: Relational operators==,!=,>=,<=,>,< Enumerated value can be assigned to integer variable Integer value can not be assigned enumerated variable The type enum

  19. Main benefit : clarity enum sex {male,female}; sex sexa,sexb; if (sexa == male) sexb = female else sexb = male; Much more clear than encoding by programmer: int sexa, sexb; // male encoded by 0, female by 1 sexb = 1 – sexa; The type enum

  20. Possible values: TRUE represented by any non zero value FALSE represented by a zero value Operators : not! , and&& , or|| X && Y X || Y Y = TRUE Y = TRUE Y = FALSE Y = FALSE X = TRUE X = TRUE TRUE TRUE FALSE TRUE X = FALSE X = FALSE FALSE TRUE FALSE FALSE The type boolean X = TRUE X = FALSE ! X FALSE TRUE

  21. a == b TRUE when a equals b a != b TRUE when a different from b a > b TRUE when a greater than b a < b TRUE when a less than b a >= b TRUE when a greater or equal b a <= b TRUE when a less or equal b Relational Expressions have a boolean value if((-1.0<=x)&&(x<=+1.0)) y = asin(x)

  22. Block Structures #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do {double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); }

  23. Block Structures A program is a set of nested blocks { ... { ... { ... } // RED { ... } // GREEN } // PURPLE } // BLUE

  24. Block Structures An identifier can be used everywhere within the block where it is declared { int a { float b { char c ... a =...; b =...; c =...; ... } X { ... a =...; b =...; c =...; ... } X ... a =...; b =...; c =...; ... } X X ... a =...; b =...; c =...; ... }

  25. Block Structures A global identifier can be redefined The local definition dominates { int a { float b { char a ... a = ‘$’; b =...; ... } X { ... a = 2; b =...; c =...; ... } X ... a =...; b =...; c =...; ... } X X ... a = 5; b =...; c =...; ... }

  26. Executable Instructions #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } PROGRAM = Description of data + Actions to perform upon these data

  27. Output Commands #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } Cout << constant string; Cout << integer, character or double variable; // \n in a string means “new line”

  28. Input Commands #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } cin >> integer, character or double variable;

  29. Arithmetic assignment #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } pihas a constant value (=3.1416) The value ofradiushas been entered throughcin The expression pi * radius * radius is evaluated and its value assigned to area

  30. Control Statements #include <iostream> void main() // This program computes the area of a circle { double radius; const double pi = 3.1416; cout << "Hello, I compute the area of a circle for you \n"; cout << "I will stop if you enter a value of 0. \n"; do { double area; cout << "Give me the radius in meters ? "; cin >> radius; area = pi * radius * radius; cout << " The area is : " ; cout << area; cout << " square meters \n"; } while (radius != 0); } {…} Radius != 0 FALSE

  31. Selection statements boolean selector : if statement Integer value selector : switch statement Iteration statements Initial termination test : while do loop Final termination test : do while loop Known number of iterations : for loop Function call : See chapter 2.2 Unstructured control statements Leave structured control statement : break Skip an iteration: continue ... Control Statements in C++

  32. B B FALSE TRUE FALSE TRUE S2 S1 S2 S1 if (B) S1 else S2; if(x%2 == 0) cout << “pair”; elsecout << “impair”;

  33. B B FALSE TRUE FALSE TRUE S S if (B) S; // absolute value of x if(x < 0) x = -x;

  34. e == a Yes S1 No e == b Yes No S2 e == c Yes No S3 S4 switch (e) switch(e){case a :S1;case b :S2; case c :S3;default:S4; } The value of the expression ecan belong to any of the types: int,enum,char.

  35. switch (e) switch(today){case mon:cout<<“get up”;case tue:cout<<“get up”; case wed:cout<<“try to get up”;case thu:cout<<“get up”;case fri:cout<<“get up”;case sat:cout<<“try to get up”;case sun:cout<<“sleep”;} A stupid example

  36. e == a Yes S1;break; No e == b Yes S2;break; No e == c Yes S3;break; No S4 switch (e), with break break Any structured control instruction break

  37. switch (e) switch(today){case mon:cout<<“get up”;break;case tue:cout<<“get up”;break; case wed:cout<<“try to get up”;break;case thu:cout<<“get up”;break;case fri:cout<<“get up”;break;case sat:cout<<“try to get up”;break;case sun:cout<<“sleep”;} A slightly less stupid example

  38. while (B) S; B B FALSE S TRUE S while(status != endoftape) {PlaySong}

  39. Specifications : Given two cardinal numbers x and y Compute g, the gcd of x and y Algorithm : gcd(x, y) = gcd(x, y-x) (x < y) gcd(x, y) = gcd(x-y, y) (x > y) while x # y x > y TRUE x = x-y y = y-x Computing the GCD (1)

  40. Top level design : Read value of x and y; Compute g, the gcd of x and y; Write the value of g ; Computing the GCD (2) #include<iostream> void main() { intx,y,g; // Read value of x and y ... // Compute g, the gcd of x and y ... // Write value of x,y and g ... cin >> x; // for keeping the window }

  41. Computing the GCD (4) #include <iostream> void main() {int x,y,g; // Read value of x and y cout << " Enter two positive integers "; cin >> x >> y; cout << "Thank you, you entered " cout << x << " and " << y << "\n"; // Compute g, the gcd of x and y … // Write value of x,y and g … cin >> x; }

  42. Computing the GCD (5) #include <iostream> void main() {int x,y,g; // Read value of x and y ... // Compute g, the gcd of x and y while (x != y) if (x > y) x = x - y; else y = y - x; g = x; // Write value of x,y and g ... cin >> x; }

  43. Computing the GCD (6) #include <iostream> void main() {int x,y,g; // Read value of x and y ... // Compute g, the gcd of x and y ... // Write value of x,y and g cout << "the gcd of " <<x <<" and " <<y cout <<" is " <<g <<"\n"; cin >> x; }

  44. Computing the GCD (7) Program states for 123 and 456 as inputs Executed instr. x y g Cin >>x >>y 123 456 ? y = y – x 123 333 ? y = y – x 123 210 ? y = y – x 123 87 ? x = x – y 36 87 ? y = y – x 36 51 ? y = y – x 36 15 ? x = x – y 21 15 ? x = x – y 6 15 ? y = y – x 6 9 ? y = y – x 6 3 ? x = x – y 3 3 ? g = x 3 3 3

  45. S S B TRUE B FALSE do S while (B) do {RemoveBolt} while (BoltsLeft != 0)

  46. init cond FALSE TRUE S change The for loop for (init,cond,change)S; Scope of variables declared here is the entire for loop for (int nbolt=1;nbolt <= 4;nbolt++) {Remove bolt number nbolt}

  47. Top level design : Read value of n; Compute fact = n!; Write the value of fact; Computing factorial n (1) #include<iostream> void main() { intfact,n; // Read value of n ... // Compute fact = n! ... // Write value of n and fact ... cin >> n; }

  48. Computing factorial n (2) #include <iostream> void main() { int n, fact = 1; // Read value of n ... // Compute fact = n! for (int i = 1; i <= n; i++) fact = fact * i; // Write value of n and fact ... }

  49. Computing factorial n (3) Program states for n = 5 Executed instr. n fact i cin >>n 5 1 i = 1 5 1 1 fact = fact * i 5 1 1 i++ 5 1 2 fact = fact * i 5 2 2 i++ 5 2 3 fact = fact * i 5 6 3 i++ 5 6 4 fact = fact * i 5 24 4 i++ 5 24 5 fact = fact * i 5 120 5

  50. The value of e is given by the infinite series: The error when limiting the calculation to term i: e = 1 + + + + + + 2 1 1 1 1 1 1 1 1 m! m! n! 3! 1! 3! 2! 2! 1! e – ( 1 + + + + + ) < Computing eby a convergent series Mathematics :

More Related