Lab Manual 2 C++ PDF
Lab Manual 2 C++ PDF
PROGRAMMING
FUNDAMENTALS
Experiment 2
Fundamentals of a C++ Program
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
Objectives:
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
Features of C++
• C++ is fast
This offers a huge boost in speed that high level languages like Python, Java
don’t give you.
• C++ is statically typed
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
The power of C++ extends with the use of standard libraries contained in it.
These libraries contain efficient algorithms that you use extensively while
coding. This saves sufficient amount of programming effort, which otherwise
would have been wasted reinventing the wheel
1. C++ is irreplaceable
Moreover, the fact that there’s a huge community improving C++ on every
iteration means that it is only expected to be used even more in the coming
future.
2. You learn the internal architecture of a computer
Since, C++ is a middle level language, you will write code that interacts
directly with the internal hardware of the computer.
You’ll learn how the computer memory really works, how information is stored
in them, how you can retrieve them and so on. (Data Structures Course)
Github, the leading open source collaboration platform, has over 600,000
repositories for C++ alone.
This metric itself proves the worth of C++ in the open source community as
well.
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Be it gaming, graphics, windows applications, you can find tons of great open
source projects extensively used today. And, you can always create your own.
So, you can expect many great developers to help you solve real-life
problems using C++.
C++ developers can expect an average of yearly $100,000 salary with over
7,700 jobs advertised every month.
Learning any language takes time and that holds even more truth for C++.
If you are here to learn C++ in a day, then you’re going to end up facing failure.
You only start learning with regular practice and dedication. So, I suggest you
to invest valuable time learning C++.
People have different theories whether one should learn C before C++ or not.
If you ask me, it isn’t a must. You can easily start with C++. If you already
know C, you will have a head start in learning C++ as they have similar
attributes like syntax and semantics.
5
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.
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
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.
5. 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. The return 0; statement is the "Exit status" of the program. In simple terms,
the program ends with this statement.
int main() {
C++ Output
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.
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}
Output
• 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.
Note: If we don't include the using namespace std; statement, we need to use std::cout
instead of cout.
#include <iostream>
int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
To print the numbers and character variables, we use the same cout object but without using
quotation marks.
#include <iostream>
using namespace std;
int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
Output
70
256.783
character: A
Notes:
• The endl manipulator(that is used to change the formatting of output) is used to insert a
new line. That's why each output is displayed in a new line.
• The << operator can be used more than once if we want to print different variables, strings
and so on in a single statement. For example:
C++ Input
In C++, cin takes formatted input from standard input devices such as the keyboard. We use the
cin object along with the >> operator for taking input.
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
Output
Enter an integer: 70
The number is: 70
to take input from the user. The input is stored in the variable num. We use the >> operator with
cin to take input.
int main() {
char a;
int num;
return 0;
}
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Output
Task
• Create a C++ Program to Generate the Following Output. (10 Marks)
11