1 eng236: eng236: c++ programming environment (2) rocky k. c. chang the hong kong polytechnic...
TRANSCRIPT
1
ENG236: ENG236: C++ Programming Environment (2)
Rocky K. C. ChangRocky K. C. Chang
THE HONG KONG
POLYTECHNIC UNIVERSITY
2
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 codes.
• For PC, one of the most popular programming environments is Microsoft Visual Studio.
• It is more popular because of its direct support to the Microsoft Windows environment.
Computer Programming and Basic Software Engineering2. A Taste of C++
3
Computer Programming and Basic Software Engineering2. A Taste of C++
• Visual C++ is one of the components of the umbrella product – Visual Studio, which also contains development tools for other programming languages, e.g. Visual Basic, Visual J++, etc.
• Visual Studio has been for sale for more than 10 years.
• The latest version is Visual Studio .NET. The C++ compiler that is incorporated is Visual C++ .NET.
• Additional features include
• Produce managed codes that run in the .NET runtime
• Allow the use of .NET class library.
Visual Studio .NET
Second semester
4
Visual Studio – Solution and Project
• 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• files containing the C++ programs• 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.
Computer Programming and Basic Software Engineering2. A Taste of C++
They are all in files
5
Computer Programming and Basic Software Engineering2. A Taste of C++
Solution
Project 1
Project 2 Project 3
Files
• A Solution must contain at least one Project
• A Project must contain at least one File
6
Source ProgramEditing
Display compilation results
Display resources
of the solution, such as, projects and files
inside
Computer Programming and Basic Software Engineering2. A Taste of C++
7
Visual Studio - Project Example
• Solution Explorer allows us to clearly see the different files included in the project
Computer Programming and Basic Software Engineering2. A Taste of C++
8
Visual Studio - Project Example
Program can be typed here
Computer Programming and Basic Software Engineering2. A Taste of C++
Built result (result of compiling and linking)
shown here
9
Build your first project - HelloWorld
• Building a project means to produce target executable files (.exe files) based on the program codes.
• Project can be built to execute in console or windows mode• Console mode: Use the Command Prompt for
in/output • Windows mode: Use the Windows environment for
program execution.
Computer Programming and Basic Software Engineering2. A Taste of C++
Second semester
10
Computer Programming and Basic Software Engineering2. A Taste of C++
Command Prompt• It is a traditional interface between the user
and computer• User types in commands to instruct computer
to work.
11
Build your first project - HelloWorld
#include <iostream>
int main(){
std::cout << "Hello World!" << std::endl;return 0;
}
• Hello.cpp : File containing a C++ program that prints a message “Hello World!” on the console output.
Computer Programming and Basic Software Engineering2. A Taste of C++
The returned type of main() must be declared in Visual Studio .NET 2005
12
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 1:Start the Visual Studio .NET 2005
Click New Project
13
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 2
Note where you create this project
You can modify the location of the files of your project.
14
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 3
15
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 4:
Right-click Source Files, select Add → New Item
16
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 5:Choose C++ File (.cpp).
Give a name to the C++ file you are going to add
17
Computer Programming and Basic Software Engineering2. A Taste of C++
Step 6:
Type the program on p.11 here.
Click Build → Build Solution
See the built result
18
Computer Programming and Basic Software Engineering2. A Taste of C++
Do you see the following result?
Note: The result is shown in the Command Prompt – Console Application
Step 7:Execute the program by clicking Debug → Start Without Debugging
19
Computer Programming and Basic Software Engineering2. A Taste of C++
What has actually done?
• In the HelloWorld example, one solution is created that contains a project called chap2p1.
• Inside the project, there is one C++ program called Hello.cpp.
Solution
Project: chap2p1
C++ File: Hello.cpp
20
Computer Programming and Basic Software Engineering2. A Taste of C++
• The C++ Compiler of Visual Studio compiles the program Hello.cpp and generates an executable file 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:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\chap2p1
Run Windows Explorer
21
2. A Taste of C++
Computer Programming and Basic Software Engineering
Contains all files created after building the solution
Contains info. about the project. Double-click this will automatically start the project
The C++ file you created
22
Computer Programming and Basic Software Engineering2. A Taste of C++
The EXE (executable) file created
23
Computer Programming and Basic Software Engineering2. A Taste of C++
We can always run the program under the command prompt
We can always run the program under the command prompt
24
Exercise 2.1Consider the two programs on the RHS. For each of the program, construct a new project in Visual Studio and then build the solution.
For each program, note the error messages when building the project. Fix the errors and execute the resulting project. What do you see?
#include <iostream>int main() cout << "Hi World!" << std::endl; return 0;}
#include <iostream>int test(){ std::cout << "How are you!"
<< std::endl; return 0;}
Computer Programming and Basic Software Engineering2. A Taste of C++
25
Exercise 2.21. Open the Command Prompt.
2. 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.)
3. Run chap2p1.exe by typing its file name and press Enter.
4. Compare the result with that you run the program in Visual Studio. Can you see any difference coming up?
5. If no, add the following lines of code before “return 0;” of Hello.cpp. std::cout<<"Press Enter to continue."<<std::endl;
std::cin.get(); Rebuild the solution. Repeat 1 to 4 and note any difference.
Computer Programming and Basic Software Engineering2. A Taste of C++
26
Acknowledgment
The slides are based on the set developed by Dr. Frank Leung (EIE).