0% found this document useful (0 votes)
3 views9 pages

Lab 1 CP

This document outlines Lab #01 for the CS-107 Computer Programming course at NUST, focusing on the introduction to C++ program structure and the use of the MS Visual Studio IDE. It includes detailed instructions for setting up the software, writing a simple 'Hello World' program, and understanding various components of a C++ program such as headers, namespaces, and the main function. Additionally, it provides lab tasks for students to complete, emphasizing proper coding practices and originality in submissions.

Uploaded by

mohsinsubhani96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab 1 CP

This document outlines Lab #01 for the CS-107 Computer Programming course at NUST, focusing on the introduction to C++ program structure and the use of the MS Visual Studio IDE. It includes detailed instructions for setting up the software, writing a simple 'Hello World' program, and understanding various components of a C++ program such as headers, namespaces, and the main function. Additionally, it provides lab tasks for students to complete, emphasizing proper coding practices and originality in submissions.

Uploaded by

mohsinsubhani96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer and Software Engineering

College of E&ME, NUST, Rawalpindi

CS – 107: Computer Programming

LAB # 01: Introduction to C++ Program Structure & IDE

Course Instructor: Assoc. Prof. Dr. Farhan Hussain

Lab Instructor: Engr. Ayesha Khanam

Sr. No. Student’s Name CMS ID

CS – 107: Computer Programming 1


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

Lab # 01: Introduction to C++ Program Structure and IDE

Software Requirement(s): MS Visual Studio

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.

Download and install Microsoft Visual Studio

1. Go to the Visual Studio Community from the link given below:


https://visualstudio.microsoft.com/vs/older-downloads/

2. Download and install VisualStudioInstaller.exe, You will be directed to a dialogue box,


click “Continue”.

CS – 107: Computer Programming 2


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

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!

6. Click on “Continue without Code”.

7. Select File → New → Project.

CS – 107: Computer Programming 3


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

8. Another dialogue box will appear, click on “Empty 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”.

CS – 107: Computer Programming 4


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

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”.

12. Write your first C++ code.

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.

CS – 107: Computer Programming 5


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

Writing your first C++ Program

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.

1. The Header File


The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream> is needed.
It is a header file/library that lets us work with input and output objects, such as cout or
cin.

2. The Standard Namespace


A namespace in C++ is a container that organizes code into groups and helps avoid
naming conflicts. It allows you to have multiple identifiers (like variables, functions, and
classes) with the same name in different parts of a program without causing errors.

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;.

CS – 107: Computer Programming 6


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

3. The Main Function


The line int main() is where program execution begins. This indicates the start of the
main function, whose return type is “int”. The scope of the main function is enclosed
within {…}.(Detailed explanation about datatypes in next lab)

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.

7. The system("pause") command in C++ is a Windows-specific feature that pauses the


program and displays the message "Press any key to continue . . .", waiting for user
input. Once the user presses any key, the program is halted.

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)

CS – 107: Computer Programming 7


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

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.

10. Escape Sequences


In C++, escape sequences are special character combinations that represent characters or
actions that cannot be directly typed or would otherwise be difficult to include in a string.
These sequences always start with a backslash (\) followed by one or more characters.

CS – 107: Computer Programming 8


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

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

 Properly comment and indent the code.


 Make the console intuitive.
 Attach the code and screenshot of the console for each task.
 Ensure originality in your documentation. Plagiarized reports get zero credit, whatsoever.
 Make submissions well in time to earn a maximum score.
 In case of any queries related to the lab, reach me out at [email protected] .

CS – 107: Computer Programming 9

You might also like