Lab No. 08
C++ Programming Basics (Basic Input / Output, Directives, Arithmetic
Operators, Building Blocks of Programming Language)
Objectives:
Understand the Basics of C++
Programming
working
using visual studio 2008.
Tools:
Visual
Studio 2008.
Working
With Visual Studio 2008
Open Visual Studio 2008. You will see the start page.
Select "File
Menu-->New-->Project"
In
the dialog box that pops up, select Visual C++ in project type and select
"Win32 Console Application." Enter the name of project and leave the
default path the same (Figure 1.2) you can leave the Create Directory for
Solution box checked and you can change the path also). Click ok.
Make sure that the following are checked (as shown):
1. Under "Application type": Console Application
2. under "Additional Options": Empty Project
3. Then Press Finished
After clicking on finish,
project will be created. Now you need to add a CPP file to the project. Go to
the "Solution Explorer" on the left side and right-click on
"Program1" (name of project). Once you have right clicked on Program1
select "Add-->New Item"
A Sample Program:
#include <iostream>
using namespace std;
int main()
{ int i;
cout << "This is output.\n";
// this is a single line comment /* you can still use C style
comments */
// input a number using >>
cout << "Enter a number: ";
cin >> i;
// now, output a number using <<
cout << i << " squared is " << i*i
<< "\n";
return 0;
}
Output of the Program:
Enter a number: _
If
we enter the number 4 then the program displays
Squared is 16
Screenshots:
Go to the Build Menu and select "Build Solution" (Figure
1.6).
Go to the Debug Menu and select “Start
without Debugging” or simply PRESS F5 on your keyboard, and a small box should
pop up that is running your program that will add 2 numbers together
Debugging your program in Visual
Studio 2008
The most basic aspect of any debugging session is the use of
breakpoints, which allows you to specify a location in your application (a line
of code) where program execution will stop (break) and allow you to inspect the
internal state of the application at that exact moment.
To
add a break point place mouse pointer on the gutter (narrow gray strip on left
of code) and click on it in the line you want to debug. A red circle will
appear
To run your program in debugging mode, select
'Start Debugging' from Debug menu. Program execution will stop at the line of
break point
Yellow arrow is pointing to the line which will be executed next.
At this point, you can view the contents of
All
variable in different tabs of bottom left window
Watch will
allow the user to select the variables and objects in the program and display
their values
Autos is
very similar to the watch and locals windows except along with the value of the
object or variable, it also displays the expressions from the current line of
code some of it around it. You can see that it picks up more than just the
current scope of the instruction pointer; it has picked up the variables from
the entire function
Bottom Right window allow you to view the
following:
Call Stack is
very useful when a breakpoint is hit or when the program calls an exception.
Assuming the call stack is intact, it gives the user a history of program calls
to get to the stop execution point. The user can then select any of the entries
to go back in time of the execution and trace backwards things that were
happening.
Breakpoints window
displays the status and the descriptive items associated with program
breakpoints
Output:
The 'Trace' window, it is usually displayed in both code and debug views.
Now
you can use following options for single stepping from Debug menu
Step into (F11):
Step Into executes only the call itself, then halts at the first line of code
inside the function. Use Step Into to go inside your method and stop
immediately inside it.
Step Over: Moves
to the next step in your code also, but doesn't break inside any methods.
Step Out: Ignores the rest of the current method and goes to the calling
method.
After
debugging desired portion of code, you can execute the remaining code at once
by selecting Continue from Debug menu.
Advanced Breakpoints
In
order to set an advanced breakpoint, you must first set a normal breakpoint and
then choose a modifier to set from a context menu. To display the context menu
from a breakpoint symbol in the gutter, simply right click mouse on the
breakpoint symbol or the line containing the breakpoint
Assignment #1
Ans1
Ans 2
0 comments:
Post a Comment