Visual Studio_ C++

download Visual Studio_ C++

of 10

Transcript of Visual Studio_ C++

  • 7/28/2019 Visual Studio_ C++

    1/101

    Visual C++ 2012 Tutorial

    For Introduction to Programming with C++

    By Y. Daniel Liang

    1 Introduction

    Vi sual C++ i s a component of Mi cr osof t Vi sual St udi o 2012 f ordevel opi ng C++ progr ams. A f r ee versi on named Visual StudioExpress can be downl oaded f r om Suppl ement I I . A. Thi s sect i oni nt r oduces how t o creat e a pr oj ect , cr eat e a pr ogr am, compi l eand r un t he pr ogr am i n Vi sual C++ Expr ess 2012 Edi t i on.

    2 Getting Started with Visual C++Vi sual C++ i s easy to i nst al l . I f you need hel p wi t hi nst al l at i on, pl ease r ef er t o Suppl ement I I . A on t he Compani on

    Websi t e.

    Suppose you have i nst al l ed Vi sual C++ 2012 Expr ess Edi t i on. Youcan l aunch Vi sual C++ f r om t he Wi ndows St ar t but t on by choosi ngVS Express for Desktop. The Vi sual C++ 2012 Expr ess useri nt er f ace appear s, as shown i n Fi gur e 1.

    Figure 1

    The Visual C++ user interface is a single window that

    performs editing, compiling, debugging, and running

    programs.

  • 7/28/2019 Visual Studio_ C++

    2/102

    3 Creating a Project

    To cr eat e C++ pr ograms i n Vi sual C++, you have t o f i r st cr eat e apr oj ect . A pr oj ect i s l i ke a hol der t hat t i es al l t he f i l est oget her . Her e ar e t he st eps t o cr eat e a pr oj ect :

    1.Choose File, New, Project t o di spl ay t he New Pr oj ectwi ndow, as shown i n Fi gur e 2.

    2. Choose C++ under t he Templ at e and sel ect Win32 ConsoleApplication i n t he mi ddl e col umn. Type bookexampl e i n t heName f i el d and c: \ smi t h i n t he Locat i on f i el d. Cl i ck OKt odi spl ay t he Wi n32 Appl i cat i on Wi zard wi ndow, as shown i nFi gur e 3.

    3. Cl i ck Next t o di spl ay t he appl i cat i on set t i ngs wi ndow, asshown i n Fi gur e 4.

    4. Sel ect Console application i n t he Appl i cat i on t ype sect i onand check Empty project i n t he Addi t i onal opt i ons sect i on.Cl i ck Finish t o cr eat e a pr oj ect . You wi l l see t he pr oj ect

    named bookexampl e i n the Sol ut i on Expl orer , as shown i nFi gur e 5.

    Figure 2

    You need to create a project before creating programs.

  • 7/28/2019 Visual Studio_ C++

    3/103

    Figure 3

    Win32 Application Wizard creates a project for Win32

    applications.

    Figure 4

    Win32 Application Settings window lets you set the application

    type.

    ChooseEmpty

  • 7/28/2019 Visual Studio_ C++

    4/104

    Figure 5

    A project is created for C++ console applications.

    4 Creating a C++ Program

    Af t er you cr eat e a pr oj ect , you can cr eat e pr ogr ams i n i t . Her ear e t he st eps t o cr eat e a C++ pr ogr am f or Li st i ng 1. 1:

    1. Ri ght - cl i ck t he bookexampl e pr oj ect i n t he Sol ut i onExpl orer t o di spl ay a cont ext menu. Choose Add, Add NewItem f r om t he cont ext menu of t he bookexampl e pr oj ect ( seeFi gur e 6) t o di spl ay t he Add New I t em wi ndow, as shown i nFi gur e 7.

    2. Choose Code under Vi sual C++ on the l ef t col umn and C++Fi l e ( . cpp) i n t he mi ddl e col umn. Ent er Wel come i n t he Namef i el d and c: \ smi t h\ bookexampl e\ bookexampl e i n t he Locat i onf i el d. Cl i ck Addt o creat e t he f i l e, as shown i n Fi gur e 8.

    3. Ent er t he code f or Wel come. cpp exact l y f r om Li st i ng 1. 1, asshown i n Fi gur e 9.

    SolutExploshows

  • 7/28/2019 Visual Studio_ C++

    5/105

    Figure 6

    You can open the Add New Item window from the projects

    context menu.

    Figure 7

    You can specify the file type, name, and location to create a

    file.

  • 7/28/2019 Visual Studio_ C++

    6/106

    Figure 8

    Welcome.cpp is created in the project.

    Figure 9

    The source code for Welcome.cpp is entered.

    5 Compiling a C++ Program

  • 7/28/2019 Visual Studio_ C++

    7/107

    Af t er you creat e a pr ogr am, you can compi l e i t . You may do so bychoosi ng Build, Compile, or pr ess Ctrl+F7, or choose Compile i nt he cont ext menu f or Wel come. cpp, as shown i n Fi gur e 10.

    Figure 10

    Choose the Compile command to compile the program.

    6 Running a C++ Program

    To r un t he program, press Ctrl+F5. You wi l l see t he out putdi spl ayed i n a DOS wi ndow, as shown i n Fi gur e 11.

    Figure 11

    The output is displayed in a DOS window.

    NOTE:

    The Run command i nvokes t he Compile command i f t hepr ogr am i s not compi l ed or was modi f i ed af t er t hel ast compi l at i on.

    NOTE:

  • 7/28/2019 Visual Studio_ C++

    8/108

    Each pr oj ect can have onl y one f i l e t hat cont ai ns amai n f unct i on. I f you need t o cr eat e anot her f i l ewi t h a mai n f unct i on, you have two opt i ons:

    Remove t he cur r ent f i l e t hat cont ai ns a mai nf unct i on f r om t he pr oj ect by choosi ng Removef r om t he cont ext menu of t he pr ogr am, as showni n Fi gur e 12. ( Note t hat you can add an

    exi st i ng f i l e t o t he pr oj ect by choosi ng File,Add Existing Item. )

    Cr eat e a new proj ect f or t he new progr am.

    Figure 12You can remove a file from a project.

    ***END NOTE

    7 Including Header File from Different Directories

    I n Chapt er 9, you wi l l l ear n header f i l es. You need t o add t heheader f i l es i n t he Header Fi l es node under t he pr oj ect node.

    I f a header f i l e i s i n a di r ect or y t hat i s di f f er ent f r om t hepr ogr am di r ect or y, you need t o add t he di r ect or y i n t he pr oj ectpr oper t i es. For exampl e, suppose Test . cpp needs t o i ncl udeCi r cl e. h and Ci r cl e. h i s i n c: \ t eacher . Fol l ow t he st eps bel owt o add c: \ t eacher i n t he pr oj ect pr oper t y di al og box:

    1. Ri ght - cl i ck on Test . cpp i n t he Sol ut i on expl or er t odi spl ay a cont ext di al og box, as shown i n Fi gur e 13.

  • 7/28/2019 Visual Studio_ C++

    9/109

    2. Choose Properties t o di spl ay Pr oj ect Proper t i es di al ogbox, as shown i n Fi gur e 14.

    3. Choose General under C/ C++ on t he l ef t col umn. Ent erc: \ t eacher i n t he Additional Include Directories f i el d.

    Figure 13

    You can customize project properties in VC++.

  • 7/28/2019 Visual Studio_ C++

    10/10

    10

    Figure 14

    C:\teacher is added in the path.