C++ Lab Report Section A
C++ Lab Report Section A
2016 Pre-Engineering
SECTION-A
1.Amanuel Ginbo . . . . . . . . . . . . . . . . . . .NSR/084/16
2.Atnan Chali . . . . . . . . . . . . . . . . . . . . . NSR/1164/16
3.Belete Beyhun . . . . . . . . . . . . . . . . . . . .NSR/152/16
4.Dagmawi Wube . . . . . . . . . . . . . . . . . . NSR/092/16
5.Dina Zilo. . . . . . . . . . . . . . . . . . . . . . . . NSR/T/019/16
6.Etsubdink Ayalew. . . . . . . . . . . . . . . . . NSR/327/16
7.Haile Tegenu. . . . . . . . . . . . . . . . . . . . . NSR/450/16
8.Masresha Dagmawi . . . . . . . . . . . . . . . NSR/1404/16
9.Milkyas Tesfaye. . . . . . . . . . . . . . . . . . NSR/699/16
10.Molalign Tsegaye. . . . . . . . . . . . . . . . NSR/717/16
11.Nathan Kaleb. . . . . . . . . . . . . . . . . . . . NSR/T/011/16
C++ is a powerful general-purpose programming language developed by Bjarne Stroustrup in 1985. It was designed as
an extension of the C programming language to add features for high-level programming while retaining the efficiency
and flexibility of C.
C++ is widely used for systems programming (like operating systems, browsers, and games), application development,
and embedded systems. One of its key features is support for object-oriented programming (OOP), which allows
developers to create classes, objects, and methods, thereby making code more reusable and modular.
In addition to OOP, C++ supports other paradigms such as procedural and functional programming. With features like
direct memory manipulation using pointers, it’s an ideal choice for performance-critical applications. Its standard library
provides data structures, algorithms, and input/output functions, making it a comprehensive tool for developers.
• Developer Name
1.Amanuel Ginbo
2.Atnan Chali
3.Belete Beyhun
4.Dagmawi Wube
5.Dina Zilo
6.Etsubdink Ayalew
7.Haile Tegenu
8.Masresha Dagmawi
9.Milkyas Tesfaye
10.Molalign Tsegaye
11.Nathan Kaleb
• Developed Lab Name
2. C++ Comments
Comments are non-executable statements in C++ that help explain the code. While they don't affect the program's
functionality, they improve code readability and help other developers understand the logic behind the code. C++ supports
two types of comments:
1. Single-line comments:
Single-line comments start with // and continue until the end of the line. They are typically used for brief explanations or
to comment out specific lines of code.
Example:
2. Multi-line comments:
Multi-line comments start with /* and end with */. These are useful for longer explanations or for temporarily disabling
multiple lines of code during debugging.
Example:
int sum = 0;
✓ Variables
Variables in C++ are used to store data that the program will use and manipulate during execution. Each variable has a
specific data type, such as int, float, or char, which determines the kind of value it can hold.
A variable is declared by specifying the data type followed by the variable name.
Example:
✓ Literals
Literals are fixed values that appear directly in the code and do not change. They represent constant values of basic data
types.
Examples:
Integer literal: 42
Literals are assigned to variables and help define specific values that a variable will store.
✓ Constants
Constants are variables whose value cannot be changed once they are initialized. In C++, you can define constants using
the const keyword or preprocessor directives like #define.
1. Using const:
2. Using #define:
Constants are often used for values that do not change throughout the program, such as mathematical constants (e.g., PI),
or configuration values (e.g., maximum allowed users).
4. C++ Data Types
C++ supports various fundamental data types that allow programmers to define the kind of data a variable will hold.
Understanding the different data types is crucial for proper memory allocation and performance optimization.
1. int (Integer):
Example:
Example:
3. double:
Used to store double-precision floating-point numbers, providing more precision than float.
Example:
4. char (Character):
Example:
5. bool (Boolean):
Example:
Array: Used to store multiple values of the same type in contiguous memory locations.
5. C++ Constants
Constants in C++ are variables that are declared with a fixed value that cannot be altered during the execution of the
program. Defining constants is essential for making your code more understandable and less prone to errors.
This method was more commonly used in C programming but is still supported in C++.
#define PI 3.14159
Using constants improves the readability of the code by giving meaningful names to fixed values. It also makes
maintenance easier, as changes can be made in one place instead of multiple occurrences in the code.
C++ provides standard input and output operations through the iostream library, which contains objects like cin (for input)
and cout (for output). These are part of the C++ Standard Library and are essential for interacting with the user.
The cin object is used to accept input from the user. It reads input from the keyboard and assigns it to a variable.
#include <iostream>
int main() {
int num;
return 0;
The cout object is used to display output to the console. You can use the insertion operator << to send data to cout.
You can chain multiple cout operations in a single line using the insertion operator:
cout << "The sum is " << sum << " and the product is " << product << endl;
This flexibility makes cout and cin essential for building interactive applications in C++.
7. C++ Operators
Operators in C++ are symbols that tell the compiler to perform specific mathematical, logical, or relational operations.
Operators form the core of any program, enabling calculations, decision-making, and data manipulation.
✓ Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
Example:
✓ Relational Operators:
== (Equal to)
Example:
✓ Logical Operators:
Logical operators are used to combine or modify boolean expressions and return a boolean result (true or false). They are
often used in control flow statements like if, while, or for loops.
Example:
bool result = (true && false); // result is false because one operand is false
• Logical operators are crucial in controlling program flow, especially when combining multiple conditions in an if
or while statement.
✓ The if Statement:
The if statement allows the program to execute a block of code only if a specified condition evaluates to true.
Example:
In this case, if the age is 18 or greater, the message "You are eligible to vote" will be printed.
If the condition in an if statement is false, the else statement allows the program to execute an alternate block of code.
Example:
} else {
Here, since the age is less than 18, the message "You are not eligible to vote" will be printed.
Example:
} else {
The for loop is used to iterate over a sequence (such as numbers in a range or elements in an array) a specific number of
times.
// loop body
Example:
In this loop, the value of i starts at 0, and as long as i is less than 5, the loop runs, printing the value of i and incrementing
it by 1 each time.
Syntax:
while (condition) {
// loop body
Example:
int i = 0;
while (i < 5) {
i++;
The do...while loop is similar to the while loop, but it guarantees that the loop body will be executed at least once, even if
the condition is false.
Syntax:
do {
// loop body
} while (condition);
Example:
int i = 0;
do {
i++;
In this case, the loop body will execute at least once even if i is initially greater than or equal to 5.
The break statement is used to immediately exit a loop, regardless of the condition.
Example:
if (i == 5) {
break; // Loop stops when i equals 5
In this loop, when i becomes 5, the loop terminates, and no further iterations are performed.
The switch statement provides a way to execute different parts of code based on the value of a variable or expression.
Syntax:
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// default statements
Example:
int day = 3;
switch (day) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
cout << "Invalid day";
In this example, since day is 3, the message "Wednesday" will be printed. If day had no matching case, the default case
would be executed.
An array is a data structure that can hold multiple values of the same data type. In C++, arrays are used to store a
collection of values in contiguous memory locations.
Syntax of an Array:
dataType arrayName[arraySize];
Example:
Here, numbers is an array that can hold 5 integers. The elements are initialized to the values 1, 2, 3, 4, and 5.
You can access an element of an array using the index, starting from 0:
• Arrays are useful when you need to store a fixed-size collection of elements, such as a list of numbers or
characters.
As shown in the previous example, 1D arrays store a sequence of elements of the same type.
Example:
▪ Multidimensional Arrays: C++ also supports multidimensional arrays, where arrays are nested within arrays.
The most common form is the two-dimensional array, which is used to represent a matrix.
Syntax:
dataType arrayName[rows][columns];
In C++, you can pass an entire array to a function by specifying the array name as the argument. Since the array name
represents the memory address of the first element, you are essentially passing a pointer to the function.
Syntax:
Example:
int main() {
printArray(myArray, 3);
return 0;
As discussed earlier, multidimensional arrays, particularly 2D arrays, are used for storing data in rows and columns. You
can define and manipulate 3D arrays or higher, although this is less common in most basic applications.
Example of a 2D Array:
In C++, strings can be created using either character arrays or the std::string class from the C++ Standard Library. The
std::string class offers a more flexible and efficient way to manipulate strings compared to character arrays.
#include <iostream>
#include <string>
int main() {
return 0;
The std::string class allows you to easily perform string operations such as concatenation, finding substrings, and
modifying characters. For example:
A pointer is a variable that stores the memory address of another variable. Pointers are very powerful and allow you to
manipulate memory directly.
Syntax of a Pointer:
dataType* pointerName;
Example:
cout << "Address of var: " << ptr << endl; // Outputs the memory address
cout << "Value of var: " << *ptr << endl; // Outputs the value of var (42)
In this example, the pointer ptr holds the address of the variable var. The dereference operator * is used to access the
value stored at the memory address.
1. Null Pointer: A pointer that does not point to any valid memory location.
void* ptr;
int x = 10;
int x = 5;
Pointers and arrays are closely related in C++. The name of an array is essentially a pointer to its first element. Therefore,
you can use pointers to iterate through an array.
Example:
cout << *(ptr + i) << " "; // Access array elements using pointer arithmetic
In this example, the pointer ptr is used to access each element of the array arr. Pointer arithmetic (ptr + i) allows you to
move through the array, and dereferencing (*) gives access to the array elements.