Microsoft Visual C Tutorial Pdf
The usual starting point for a C++ programmer is a 'Hello, world!' application that runs on the command line. That's what you'll create in Visual Studio in this article, and then we'll move on to something more challenging: a calculator app.
Inside and outside of Visual Studio. This note will give examples of final forms and give a more clear view of what is possible with Visual Studio. Operation: Microsoft Visual Studio has an advanced variety of tools to create a high level performance GUI’s. This integrated development environment (IDE) is compatible with many different. Learning to Program with Visual Basic and.NET Gadgeteer 7 CHAPTER 1. INTRODUCTION Microsoft.NET Gadgeteer is a really easy-to-use platform for creating new electronic devices using a wide variety of hardware modules and a powerful programming environment. Students with little or no electronics. Programming Microsoft Visual C PDF. Is NOT an introductory text, NOR an in-depth reference. It is a comprehensive tutorial that is. Art of Creating Interactive Web Pages with Visual Basic Script 2 and ActiveX Programming Microsoft Visual C Inside Visual C, Version 1.5 (Microsoft. Contents at a Glance Introductionxxi PART I GETTING STARTED WITH C.NET ChapTer 1 hello C! 3 ChapTer 2 Introducing object-oriented programming 13 ChapTer 3 Variables and operators 23 ChapTer 4 Using functions 37 ChapTer 5 Decision and loop statements 57 ChapTer 6 More about classes and objects 77 ChapTer 7 Controlling object lifetimes 103 ChapTer 8 Inheritance 121.
Prerequisites
- Have Visual Studio with the Desktop development with C++ workload installed and running on your computer. If it's not installed yet, see Install C++ support in Visual Studio.
Create your app project
Visual Studio uses projects to organize the code for an app, and solutions to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It also manages the relationship between all the project's files and any external files. To create your app, first, you'll create a new project and solution.
If you've just started Visual Studio, you'll see the Visual Studio 2019 dialog box. Choose Create a new project to get started.
Otherwise, on the menubar in Visual Studio, choose File > New > Project. The Create a new project window opens.
In the list of project templates, choose Console App, then choose Next.
Important
Make sure you choose the C++ version of the Console App template. It has the C++, Windows, and Console tags, and the icon has '++' in the corner.
In the Configure your new project dialog box, select the Project name edit box, name your new project CalculatorTutorial, then choose Create.
An empty C++ Windows console application gets created. Console applications use a Windows console window to display output and accept user input. In Visual Studio, an editor window opens and shows the generated code:
Verify that your new app builds and runs
The template for a new Windows console application creates a simple C++ 'Hello World' app. At this point, you can see how Visual Studio builds and runs the apps you create right from the IDE.
To build your project, choose Build Solution from the Build menu. The Output window shows the results of the build process.
To run the code, on the menu bar, choose Debug, Start without debugging.
A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints 'Press any key to close this window . . .' to give you a chance to see the output. Congratulations! You've created your first 'Hello, world!' console app in Visual Studio!
Press a key to dismiss the console window and return to Visual Studio.
You now have the tools to build and run your app after every change, to verify that the code still works as you expect. Later, we'll show you how to debug it if it doesn't.
Edit the code
Now let's turn the code in this template into a calculator app.
In the CalculatorTutorial.cpp file, edit the code to match this example:
Understanding the code:
- The
#include
statements allow you to reference code located in other files. Sometimes, you may see a filename surrounded by angle brackets (<>); other times, it's surrounded by quotes (' '). In general, angle brackets are used when referencing the C++ Standard Library, while quotes are used for other files. - The
using namespace std;
line tells the compiler to expect stuff from the C++ Standard Library to be used in this file. Without this line, each keyword from the library would have to be preceded with astd::
, to denote its scope. For instance, without that line, each reference tocout
would have to be written asstd::cout
. Theusing
statement is added to make the code look more clean. - The
cout
keyword is used to print to standard output in C++. The << operator tells the compiler to send whatever is to the right of it to the standard output. - The endl keyword is like the Enter key; it ends the line and moves the cursor to the next line. It is a better practice to put a
n
inside the string (contained by ') to do the same thing, asendl
always flushes the buffer and can hurt the performance of the program, but since this is a very small app,endl
is used instead for better readability. - All C++ statements must end with semicolons and all C++ applications must contain a
main()
function. This function is what the program runs at the start. All code must be accessible frommain()
in order to be used.
- The
To save the file, enter Ctrl+S, or choose the Save icon near the top of the IDE, the floppy disk icon in the toolbar under the menu bar.
To run the application, press Ctrl+F5 or go to the Debug menu and choose Start Without Debugging. You should see a console window appear that displays the text specified in the code.
Close the console window when you're done.
Add code to do some math
It's time to add some math logic.
To add a Calculator class
Go to the Project menu and choose Add Class. In the Class Name edit box, enter Calculator. Choose OK. Two new files get added to your project. To save all your changed files at once, press Ctrl+Shift+S. It's a keyboard shortcut for File > Save All. There's also a toolbar button for Save All, an icon of two floppy disks, found beside the Save button. In general, it's good practice to do Save All frequently, so you don't miss any files when you save.
A class is like a blueprint for an object that does something. In this case, we define a calculator and how it should work. The Add Class wizard you used above created .h and .cpp files that have the same name as the class. You can see a full list of your project files in the Solution Explorer window, visible on the side of the IDE. If the window isn't visible, you can open it from the menu bar: choose View > Solution Explorer.
You should now have three tabs open in the editor: CalculatorTutorial.cpp, Calculator.h, and Calculator.cpp. If you accidentally close one of them, you can reopen it by double-clicking it in the Solution Explorer window.
In Calculator.h, remove the
Calculator();
and~Calculator();
lines that were generated, since you won't need them here. Next, add the following line of code so the file now looks like this:Understanding the code
- The line you added declares a new function called
Calculate
, which we'll use to run math operations for addition, subtraction, multiplication, and division. - C++ code is organized into header (.h) files and source (.cpp) files. Several other file extensions are supported by various compilers, but these are the main ones to know about. Functions and variables are normally declared, that is, given a name and a type, in header files, and implemented, or given a definition, in source files. To access code defined in another file, you can use
#include 'filename.h'
, where 'filename.h' is the name of the file that declares the variables or functions you want to use. - The two lines you deleted declared a constructor and destructor for the class. For a simple class like this one, the compiler creates them for you, and their uses are beyond the scope of this tutorial.
- It's good practice to organize your code into different files based on what it does, so it's easy to find the code you need later. In our case, we define the
Calculator
class separately from the file containing themain()
function, but we plan to reference theCalculator
class inmain()
.
- The line you added declares a new function called
You'll see a green squiggle appear under
Calculate
. It's because we haven't defined theCalculate
function in the .cpp file. Hover over the word, click the lightbulb (in this case, a screwdriver) that pops up, and choose Create definition of 'Calculate' in Calculator.cpp.A pop-up appears that gives you a peek of the code change that was made in the other file. The code was added to Calculator.cpp.
Currently, it just returns 0.0. Let's change that. Press Esc to close the pop-up.
Switch to the Calculator.cpp Antivirus software for windows 10. file in the editor window. Remove the
Calculator()
and~Calculator()
sections (as you did in the .h file) and add the following code toCalculate()
:Understanding the code
- The function
Calculate
consumes a number, an operator, and a second number, then performs the requested operation on the numbers. - The switch statement checks which operator was provided, and only executes the case corresponding to that operation. The default: case is a fallback in case the user types an operator that isn't accepted, so the program doesn't break. In general, it's best to handle invalid user input in a more elegant way, but this is beyond the scope of this tutorial.
- The
double
keyword denotes a type of number that supports decimals. This way, the calculator can handle both decimal math and integer math. TheCalculate
function is required to always return such a number due to thedouble
at the very start of the code (this denotes the function's return type), which is why we return 0.0 even in the default case. - The .h file declares the function prototype, which tells the compiler upfront what parameters it requires, and what return type to expect from it. The .cpp file has all the implementation details of the function.
- The function
If you build and run the code again at this point, it will still exit after asking which operation to perform. Next, you'll modify the main
function to do some calculations.
To call the Calculator class member functions
Now let's update the
main
function in CalculatorTutorial.cpp:Understanding the code
- Since C++ programs always start at the
main()
function, we need to call our other code from there, so a#include
statement is needed. - Some initial variables
x
,y
,oper
, andresult
are declared to store the first number, second number, operator, and final result, respectively. It is always good practice to give them some initial values to avoid undefined behavior, which is what is done here. - The
Calculator c;
line declares an object named 'c' as an instance of theCalculator
class. The class itself is just a blueprint for how calculators work; the object is the specific calculator that does the math. - The
while (true)
statement is a loop. The code inside the loop continues to execute over and over again as long as the condition inside the()
holds true. Since the condition is simply listed astrue
, it's always true, so the loop runs forever. To close the program, the user must manually close the console window. Otherwise, the program always waits for new input. - The
cin
keyword is used to accept input from the user. This input stream is smart enough to process a line of text entered in the console window and place it inside each of the variables listed, in order, assuming the user input matches the required specification. You can modify this line to accept different types of input, for instance, more than two numbers, though theCalculate()
function would also need to be updated to handle this. - The
c.Calculate(x, oper, y);
expression calls theCalculate
function defined earlier, and supplies the entered input values. The function then returns a number that gets stored inresult
. - Finally,
result
is printed to the console, so the user sees the result of the calculation.
- Since C++ programs always start at the
Build and test the code again
Now it's time to test the program again to make sure everything works properly.
Press Ctrl+F5 to rebuild and start the app.
Enter
5 + 5
, and press Enter. Verify that the result is 10.
Debug the app
Since the user is free to type anything into the console window, let's make sure the calculator handles some input as expected. Instead of running the program, let's debug it instead, so we can inspect what it's doing in detail, step-by-step.
To run the app in the debugger
Set a breakpoint on the
result = c.Calculate(x, oper, y);
line, just after the user was asked for input. To set the breakpoint, click next to the line in the gray vertical bar along the left edge of the editor window. A red dot appears.Now when we debug the program, it always pauses execution at that line. We already have a rough idea that the program works for simple cases. Since we don't want to pause execution every time, let's make the breakpoint conditional.
Right-click the red dot that represents the breakpoint, and choose Conditions. In the edit box for the condition, enter
(y 0) && (oper '/')
. Choose the Close button when you're done. The condition is saved automatically.Now we pause execution at the breakpoint specifically if a division by 0 is attempted.
To debug the program, press F5, or choose the Local Windows Debugger toolbar button that has the green arrow icon. In your console app, if you enter something like '5 - 0', the program behaves normally and keeps running. However, if you type '10 / 0', it pauses at the breakpoint. You can even put any number of spaces between the operator and numbers:
cin
is smart enough to parse the input appropriately.
Useful windows in the debugger
Whenever you debug your code, you may notice that some new windows appear. These windows can assist your debugging experience. Take a look at the Autos window. The Autos window shows you the current values of variables used at least three lines before and up to the current line. To see all of the variables from that function, switch to the Locals window. You can actually modify the values of these variables while debugging, to see what effect they would have on the program. In this case, we'll leave them alone.
You can also just hover over variables in the code itself to see their current values where the execution is currently paused. Make sure the editor window is in focus by clicking on it first.
To continue debugging
The yellow line on the left shows the current point of execution. The current line calls
Calculate
, so press F11 to Step Into the function. You'll find yourself in the body of theCalculate
function. Be careful with Step Into; if you do it too much, you may waste a lot of time. It goes into any code you use on the line you are on, including standard library functions.Now that the point of execution is at the start of the
Calculate
function, press F10 to move to the next line in the program's execution. F10 is also known as Step Over. You can use Step Over to move from line to line, without delving into the details of what is occurring in each part of the line. In general you should use Step Over instead of Step Into, unless you want to dive more deeply into code that is being called from elsewhere (as you did to reach the body ofCalculate
).Continue using F10 to Step Over each line until you get back to the
main()
function in the other file, and stop on thecout
line.It looks like the program is doing what is expected: it takes the first number, and divides it by the second. On the
cout
line, hover over theresult
variable or take a look atresult
in the Autos window. You'll see its value is listed as 'inf', which doesn't look right, so let's fix it. Thecout
line just outputs whatever value is stored inresult
, so when you step one more line forward using F10, the console window displays:This result happens because division by zero is undefined, so the program doesn't have a numerical answer to the requested operation.
To fix the 'divide by zero' error
Let's handle division by zero more gracefully, so a user can understand the problem.
Make the following changes in CalculatorTutorial.cpp. (You can leave the program running as you edit, thanks to a debugger feature called Edit and Continue):
Now press F5 once. Program execution continues all the way until it has to pause to ask for user input. Enter
10 / 0
again. Now, a more helpful message is printed. The user is asked for more input, and the program continues executing normally.Note
When you edit code while in debugging mode, there is a risk of code becoming stale. This happens when the debugger is still running your old code, and has not yet updated it with your changes. The debugger pops up a dialog to inform you when this happens. Sometimes, you may need to press F5 to refresh the code being executed. In particular, if you make a change inside a function while the point of execution is inside that function, you'll need to step out of the function, then back into it again to get the updated code. If that doesn't work for some reason and you see an error message, you can stop debugging by clicking on the red square in the toolbar under the menus at the top of the IDE, then start debugging again by entering F5 or by choosing the green 'play' arrow beside the stop button on the toolbar.
Understanding the Run and Debug shortcuts
- F5 (or Debug > Start Debugging) starts a debugging session if one isn't already active, and runs the program until a breakpoint is hit or the program needs user input. If no user input is needed and no breakpoint is available to hit, the program terminates and the console window closes itself when the program finishes running. If you have something like a 'Hello World' program to run, use Ctrl+F5 or set a breakpoint before you enter F5 to keep the window open.
- Ctrl+F5 (or Debug > Start Without Debugging) runs the application without going into debug mode. This is slightly faster than debugging, and the console window stays open after the program finishes executing.
- F10 (known as Step Over) lets you iterate through code, line-by-line, and visualize how the code is run and what variable values are at each step of execution.
- F11 (known as Step Into) works similarly to Step Over, except it steps into any functions called on the line of execution. For example, if the line being executed calls a function, pressing F11 moves the pointer into the body of the function, so you can follow the function's code being run before coming back to the line you started at. Pressing F10 steps over the function call and just moves to the next line; the function call still happens, but the program doesn't pause to show you what it's doing.
Close the app
- If it's still running, close the console window for the calculator app.
The finished app
Congratulations! You've completed the code for the calculator app, and built and debugged it in Visual Studio.
Next steps
The usual starting point for a C++ programmer is a 'Hello, world!' application that runs on the command line. That's what you'll create in Visual Studio in this article, and then we'll move on to something more challenging: a calculator app.
Prerequisites
- Have Visual Studio with the Desktop development with C++ workload installed and running on your computer. If it's not installed yet, see Install C++ support in Visual Studio.
Create your app project
Visual Studio uses projects to organize the code for an app, and solutions to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It also manages the relationship between all the project's files and any external files. To create your app, first, you'll create a new project and solution.
On the menubar in Visual Studio, choose File > New > Project. The New Project window opens.
On the left sidebar, make sure Visual C++ is selected. In the center, choose Windows Console Application.
In the Name edit box at the bottom, name the new project CalculatorTutorial, then choose OK.
An empty C++ Windows console application gets created. Console applications use a Windows console window to display output and accept user input. In Visual Studio, an editor window opens and shows the generated code:
Verify that your new app builds and runs
The template for a new windows console application creates a simple C++ 'Hello World' app. At this point, you can see how Visual Studio builds and runs the apps you create right from the IDE.
To build your project, choose Build Solution from the Build menu. The Output window shows the results of the build process.
To run the code, on the menu bar, choose Debug, Start without debugging.
A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints 'Press any key to continue . . .' to give you a chance to see the output. Congratulations! You've created your first 'Hello, world!' console app in Visual Studio!
Press a key to dismiss the console window and return to Visual Studio.
You now have the tools to build and run your app after every change, to verify that the code still works as you expect. Later, we'll show you how to debug it if it doesn't.
Edit the code
Now let's turn the code in this template into a calculator app.
In the CalculatorTutorial.cpp file, edit the code to match this example:
Understanding the code:
- The
#include
statements allow you to reference code located in other files. Sometimes, you may see a filename surrounded by angle brackets (<>); other times, it's surrounded by quotes (' '). In general, angle brackets are used when referencing the C++ Standard Library, while quotes are used for other files. - The
#include 'pch.h'
(or in Visual Studio 2017 and earlier,#include 'stdafx.h'
) line references something known as a precompiled header. These are often used by professional programmers to improve compilation times, but they are beyond the scope of this tutorial. - The
using namespace std;
line tells the compiler to expect stuff from the C++ Standard Library to be used in this file. Without this line, each keyword from the library would have to be preceded with astd::
, to denote its scope. For instance, without that line, each reference tocout
would have to be written asstd::cout
. Theusing
statement is added to make the code look more clean. - The
cout
keyword is used to print to standard output in C++. The << operator tells the compiler to send whatever is to the right of it to the standard output. - The endl keyword is like the Enter key; it ends the line and moves the cursor to the next line. It is a better practice to put a
n
inside the string (contained by ') to do the same thing, asendl
always flushes the buffer and can hurt the performance of the program, but since this is a very small app,endl
is used instead for better readability. - All C++ statements must end with semicolons and all C++ applications must contain a
main()
function. This function is what the program runs at the start. All code must be accessible frommain()
in order to be used.
- The
To save the file, enter Ctrl+S, or choose the Save icon near the top of the IDE, the floppy disk icon in the toolbar under the menu bar.
To run the application, press Ctrl+F5 or go to the Debug menu and choose Start Without Debugging. If you get a pop-up that says This project is out of date, you may select Do not show this dialog again, and then choose Yes to build your application. You should see a console window appear that displays the text specified in the code.
Close the console window when you're done.
Add code to do some math
It's time to add some math logic.
To add a Calculator class
Go to the Project menu and choose Add Class. In the Class Name edit box, enter Calculator. Choose OK. Two new files get added to your project. To save all your changed files at once, press Ctrl+Shift+S. It's a keyboard shortcut for File > Save All. There's also a toolbar button for Save All, an icon of two floppy disks, found beside the Save button. In general, it's good practice to do Save All frequently, so you don't miss any files when you save.
A class is like a blueprint for an object that does something. In this case, we define a calculator and how it should work. The Add Class wizard you used above created .h and .cpp files that have the same name as the class. You can see a full list of your project files in the Solution Explorer window, visible on the side of the IDE. If the window isn't visible, you can open it from the menu bar: choose View > Solution Explorer.
You should now have three tabs open in the editor: CalculatorTutorial.cpp, Calculator.h, and Calculator.cpp. If you accidentally close one of them, you can reopen it by double-clicking it in the Solution Explorer window.
In Calculator.h, remove the
Calculator();
and~Calculator();
lines that were generated, since you won't need them here. Next, add the following line of code so the file now looks like this:Understanding the code
- The line you added declares a new function called
Calculate
, which we'll use to run math operations for addition, subtraction, multiplication, and division. - C++ code is organized into header (.h) files and source (.cpp) files. Several other file extensions are supported by various compilers, but these are the main ones to know about. Functions and variables are normally declared, that is, given a name and a type, in header files, and implemented, or given a definition, in source files. To access code defined in another file, you can use
#include 'filename.h'
, where 'filename.h' is the name of the file that declares the variables or functions you want to use. - The two lines you deleted declared a constructor and destructor for the class. For a simple class like this one, the compiler creates them for you, and their uses are beyond the scope of this tutorial.
- It's good practice to organize your code into different files based on what it does, so it's easy to find the code you need later. In our case, we define the
Calculator
class separately from the file containing themain()
function, but we plan to reference theCalculator
class inmain()
.
- The line you added declares a new function called
You'll see a green squiggle appear under
Calculate
. It's because we haven't defined theCalculate
function in the .cpp file. Hover over the word, click the lightbulb that pops up, and choose Create definition of 'Calculate' in Calculator.cpp. A pop-up appears that gives you a peek of the code change that was made in the other file. The code was added to Calculator.cpp.Currently, it just returns 0.0. Let's change that. Press Esc to close the pop-up.
Switch to the Calculator.cpp file in the editor window. Remove the
Calculator()
and~Calculator()
sections (as you did in the .h file) and add the following code toCalculate()
:Understanding the code
- The function
Calculate
consumes a number, an operator, and a second number, then performs the requested operation on the numbers. - The switch statement checks which operator was provided, and only executes the case corresponding to that operation. The default: case is a fallback in case the user types an operator that isn't accepted, so the program doesn't break. In general, it's best to handle invalid user input in a more elegant way, but this is beyond the scope of this tutorial.
- The
double
keyword denotes a type of number that supports decimals. This way, the calculator can handle both decimal math and integer math. TheCalculate
function is required to always return such a number due to thedouble
at the very start of the code (this denotes the function's return type), which is why we return 0.0 even in the default case. - The .h file declares the function prototype, which tells the compiler upfront what parameters it requires, and what return type to expect from it. The .cpp file has all the implementation details of the function.
- The function
If you build and run the code again at this point, it will still exit after asking which operation to perform. Next, you'll modify the main
function to do some calculations.
To call the Calculator class member functions
Now let's update the
main
function in CalculatorTutorial.cpp:Understanding the code
- Since C++ programs always start at the
main()
function, we need to call our other code from there, so a#include
statement is needed. - Some initial variables
x
,y
,oper
, andresult
are declared to store the first number, second number, operator, and final result, respectively. It is always good practice to give them some initial values to avoid undefined behavior, which is what is done here. - The
Calculator c;
line declares an object named 'c' as an instance of theCalculator
class. The class itself is just a blueprint for how calculators work; the object is the specific calculator that does the math. - The
while (true)
statement is a loop. The code inside the loop continues to execute over and over again as long as the condition inside the()
holds true. Since the condition is simply listed astrue
, it's always true, so the loop runs forever. To close the program, the user must manually close the console window. Otherwise, the program always waits for new input. - The
cin
keyword is used to accept input from the user. This input stream is smart enough to process a line of text entered in the console window and place it inside each of the variables listed, in order, assuming the user input matches the required specification. You can modify this line to accept different types of input, for instance, more than two numbers, though theCalculate()
function would also need to be updated to handle this. - The
c.Calculate(x, oper, y);
expression calls theCalculate
function defined earlier, and supplies the entered input values. The function then returns a number that gets stored inresult
. - Finally,
result
is printed to the console, so the user sees the result of the calculation.
- Since C++ programs always start at the
Build and test the code again
Now it's time to test the program again to make sure everything works properly.
Press Ctrl+F5 to rebuild and start the app.
Enter
5 + 5
, and press Enter. Verify that the result is 10.
Debug the app
Since the user is free to type anything into the console window, let's make sure the calculator handles some input as expected. Instead of running the program, let's debug it instead, so we can inspect what it's doing in detail, step-by-step.
To run the app in the debugger
Set a breakpoint on the
result = c.Calculate(x, oper, y);
line, just after the user was asked for input. To set the breakpoint, click next to the line in the gray vertical bar along the left edge of the editor window. A red dot appears.Now when we debug the program, it always pauses execution at that line. We already have a rough idea that the program works for simple cases. Since we don't want to pause execution every time, let's make the breakpoint conditional.
Right-click the red dot that represents the breakpoint, and choose Conditions. In the edit box for the condition, enter
(y 0) && (oper '/')
. Choose the Close button when you're done. The condition is saved automatically.Now we pause execution at the breakpoint specifically if a division by 0 is attempted.
To debug the program, press F5, or choose the Local Windows Debugger toolbar button that has the green arrow icon. In your console app, if you enter something like '5 - 0', the program behaves normally and keeps running. However, if you type '10 / 0', it pauses at the breakpoint. You can even put any number of spaces between the operator and numbers;
cin
is smart enough to parse the input appropriately.
Useful windows in the debugger
Whenever you debug your code, you may notice that some new windows appear. These windows can assist your debugging experience. Take a look at the Autos window. The Autos window shows you the current values of variables used at least three lines before and up to the current line.
To see all of the variables from that function, switch to the Locals window. You can actually modify the values of these variables while debugging, to see what effect they would have on the program. In this case, we'll leave them alone.
You can also just hover over variables in the code itself to see their current values where the execution is currently paused. Make sure the editor window is in focus by clicking on it first.
To continue debugging
The yellow line on the left shows the current point of execution. The current line calls
Calculate
, so press F11 to Step Into the function. You'll find yourself in the body of theCalculate
function. Be careful with Step Into; if you do it too much, you may waste a lot of time. It goes into any code you use on the line you are on, including standard library functions.Now that the point of execution is at the start of the
Calculate
function, press F10 to move to the next line in the program's execution. F10 is also known as Step Over. You can use Step Over to move from line to line, without delving into the details of what is occurring in each part of the line. In general you should use Step Over instead of Step Into, unless you want to dive more deeply into code that is being called from elsewhere (as you did to reach the body ofCalculate
).Continue using F10 to Step Over each line until you get back to the
main()
function in the other file, and stop on thecout
line.It looks like the program is doing what is expected: it takes the first number, and divides it by the second. On the
cout
line, hover over theresult
variable or take a look atresult
in the Autos window. You'll see its value is listed as 'inf', which doesn't look right, so let's fix it. Thecout
line just outputs whatever value is stored inresult
, so when you step one more line forward using F10, the console window displays:This result happens because division by zero is undefined, so the program doesn't have a numerical answer to the requested operation.
To fix the 'divide by zero' error
Let's handle division by zero more gracefully, so a user can understand the problem.
Make the following changes in CalculatorTutorial.cpp. (You can leave the program running as you edit, thanks to a debugger feature called Edit and Continue):
Now press F5 once. Program execution continues all the way until it has to pause to ask for user input. Enter
10 / 0
again. Now, a more helpful message is printed. The user is asked for more input, and the program continues executing normally.Note
When you edit code while in debugging mode, there is a risk of code becoming stale. This happens when the debugger is still running your old code, and has not yet updated it with your changes. The debugger pops up a dialog to inform you when this happens. Sometimes, you may need to press F5 to refresh the code being executed. In particular, if you make a change inside a function while the point of execution is inside that function, you'll need to step out of the function, then back into it again to get the updated code. If that doesn't work for some reason and you see an error message, you can stop debugging by clicking on the red square in the toolbar under the menus at the top of the IDE, then start debugging again by entering F5 or by choosing the green 'play' arrow beside the stop button on the toolbar.
Understanding the Run and Debug shortcuts
- F5 (or Debug > Start Debugging) starts a debugging session if one isn't already active, and runs the program until a breakpoint is hit or the program needs user input. If no user input is needed and no breakpoint is available to hit, the program terminates and the console window closes itself when the program finishes running. If you have something like a 'Hello World' program to run, use Ctrl+F5 or set a breakpoint before you enter F5 to keep the window open.
- Ctrl+F5 (or Debug > Start Without Debugging) runs the application without going into debug mode. This is slightly faster than debugging, and the console window stays open after the program finishes executing.
- F10 (known as Step Over) lets you iterate through code, line-by-line, and visualize how the code is run and what variable values are at each step of execution.
- F11 (known as Step Into) works similarly to Step Over, except it steps into any functions called on the line of execution. For example, if the line being executed calls a function, pressing F11 moves the pointer into the body of the function, so you can follow the function's code being run before coming back to the line you started at. Pressing F10 steps over the function call and just moves to the next line; the function call still happens, but the program doesn't pause to show you what it's doing.
Close the app
Microsoft Visual Studio C++ Tutorial Pdf
- If it's still running, close the console window for the calculator app.
The finished app
Congratulations! You've completed the code for the calculator app, and built and debugged it in Visual Studio.
Next steps
Watch: Setting up your IDE
Watch: Tips & Tricks
Watch: Opening a project from Source
Install only the tool and component bundles you need for development in Visual Studio
ASP.NET & web development
Build web applications using ASP.NET Core, ASP.NET (.NET Framework), HTML/JavaScript, and Containers including Docker support
Description
Maximize your productivity developing .NET web applications using ASP.NET Core, standards-based technologies like HTML, and JavaScript.
Development applications
– Website using Razor Pages in ASP.NET Core
– Web API with ASP.NET Core MVC
– Real-time web apps with ASP.NET Core SignalR
Components
– .NET Framework 4.x development tools
– .NET Core 2.1 development tools
– ASP.NET and web development tools
– .NET profiling tools
– Container development tools
– Cloud tools for web development
– IntelliSense, code navigation, and refactoring for C#, Visual Basic, and F#
Azure
Azure SDKs, tools, and projects for developing cloud apps, creating resources, and building Containers including Docker support
Description
Easily build, test, deploy, and manage scalable apps and services on the Microsoft cloud. Install to view resources in Cloud Explorer, create resources using Azure Manage tools, build applications for Azure Web and Cloud Services, and perform big data operations using Azure Datalake tools.
Development applications
– Publish an ASP.NET Core app to Azure in the IDE
– Test performance of a cloud service
– Debug a published Azure cloud service
Components
– Azure development prerequisites
– .NET Framework 4.x development tools
– .NET Core 2.1 development tools
– ASP.NET and web development tools
– Container development tools
Python
Editing, debugging, interactive development, and source control for Python
Description
Support for building Python web applications using familiar frameworks including Django and Flask, and Data Science applications with built-in Conda and IPython support.
Development applications
– Python applications
– Django web apps
– Flask web apps
Components
– Support for CPython, virtualenv, venv, and conda environments
– Rich editing, IntelliSense, and code comprehension
– Interactive (REPL) environment window
– Support for Python in open folder workspaces
– Project and item templates to simplify new project creating process
– Profiling and unit testing tools
– Manage Conda environments using Miniconda
– Live Share
Node.js development
Build fast and scalable applications using Node.js, a server-side JavaScript runtime environment
Description
Easily build Node.js applications using web frameworks like Express and front-end frameworks like React.
Development applications
– Node.js app with Express
– Node.js app with React
– Publish Node.js app to Azure cloud
Components
– Project templates
– IntelliCode
– npm integration
– Interactive (REPL) environment window
– Local and remote advanced debugging
– Profiling tools to track performance
– Unit testing support
– TypeScript integration
.NET desktop development
Build WPF, Windows Forms, and console applications using C#, Visual Basic, and F#
Description
Create Windows-based applications by using Windows Presentation Foundation or by using Windows Forms. Also create web applications using the .NET Framework, and client applications for computers or devices that you make available through the Microsoft Store.
Development applications
– Windows Presentation Foundation (WPF)
– Windows Forms
Components
– .NET desktop development tools
– .NET Framework 4.x development tools
– .NET profiling tools
– C# and Visual Basic language support
– Entity Framework 6 tools
– IntelliTrace
– Just-In-Time debugger
– Live Unit Testing
– Live Share
Desktop development with C++
Build modern C++ apps for Windows using the tools of your choice, including MSVC, Clang, CMake, or MS Build
Description
C++ desktop apps run in a console or in a window. Take advantage of the powerful MSVC compiler and libraries toolset or bring your own build tools to do the job.
Development applications
– Traditional Windows Desktop applications using C++
– Windows console applications using C++
Components
– MSVC x64/x86 build tools
– Visual C++ core desktop features
– C++ profiling tools
– Windows 10 SDK
– C++ CMake tools for Windows
– Test adapter for Google Test
– Test adapter for Boost.Test
– IntelliTrace
– Just-In-Time debugger
– Live Share
All Microsoft Visual C++ Download
Learn more about Desktop development with C++UWP development
Create applications for the Universal Windows Platform with C#, VB, or optionally C++
Description
Build applications that target Windows Desktop, Xbox, HoloLens, SurfaceHub, and even Windows 10 IoT.
Development applications
– Universal Windows Platform (UWP)
Components
– Blend for Visual Studio
– .NET Native and .NET Standard
– NuGet package manager
– Universal Windows Platform tools
– Windows 10 SDK
Mobile development with .NET
Build cross-platform applications for iOS, Android, or Windows using Xamarin
Description
Build fully-native iOS, Android, and Universal Windows Platform apps using C# and XAML. Xamarin gives you full access to platform APIs and features using a single language and allows you to have a shared codebase with other .NET platforms.
Development applications
– Build cross-platform apps using Xamarin.Forms
– Access native features with the Xamarin.Essentials API
Components
– Xamarin
– .NET Framework4.x development tools
– C# and Visual Basic language support
– Android SDK setup
Mobile development with C++
Build cross-platform C++ apps for iOS or Android
Development applications
– Android Native App
– Import an XCode Project
– OpenGL ES application on Android and iOS
Components
– Visual Studio C++ core features
– Android SDK setup
– Android NDK
– Apache Ant
– C++ Android development tools
Game development with Unity
Create 2D and 3D games with Unity, a powerful cross-platform development environment
Description
Create games and interactive content and publish to 21 platforms, including all mobile platforms, WebGL, Mac, PC and Linux desktop, web or consoles.
Development applications
Develop Unity games with a premium debugging experience
Components
– Visual Studio Tools for Unity
– C# and Visual Basic
Game development with C++
Use the full power of C++ to build professional games powered by DirectX, Unreal, or Cocos2d
Description
Use the full power of C++ to build professional games for Windows, Xbox, and other consoles powered by DirectX, Unreal Engine, or Cocos2d.
Development applications
– Xbox live apps
– UWP apps on HoloLens
– Marble Maze
Components
– MSVC x64/x86 build tools
– Visual Studio C++ core features
– Windows Universal C Runtime
– C++ profiling tools
– Windows 10 SDK
– IntelliTrace
Data storage & processing
Connect, develop, and test data solutions using SQL Server, Azure Data Lake, Hadoop, or Azure ML
Description
The Data workload enables you to develop across a wide range of relational and big data assets. It provides you the tools to develop queries against databases, data warehouses and data lakes, whether on-premises or in Azure. It provides support for SQL, U-SQL, and Hive.
Development applications
– Use SQL Server Data Tools to design, deploy, and mantain databases
– Develop U-SQL scripts by using Azure Data Lake Tools
– Run Hive queries with Hadoop on Azure HDInsight
Components
– SQL Server Data Tools
– Azure Data Lake and Stream Analytics Tools
– .NET Framework 4.x development tools
Data science & analytical applications
Languages and tooling for creating data science applications, including Python and F#
Description
Use R and Python for wide range of scenarios such as data acquisition, cleaning, model training, deployment, and plotting. Use F#, a powerful functional-first .NET language, for a wide variety of data processing tasks.
Development applications
– Create visual data plots with R
– Work with the R interactive (REPL) window
– Create machine learning models using R and Microsoft ML Server
Components
– R and Microsoft R Client language support & runtime distributions
– Python and Anaconda language support & runtime distributions
– F# with the .NET framework language support & runtime distributions
Visual Studio extension development
Create add-ons and extensions for Visual Studio, including new commands, code analyzers, and tool windows
Description
Add the SDKs and tools you need to create new commands, code analyzers, tool windows, and language services using C#. Then, share your extension with the community in the Visual Studio Marketplace.
Development applications
– Extensions for Visual Studio IDE
– Software development kit development
Components
– Visual Studio SDK
– Visual Studio extension development prerequisites
– .NET profiling tools
– IntelliTrace
Office/SharePoint
Create Office and SharePoint add-ins, SharePoint solutions, and VSTO add-ins using C#, VB, and JavaScript
Description
Take advantage of various project templates to extend Office and SharePoint. Easily build, debug, and publish your add-ins and solutions from Visual Studio.
Development applications
– Office add-ins
– VSTO add-ins
– SharePoint add-ins
– SharePoint solutions
Components
– Office Developer Tools for Visual Studio
– .NET Framework 4.5 targeting pack
– .NET Framework 4.x development tools
– Developer Analytics tools
Linux development with C++
Create and debug applications running in a Linux environment or Windows Subsystem for Linux (WSL)
Description
Build and run code on a remote machine or Windows Subsystem for Linux and browse, edit, and debug from within Visual Studio. Use CMake to target multiple platforms from the comfort of a single IDE.
Development applications
– Get Started
Components
– Visual Studio C++ core features
– Windows Universal C Runtime
– Visual C++ for Linux Development
– Visual C++ tools for CMake and Linux
.NET Core cross-platform development
Build cross-platform applications using .NET Core, ASP.NET Core, HTML/JavaScript, and Containers including Docker support
Description
.NET Core is an open source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It’s cross-platform (supporting Windows, macOS, and Linux), and can be used to build device, cloud, and IoT applications.
Development applications
– Complete .NET Core solution on Windows
– Docker images for .NET Core applications
– C# console application using .NET Core
Components
– .NET Core 2.x development tools
– .NET Framework 4.x development tools
– ASP.NET and web development tools prerequisites
– Cloud tools for web development
– .NET profiling tools
– IntelliTrace
– Live Unit Testing
– Live Share Learn more about .NET Core cross-platform development
Free, fully-featured IDE for students, open-source and individual
developers
Professional developer tools, services, and subscription benefits for small
teams
End-to-end solution to meet demanding quality and scale needs of teams of all
sizes