C/C++ 编程代写
当前位置:以往案例 > >C++ Lecture Lab1 案例编程程序
2017-10-07



Programming Environment

I We use Visual Studio on Windows as our primary programming environment:

I Most students are familiar with Windows O/S

I Windows can be installed easily on MacBook

I Visual Studio is user friendly and feature rich programming environment

I Free access

I We will also use gcc on Linux as a secondary platform:

I some employers value programming experience on Linux

I another option for those who do not want to use Windows

I You can use one or the other, or both.

I I will use Windows and Visual Studio most of the time.

I I will show how to use gcc and Linux.

I Visual Studio is installed on lab computers. Report issues to Minsik Yu ([email protected])


Obtaining Software

I Installing Visual Studio (on Windows):

I Instructions are posted on course site

I Mac users:

I may download and install Windows as a second O/S using bootcamp (https://support.apple.com/en-us/HT201468)

I use VMWare (instructions posted on course site)

I Linux access:

I Request a CS account at: https: //account-request.cs.uchicago.edu/account/requests

I Need help? Contact the TAs.


Getting Started with Visual Studio Integrated Development


Environment (IDE)


Getting Started

I Goal is to learn how how to use Visual Studio IDE, to:

1. create a project (application).

2. use the editor to write code.

3. build the application.

4. run the application.


Getting Started: Hello World Program

I We will create a very simple project to illustrate how to use Visual Studio IDE.

I The program shown below writes a message, "Hello, World!", to console (screen):

#include

int main()

{

std::cout << "Hello, World!" << std::endl;

return 0;

}


Our First C++ Project

I Let's code, build and run this program in Visual Studio.

I First, we will learn the steps we need to follow to create a project.

I After that, we will explain this program step by step and learn some C++.


Step by Step

I Step by step instructions used to create an application are illustrated next.

I Start Visual Studio. Select new project.


I Select "Visual C++" -> "Windows Desktop"


I Select "Windows Desktop Wizard"


Enter:

1. (project) name

2. directory location

3. solution name for the project.


I Change Additional Options as follows:


I uncheck Precompiled Header box.

I check Empty Project box.


I We have a new project!


I Now we can to add source  les to our project.

I Go to Source Files folder on the Solutions Explorer.

I Right click, and select, Add -> New Item


I Select C++ File

I Name the  le appropriately.

I Now you have a blank cpp  le.


I Type code.

I Build the project.


I Make sure the project builds successfully.


I Run it.


I You'll see the greeting message on the screen.


Exercise

1. Use the steps shown above to create an application/project.

2. Code Hello World example. NOTE: C++ is case sensitive

3. Build the application.

4. Run it.

5. Change the program to print a di erent message on the screen.


Hello, World Program: Line by Line

int main()

I main is a special function.

I It is the program entry point.

I Every console application we create should have a main function.

I This function returns an integer (int) value.

I We do not pass any arguments to this function.

I Note: We can have two di erent function signatures for main function:

1. int main()

2. int main(int argc, char* argv[]). We will discuss the use of the second function signature later.


I The line below shows the body of our main function.

{

std::cout << "Hello, World!" << std::endl;

return 0;

}

I A function body starts with { and ends with }.

I Body has 2 statements; each statement ends with a semicolon (";").

I First line writes a message, "Hello, World!", to console.

I Here we use some functionality from the C++ Standard Library.

I After that, the function returns 0 (an integer value)1.

1We don't have to return a value in main function. Return 0 (zero) is implied.


The C++ Standard Library: A First Look


The C++ Standard Library

I Here, we brie y look at the feature (i.e. to write a message to console) used in the Hello World example.

I The C++ Standard Library implements some very useful features we need.

I The Standard Library is divided into several sections (more precisely headers), based on functionality.

I Each header has a name.

I We need to include the appropriate header in order to have access to its components.

I The functionality to handle output to console is de ned in the iostream header.

I To include it: #include


The Standard Input and Output

I Terminology:

I input: a sequence of byte ow coming into the computer memory from a device (e.g. keyboard, le).

I output: a sequence of byte ow from the memory to an output device (e.g. console, le).

I stream: a sequence of characters (bytes) read from an input device or written to an output device.

I The iostream section of de nes objects to read and write a stream to Standard I/O channels:

I cin

I cout

I and more..


Standard Output

cout (pronounced as see-out):

I known as the standard output

I attached to the standard output device, which usually is the console

I uses the stream insertion operator, << cout << "Hello, World";


Standard Input

cin (we pronounce it as see-in):

I known as the standard input

I attached to the standard input device, which usually is the keyboard

I uses the stream extraction operator ( >>), to read a stream from the standard input

int x; cin >> x;


Namespace

I Namespaces allow us to group classes and functions using a name.

I Functions and classes in the C++ Standard Library are de ned in the std namespace.

I To use them (classes/functions), we have to use the fully quali ed name.

I Example: We saw the use of std::cout.

I std is the namespace.

I :: is the scope resolution operator in C++.

I cout is the object in this case.


I We will see the use of namespaces later, when we discuss classes.


Using Namespaces

I Having to write the namespace every time we use a function is a bit cumbersome.

I There are di erent ways to overcome this problem.

I Technique 1:

using std::cout;

using std::endl;

int main()

{

cout << "Hello, World!" << endl;

}

I Technique 2:

using namespace std;

int main()

{

cout << "Hello, World!" << endl;

}


I If we're not careful, the second technique can lead to subtle problems in large programs.

I I encourage you to use the  rst technique.

I I may not show every namespace in subsequent slides after they are introduced.

I E.g. I may show cout without showing using std::cout; but you should assume it is there.



Data Types

I We write programs to handle/manipulate data.

I We use variables to store data.

I Programs need to store di erent kinds/types of data:

I a letter or sequence of letters: e.g. your name

I numbers: e.g. your age

I In C++ every variable has to have a type.

I The type of the variable has to be declared before it is used.

I Once a type is assigned to a variable, it cannot be changed.

I Data types in C++ belong to two main categories:

1. fundamental data types

2. user de ned data types


I Fundamental data types are de ned in the C++ language.

I C++ also allows the user to de ne types (classes).


Fundamental Data Types: Numerical Types

I Numerical values can be whole or real numbers: e.g. 1 or 1.2

I For real numbers we have two types (in C++):

I oat : uses 4 bytes

I double : uses 8 bytes

I As a result, max and min values each can store are di erent.

Type

Size (Bytes)

Value Range

oat

4

3.4


在线提交订单