Lab 1 CP
Lab 1 CP
This lab focuses on introducing the basics of programming with C++. Students will set up the
required IDE, write their first program, and learn about the structure of a simple C++ program.
By the end of this lab, students will have a strong foundation for future programming tasks.
3. Once the installation is complete, from the workloads select “Desktop Development with
C++” and click install.
4. Next, status screens appear that show the progress of your Visual Studio installation.
5. After the workloads and dependencies are installed, you’ll be asked to restart the system.
Restart and Launch Visual Studio. Now Create Your First Project!
9. Now you are required to name your project, after entering suitable name for your project,
click on “Create”.
10. Go to “Source Files” in the Solution Explorer, click on “Add”, then “New Item”.
11. Click on “Show all templates”, A new window shall appear. From the right menu, choose
“Visual C++”. Select C++ file (.cpp), and click “Add”.
13. Note that your program file (Source.cpp) comes with a .cpp extension, default for any C+
+ program.
14. Press ctrl + F5 to see the output on console. The output for the above code should look
like this:
15. Press any key to halt the program and get back.
Let us look at a simple code that would print the words Hello World.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl; // prints Hello World
system(“pause”);
return 0;
}
Let us look at the various parts of a typical C++ program, in reference to the above program.
The directive using namespace std; in C++ allows you to use all the symbols in
the standard namespace (std) without needing to prefix them with std::. This simplifies
the code when working with standard library features like cout, cin, etc. For example,
instead of writing std::cout << "Hello";, you can simply write cout << "Hello";.
While convenient for small programs, it can lead to potential naming conflicts in
larger projects or when multiple namespaces are involved, as it imports everything from
the std namespace into the global scope. For better practice, especially in larger
codebases, it’s recommended to use specific std prefixes or selectively use symbols like
using std::cout;.
4. Cin / cout
The next line cout << "Hello World"; causes the message "Hello World" to be
displayed on the screen.
In C++, cin and cout are used for handling input and output operations, respectively.
They are part of the iostream library and are essential for interacting with the user
through the console.
cin: (short for "character input") is used to receive data from the user. It works in
conjunction with the extraction operator (>>). This allows users to input values,
such as integers, strings, or characters, which are then stored in variables for
further use.
For example, to read an integer from the user and store it in a variable, you would
write: “cin >> age;” (Detailed explanation in next lab)
cout: (short for "character output") is used for displaying data on the console. It
works with the insertion operator (<<) and allows to output various types of
information, such as strings, numbers, or results of calculations, to the screen.
5. Comments
Comments are explanatory statements that are not executable lines of code, but are for
the programmers’ own understanding of the code. The statement “//prints hello world”
indicates a comment. In C++, comments start with a “//” for single line comments and are
written within “/*…*/” for multiple lined ones.
6. The “endl” keyword is used to push the next command in sequence to the next line.
8. The line return 0; in the main() function ends the program and signals to the operating
system that it ran successfully. Note that since the retun type of main() is int, we must
prompt an integer here, trying to return a floating point number (or any other datatype)
from a function whose return type is int(such as out main function) will cause a
compilation error.
Although modern C++ compilers allow you to omit return 0; at the end of main(),
explicitly including it is considered good practice for clarity and consistency.
9. The Semicolon (;)
In C++, the semicolon is a statement terminator. That is, each individual statement must
be ended with a semicolon. It indicates the end of one logical entity.
Lab Tasks
1. Copy the Hello World Program from the Manual and execute. Attach screenshots of
successful program execution and the output.
2. Write a program to display the following information on separate lines. (Make use of the
endl directive.) Execute the program to ensure the output is formatted correctly.
a) Your full name
b) Degree Program
c) Course name
d) Today's date (add this as a comment at the top of the program)
3. Write a program to display a box enclosed by ‘*’ on the console, such as the one shown
below. Use appropriate escape sequences where ever required.
4. Edit your program for Question # 02, by enclosing every line in double quotes. Use
appropriate escape sequences to include spaces and new line indicator to your program.
General Guidelines