Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG

Similar presentations


Presentation on theme: "Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG"— Presentation transcript:

1 Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG
THE HONG KONG POLYTECHNIC UNIVERSITY Computer Programming Department of Electronic and Information Engineering Lecturer: Ir Dr Frank H.F. LEUNG Room: CD633 Tel: On-line tutorial in Blackboard ENG2002_20161_x: Computer Programming  Discussions

2 References: S. Rao, Sams Teach Yourself C++ in One Hour a Day (7th ed.) Indianapolis, Ind.: Sams, 2012. P.J. Deitel and H.M. Deitel, C++ How To Program (9th ed.) Boston, MA: Prentice Hall, 2014. J Liberty and R. Cadenhead, Sams Teach Yourself C++ in 24 hours (5th ed.) Indianapolis, Ind.: Sams, 2011.

3 Contents What is Computer Programming? A Taste of C++
Nuts and Bolts of C++ Program Flow Control Program Design and Debugging Objects and Classes in C++ Pointers and Arrays Stream I/O

4 What is Computer Programming?
Getting Started What is Computer Programming?

5 Computer Programming What is Computer Programming? Computer Programming A systematic approach to instruct a computer doing something for us. Hardware I/O devices CPU Memory

6 (Compile, link and load)
Process of Computer Programming Way of thinking Computer Computing Problems Operating System Hardware Algorithms and Data Structures Mapping Coding Application Software High-level Language Binding (Compile, link and load)

7 Coding Computer can only manipulate 0 and 1
The most direct way to communicate with the computer is to use 0 and 1  Machine Language Programming : Machine Language Program Very tedious and can make error easily

8 Coding - Assembly Language
Assembly Language is created to help human instructing CPU to work By using a software tool called Assembler, assembly language programs can be converted into machine language programs. Assembly Language Machine Language LD R0, #05 ADD R0, $1234 : SUB R0, #22 LD $2345, R0 : Assembler

9 Coding - High Level Language
Assembly language programming is still too complicated for general users They are far from human used language, e.g. Set W equal to W plus X minus Y divided by Z Repeat the next sequence of instructions until X is less than 0 or Y equals Z A high level language is required to reduce the gap between human and computers.

10 Compiler Linker Start Give me the result of adding 1 to 10 1011010101
High Level Language Program Start Details in the following slides Compiler Object code .cpp file : Executable Machine Language Program .exe file Finish Linker Library

11 Compiler: Generate the object code
Object code  Similar to the machine language code but cannot be executed because of the missing of some information. Linker: Resolve the missing information from the library or other programs. For a program, it is very common that some of the information in the program needs to be obtained from the Library provided by the programming tool. It is the job of linker to obtain these missing information.

12 Coding - High Level Languages
From time to time, high level languages (HLL) are designed for different purposes Earlier examples: BASIC: for simple and general computing (obsolete for long) COBOL: for data processing (still using in banks) FORTRAN: for scientific computation (ever replaced by C or C++) Recent examples: Pascal: a sophisticated but complex HLL (replaced by C or C++) C: a simple structural HLL for general computing (still in use) Current examples: C++: the most well-known object-oriented (OO) language Java: an OO language designed with the concept of networking.

13 Programming Language C++
The Object-Oriented programming concept is overwhelming OO Programming has the advantage of achieving reuse of software components C++ is an extension to the popular C language The C++ programming language is created to implement the concept of OO programming It has become the predominant language for the development of commercial software Standardized by the American National Standards Institute (ANSI), C++ program is portable to different machines.

14 Getting Started A Taste of C++ Learn the development environment

15 Programming Environment
Before doing any C++ programming, first we need to familiarize with the programming environment. A programming environment is a software package that can help us develop and test program code. For PCs, one of the most popular programming environment is the Microsoft Visual Studio. It is more popular because of the direct support to and from the Microsoft Windows environment.

16 Visual Studio Community 2015
Visual C++ is one of the components of the product – Visual Studio, which also contains development tools for other programming languages, e.g. Visual Basic, Visual C#, etc. The version we use is Visual Studio Community (Visual Studio Enterprise 2015 in lab). The compiler that is incorporated is Visual C Additional features include Produce C++/CLI codes that run in the Common Language Runtime (CLR) Allow the use of .NET Framework class libraries. NOT covered in ENG2002

17 Solution and Project They are all in files
In Visual Studio, each user task is referred to as a solution to a problem. A solution refers to the computer resource (such as memory, disk space, etc.) required for one or more projects to complete the task. Every project contains executable code and debug information the C++ programs (source code) classes and objects used in the project other related resources such as icon images used in the project graphical user interface (GUI), e.g. button, menu. ONE solution ONE project in this subject They are all in files NOT covered in ENG2002

18 A Solution must contain at least one Project
Files A Solution must contain at least one Project Solution Project 1 A Project must contain at least one File Project 2 Project 3 ONE solution ONE project in this subject ENG2002

19 Build your first project - HelloWorld
To Build a project means to produce target executable file(s) (.exe file) based on the program code. Project can be built to execute in console or windows mode Console mode: Use the Command Prompt for input and output Windows mode: Use the Windows environment for program execution. NOT covered in ENG2002

20 Command Prompt It is a traditional interface between the user and computer The user types in commands to instruct the computer to work.

21 Build your first project - HelloWorld
a.cpp: File containing a C++ program that prints a message "Hello World!" on the console output, i.e. the display screen The returned type of main() MUST be declared in Visual Studio 2015 (void is needed if you return nothing.) #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }

22 Step 1: Start Visual Studio 2015 Click New Project...
First time starting may take minutes, be patient. Step 1: Start Visual Studio Click New Project...

23 Step 2 You can modify the location of the files of your project.
Note where you create this project

24 Click Next > NEVER click Finish

25 Step 3

26 Step 4: Right-click Source Files, select Add → New Item...

27 Step 5: Choose C++ File (.cpp).
Give a name to the C++ file you are going to add

28 Display compilation results
Solution Explorer Display resources of the solution, such as, projects and files inside Source Program Editing Display compilation results

29 Step 6: Type the program on p.21 in here Click Build → Build Solution
See the built result

30 Step 7: Execute the program by clicking
Debug → Start Without Debugging Do you see the following result? Note: The result is shown in the Command Prompt (cmd.exe) because we are developing a Console Application

31 What actually has been done?
C++ source file: a.cpp In this HelloWorld example, one solution is created that contains a project called chap2p1. Inside the project, there is one C++ source file called a.cpp. Solution Project: chap2p1

32 All files generated in this project is stored in C:\VS_Proj\chap2p1
The C++ Compiler of Visual Studio compiles the program in a.cpp and generates an executable file named chap2p1.exe. When you click Debug → Start Without Debugging, Visual Studio actually runs the executable file chap2p1.exe for you. All files generated in this project is stored in C:\VS_Proj\chap2p1 Build Seen from Windows Explorer

33 Windows Explorer Contains files created after building the solution Contains info. about the project. Double-click this will automatically start the project The C++ source file you created

34 The EXE (executable) file created

35 We can always run the program under Command Prompt

36 Exercise 2.1 Consider the two programs on the RHS. For each program, construct a new project in Visual Studio and then build the solution. For each program, note the error messages on building the project. Fix the errors and execute them so that you can read the string "C++" on the screen. #include <iostream> int main() std::cout << "C++" << endl; return 100; } #include <iostream> int Main() { std::cout << "C++" << std::endl; return 0; }

37 Exercise 2.2 Observe the output and explain.
1. Build the project in P.21. Open the Command Prompt. By using the command cd, change the current folder to the folder that contains chap2p1.exe. (You may want to use the command dir to show the content of a folder.) Run chap2p1.exe by typing its file name and press Enter. Compare the result with that you run the program inside Visual Studio. State the difference between them. Add the following lines of code before "return 0;" of a.cpp. std::cout<<"Press Enter to continue!"<<std::endl; std::cin.get(); Rebuild the solution. Repeat steps 1 to 4 and try to guess the use of "std::cin.get();".


Download ppt "Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG"

Similar presentations


Ads by Google