31
Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering and Computer Science

Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Microsoft Visual Studio Tutorial

Colorado School of Mines Department of Electrical Engineering and Computer Science

Page 2: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

Start Microsoft Visual Studio.

This will bring up the main window as shown

below.

Select the menu item File -> New -> Project.

Under the “Templates” menu on the left,

select “Visual C++” and “Win32” as shown

below. Also select “Win32 Console

Application Visual C++”, which is the simplest

type of application.

Page 3: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

At the bottom, enter a location

that you can save the files to,

such as your home directory on

“adit”. Enter a name for the

project such as “myprog1” (or

you can leave it with the default

name).

Page 4: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

After clicking “Ok”, it will bring up the

window below:

Click on “Next” to go to the next screen:

Page 5: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

Select the buttons for “Console application”

and “Empty project”, and click on Finish.

Page 6: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

Back on the main window, go

to the right box where your

project files are displayed, and

right click on the folder called

“Source Files”. Choose Add ->

New Item … to get to a pop-up

window as shown below.

Select type “C++ File (.cpp)”

and type a name for the file,

such as “Source”. Then click

on Add.

Page 7: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create Your First Program in Microsoft Visual C++

This will bring up the editor, where you

can type in the code for the program.

Type in the code as shown below:

Then choose Debug -> Start

Debugging. This will compile and run

the program.

Page 8: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

C++ Programming Tutorial

• Control Structures• if structure – single-selection

• if/else structure – double-selection• switch – multiple selection structure with breaks

• while structure – repetition structure• do/while – repetition structure

• for structure – counter-controlled repetition structure

Page 9: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

if structure – single selectionProgram: Output:

Page 10: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

If/else structure – double selectionProgram: Output:

Page 11: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

switch structure – multiple selection

Program:

Output:

Page 12: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

while structure – repetition structure

Program:

Output:

Page 13: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

do/while structure – repetition structure

Program: Output:

Page 14: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

for structure – counter-controlled repetition structure

Program:

Output:

Page 15: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

C++ Programming Tutorial

• Functions• Library Functions

• User-defined Functions

• Header Files

• Recursive Function

Page 16: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Library functions

IO Library -- #include <iostream>Math Library -- #include <cmath>String Library -- #include <string>Standard Library -- #include <cstdlib>File stream Library -- #include <fstream>

#include <string>std::memcpystd::strcpystd::strcatstd::strlen

#include <cmath>std::cosstd::expstd::pow

#include <iostream>std::coutstd::cinstd::endl

#include <fstream>std::ofstreamstd::ifstream

#include <cstdlib>std::randstd::srandstd::itoa

Page 17: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Library functionsExample Program 1:

Output:

Page 18: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Library functionsExample Program 2:

Output:

Page 19: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

User-defined functions Printmessage.cpp

Source.cpp

Output:

Page 20: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Header filesSource.cpp

printmessage.h

• Put function prototype and variable

declaration in header file

• Put function details in cpp file

• Include the header file

Page 21: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Recursive functionsSource.cpp:

Output:

factorial.cpp:

factorial.h:

Page 22: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

C++ Programming Tutorial

• Arrays, Pointers and Vectors• Arrays

• Pointers

• vectors

Page 23: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Arrays

Program: Output:

Page 24: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

PointersProgram:

Output:

Page 25: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

VectorsProgram: Output:

Page 26: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

C++ Programming Tutorial

• Classes• Define a class

• Create a object/instance of the class

• Class in vector

• Class in header

Page 27: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Define a classClass definition:

Class functions:

Page 28: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Create a object/instance of the class

Main function: Output:

Page 29: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Class in a vectorMain function:

Output:

Page 30: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Class in headerCar.h: Car.cpp:

• Put class definition in header file

• Put class functions in cpp file

• Include the header file “car.h” in the main cpp file

Page 31: Microsoft Visual Studio Tutorialcs-courses.mines.edu/csci507/schedule/13/VS_C++.pdf · Microsoft Visual Studio Tutorial Colorado School of Mines Department of Electrical Engineering

Class in headerSource.cpp Output: