CP Lab 02
CP Lab 02
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
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
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.
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
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.
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.
8. The return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement.
int main() {
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.
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}
Output
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
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)