0% found this document useful (0 votes)
24 views5 pages

CP Lab 02

1) The document provides instructions for an experiment on getting started with a basic C++ program structure. It introduces the "Hello, World!" program as the simplest C++ program that outputs text. 2) The document explains the key components of a basic C++ program, including header files, namespaces, the main function, and using cout to output text. It provides sample code and an explanation of how the "Hello, World!" program works. 3) Students are asked to create their own C++ program to generate given output, applying the concepts taught in the document.

Uploaded by

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

CP Lab 02

1) The document provides instructions for an experiment on getting started with a basic C++ program structure. It introduces the "Hello, World!" program as the simplest C++ program that outputs text. 2) The document explains the key components of a basic C++ program, including header files, namespaces, the main function, and using cout to output text. It provides sample code and an explanation of how the "Hello, World!" program works. 3) Students are asked to create their own C++ program to generate given output, applying the concepts taught in the document.

Uploaded by

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

COMPUTER
PROGRAMMING
Experiment 2
Getting Started with A Basic C++ Program Structure

CLO 2. Use modern tools and languages for solving problems of varying
complexities

CLO 3. Construct the experiments/projects of varying complexities.

CLO 4. Demonstrate unique solution of problem under discussion.

1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Purpose
This experiment provides an introduction Fundamentals of a basic C++ Program. Students will
compile and run programs consisting of C++ building blocks.

Objectives:
At the end of this experiment you will:
1) Get familiar with the basic structure of C++ Program.
2) Able to run and check different but related programs consisting of C++ building blocks.

Equipment and Components:


3) Dev-C++ 5.0 Beta 9.2 (4.9.9.2) 9.0 MB with Minge/GCC 3.4.2
4) Code::Blocks IDE 20.03

2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

C++ "Hello, World!" Program


A "Hello, World!" is a simple program that outputs/Prints Hello, World! on the screen

// Your First C++ Program


#include <iostream>

using namespace std;

int main()
{
cout<<"Hello, World!" Program";
}
Note:
1. any line starting with // is a comment. Comments are intended for the person
reading the code to better understand the functionality of the program. It is
completely ignored by the C++ compiler.

2. The #include is a "preprocessor directive” that tells the compiler to put code from
the header called iostream into our program before actually creating the
executable (object Code). By including header files, you gain access to many
different functions. For example, iostream is necessary header file to use cout
object in our program that print output on the screen.

3. Consider a situation, when we have two persons with the same name, Zara, in the
same class. Whenever we need to differentiate them definitely we would have to
use some additional information along with their name, like either the area, if they
live in different area or their mother’s or father’s name, etc. Same situation can
arise in your C++ applications. For example, you might be writing some code that
has a function called xyz() and there is another library available which is also
having same function xyz(). Now the compiler has no way of knowing which
version of xyz() function you are referring to within your code.
A namespace is designed to overcome this difficulty and is used as additional
information to differentiate similar functions, classes, variables etc. with the same
name available in different libraries. Using namespace, you can define the context
in which names are defined. In essence, a namespace defines a scope.
4. In programming, we cannot have variables, functions, etc with the same name. So
to avoid those conflicts we use namespaces. “using namespace std” means we use
the namespace named std. std is an abbreviation for standard. So that means we use
all the things with in std namespace. If we don’t want to use this line of code, we
can use the things in this namespace like this. std::cout, std::endl.

5. int main() {...}


3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

A valid C++ program must have the main() function. The curly braces indicate the
start and the end of the function. The execution of code beings from this function.

6. cout object prints the content inside the quotation marks. It must be followed by
<< followed by the format string. In our example, "Hello World!" is the format
string.

7. ; is used to indicate the end of a statement.

8. The return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement.

Things to take away


 We use cout in order to print output on the screen.
 We must include iostream if we want to use cout.
 The execution of code begins from the main() function.

int main() {

// Write your code here


}

In C++, cout sends formatted output to standard output devices, such as the screen. We use the
cout object along with the << operator for displaying output.

Example: String Output

#include <iostream>
using namespace std;

int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}

Output

This is C++ Programming

4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

How does this program work?

 We first include the iostream header file that allows us to display output.
 The cout object is defined inside the std namespace. To use the std namespace, we used the
using namespace std; statement.
 Every C++ program starts with the main() function. The code execution begins from the
start of the main() function.
 cout is an object that prints the string inside quotation marks " ". It is followed by the <<
operator.
 return 0; is the "exit status" of the main() function. The program ends with this statement;
however, this statement is not mandatory.

Task
 Create a C++ Program to Generate the Following Output. (10 Marks)

You might also like