intro2cs-week1-2.pdf
Document Details
Uploaded by DignifiedSapphire
Tags
Full Transcript
1 introduction to computer science lecture 1 : intro part 2 thanh-trung phan objectives 2 thanh-trung phan § Why C++? § Hardw...
1 introduction to computer science lecture 1 : intro part 2 thanh-trung phan objectives 2 thanh-trung phan § Why C++? § Hardware/Software § To learn fundamental problem solving techniques § To learn how to design a program § To learn to use the basic of C++ programming language What is C++? 3 thanh-trung phan § C++ is a programming language § A computer program performs a specific task, and may interact with the user and the computer hardware. Why is C++? 4 thanh-trung phan § Bad news: C++ is not easy to learn § Good news: Lots of good-paying jobs for programmers because C++ is not easy to learn! Java uses C++ syntax, it is easy to learn Java if you know C++. Though C++ is not the easiest language (Basic and Pascal are easier), it is not the hardest either (Ada, Prolog and Assembly languages are really difficult!) Who uses C++? 5 thanh-trung phan § Computer makers such as Sun, SGI, IBM, and HP § Airport § Computer chip manufacturers like Motorola & Intel § Software companies § Banks § Hong Kong Government § Hospital Authority § Telecommunications § Universities NAME EVENT / NAME PRESENTATION hardware and software 8 Speaker Hardware 9 thanh-trung phan § Four components of a computer system: CPU -central processing unit § Makes decisions, performs computations, and delegates input/output requests Memory: Disk Drives, CD drives, Tape drives, USB flash drives. § Stores information Input devices: Keyboard, Mouse, § Gets information from the user to the computer Output devices: monitor § Sends information from computer to the user NAME EVENT / NAME PRESENTATION Hardware thanh-trung phan 10 NAME EVENT / NAME PRESENTATION Software thanh-trung phan 11 Software 12 thanh-trung phan § Application software Easy-to-use programs designed to perform specific tasks § System software Programs that support the execution and development of other programs Two major types § Operating systems § Translation systems (compilers & linkers) thanh-trung phan 13 Computer Software Relationships 14 thanh-trung phan Application Software 15 thanh-trung phan § Application software makes computer popular and easy to use § Common application software: Microsoft Word, WordPerfect PowerPoint … Operating System 16 thanh-trung phan § Controls and manages the computing resources § Examples Windows, MacOS, iOS,… § Important services that an operating system provides: Security: prevent unauthorized users from accessing the system Commands to manipulate the file system Input and output on a variety of devices Window management What is a (programming) language? 17 thanh-trung phan § A program needs to be written in a language § There are many programming languages Low-level, understandable by a computer High-level, needs a translator! § C++ is a high level programming language Levels of programming language 18 thanh-trung phan § Machine binary language: unintelligible § Low-level assembly language Mnemonic names for machine operations Explicit manipulation of memory addresses Machine-dependent § High-level language Readable Machine-independent An example thanh-trung phan 19 How to translate? 20 thanh-trung phan § Examples of compilers: Microsoft Visual C++, Eclipse, g++ Translation System 21 thanh-trung phan § Set of programs used to develop software § Types of translators: Compiler Linker § Examples Microsoft Visual C++ Eclipse g++ Software Development 22 thanh-trung phan § Major activities: Editing (writing the program) Compiling (creates.obj file) Linking with compiled files (creates.exe file [for files on Window]) § Object files § Library modules Loading and executing Testing the program Integrated Development Environments 23 thanh-trung phan § Combine all of the capabilities that a programmer would want while developing software (VC++2008, Eclipse, Xcode, CLion) Editor Compiler Linker Loader Debugger Viewer Our First Program 24 thanh-trung phan thanh-trung phan 25 26 Speaker let’s make your fingers dirty … by installing CLion NAME EVENT / NAME PRESENTATION Step 1 : Download 27 Speaker CLion § Visit the official JetBrains website: www.jetbrains.com/clion § Click on Download. § Select the macOS/ Windows/Linux version. NAME EVENT / NAME PRESENTATION Step 2: Open the 28 Speaker Installer § Locate the CLion-*.dmg/ CLion-*.exe file in your Downloads folder after downloading. § Double-click the.dmg/.exe file to open the installer. § Follow and finish the installation NAME EVENT / NAME PRESENTATION § Log in with your JetBrains account to activate your license, or start a free 30- day trial. Use your FUV email to register education account for free registration. § Create your first project and enjoy!!! NAME EVENT / NAME PRESENTATION Speaker 29 General form of a C++ program 30 thanh-trung phan C++ keywords 31 thanh-trung phan § Keywords appear in color. § Each keyword has a predefined purpose in the language. § Do not use keywords as variable and constant names!! § 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 Example 0 – adding 2 numbers 32 thanh-trung phan § Peter: Hey Frank, I just learned how to add two numbers together. § Frank: Cool! § Peter: Give me the first number. § Frank: 2. § Peter: Ok, and give me the second number. § Frank: 5. § Peter: Ok, here's the answer: 2 + 5 = 7. § Frank: Wow! You are amazing! after Frank says “2”, Peter has to keep this number in his mind. after Frank says “5”, Peter also needs to keep this number in his mind. First number: 2 Second number: 5 Sum: 7 The Corresponding C++ Program 33 thanh-trung phan Demo Example 1 34 thanh-trung phan Demo Example 1 35 thanh-trung phan C++ identifiers 36 thanh-trung phan Identifiers appear in black in Visual C++ or white in CLion. § An identifier is a name for a variable, constant, function, etc. § 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++ comments 37 thanh-trung phan § Comments appear in green in Visual C++ or gray in CLion. § Comments are explanatory notes; they are ignored by the compiler. § There are two ways to include comments in a program: C++ compiler directives 38 thanh-trung phan § Compiler directives appear in blue in Visual C++ or yellow in CLion. § The #include directive tells the compiler to include some already existing C++ code in your program. § The included file is then linked with the program. § There are two forms of #include statements: Programming Style (cont. ) 39 thanh-trung phan In order to improve the readability of your program, use the following conventions: § Start the program with a header that tells what the program does. § Use meaningful variable names. § Document each variable declaration with a comment telling what the variable is used for. § Place each executable statement on a single line. § A segment of code is a sequence of executable statements that belong together. Use blank lines to separate different segments of code. Document each segment of code with a comment telling what the segment does. #include // Include the iostream library for input/output operations #include // Include the cmath library for mathematical functions int main() { // Variable declaration double radius; // Holds the radius of the circle double area; // Holds the calculated area of the circle double circumference; // Holds the calculated circumference of the circle 41 thanh-trung phan // Prompt the user for the radius of the circle std::cout > radius; // Calculate the area and circumference of the circle area = M_PI * radius * radius; // Area = π * r^2 circumference = 2 * M_PI * radius; // Circumference = 2 * π * r // Output the calculated area and circumference std::cout