Object Oriented Programming: Lab 01 Introduction To C++ (Revision of Control Structure, Arrays, Functions)
Object Oriented Programming: Lab 01 Introduction To C++ (Revision of Control Structure, Arrays, Functions)
1
Lab Tasks
5.1. Write a program that declares a structure to store book Id, price and pages of a book. The
structure should include functions to assign user defined values to each book and display the record of
most costly book.
Program: Explanation:
The displayMostCostlyBook
function takes an array of Book
objects and its size as
parameters. It iterates through
the array to find the book with
the highest price and then
displays its information.
2
book and frees the dynamically
allocated memory.
Output:
5.2. Write a program to take the values of two integers and use pointers to add 10 to the value of
each integer.
Program: Explanation:
This program that takes the values of
two integers and uses pointers to
add 10 to the value of each integer.
3
In the main function:
We declare two integer variables
num1 and num2 to store the user's
input.
We prompt the user to input two
integers.
Then call the addTen function twice,
passing pointers to num1 and num2.
This effectively modifies the values of
num1 and num2 by adding 10 to
each of them.
Then we display the updated values
of num1 and num2 to the user.
Output:
5.3. Write a function that swaps the values of two integer variables
a. using pass by value b. and pass by reference and see their differences
Program: Explanation:
The program demonstrates the
differences between pass by value
and pass by reference. After swapping
by value, the original values of a and
b remain unchanged, while after
4
swapping by reference, the values of
a and b are swapped.
Main Function:
Declares two integer variables a and
b.
Prompts the user to enter the values
of a and b.
Prints the values of a and b before
swapping.
Calls the swapByValue function with
a and b as arguments.
Prints the values of a and b after
swapping using pass by value.
Calls the swapByReference function
with a and b as arguments.
Prints the values of a and b after
swapping using pass by reference.
Output:
5
Home Tasks
6.1 There is a structure called employee that holds information like employee code, name, date of
joining. Write a program to create an array of the structure and enter some data into it. Then ask the
user to enter current date. Display the names of those employees whose tenure is 3 or more than 3
years according to the given current date.
Program: Explanation:
This C++ program manages
employee records through a
defined structure named
Employee. This structure
encapsulates essential
employee information,
including their unique code,
name, and the date of joining
(DOJ). It facilitates structured
organization and manipulation
of employee data within the
program.
Within the main() function, an
array employees of type
Employee is declared, capable
of accommodating data for up
to five employees. This array
serves as a container to hold
the details of each employee
entered by the user, such as
their code, name, and date of
joining.
Through a loop, the program
prompts the user to input data
for each employee, iterating
over the array's elements. For
each iteration, the user is
prompted to provide the
employee's code, name, and
date of joining. This data is
then stored in the
corresponding fields of the
Employee structure within the
employees array.
The user is prompted to input
the current year, which is
essential for calculating each
employee's time period. This
input is important for
determining the length of time
6
each employee has been with
the company.
The program then compares
the current year with the date
of joining of each employee. It
iterates through the
employees array , checking if
the difference between the
current year and the date of
joining is three years or more.
For employees meeting this
tenure requirement, their
names are displayed.
Output:
6.2. Write a function to sort data (in increasing order) in an array using
7
Program: Explanation:
This program defines two
functions: sortArrayByValue and
sortArrayByReference, which
implement the Bubble Sort
algorithm to sort an array of
integers. Bubble Sort is a simple
sorting algorithm that compares
adjacent elements, and swaps
them if they are in the wrong
order. This process is repeated
until the entire array is sorted in
ascending order.
In the main function, an array
arr containing integers {55, 45,
10, 70, 35} is declared, and the
size of the array is determined
using sizeof(arr) / sizeof(arr[0]).
Next, the array arrByValue is
created and initialized with the
same elements as arr. This array
will be sorted using the
sortArrayByValue function. The
std::copy function is used to
copy the elements of arr to
arrByValue.
The sortArrayByValue function is
then called with arrByValue and
its size as arguments. This
function sorts the elements of
the arrByValue array in
ascending order using the
Bubble Sort algorithm.
The sorted array arrByValue is
then printed, demonstrating the
sorting of the array by value.
Similarly, the
sortArrayByReference function
is called with the original arr
array and its size as arguments.
This function sorts the elements
of the arr array in ascending
order using the Bubble Sort
algorithm.
Finally, the sorted array arr is
printed, demonstrating the
sorting of the array by reference.
8
Output:
6.3. Write a program that inputs a string value from the user and displays it in reverse using pointers.
Program: Explanation:
This C++ program takes a string input from
the user and displays the input string in
reverse using pointers.
Initially, the program declares a character
array str of size 100 to store the user input
string. The program then prompts the user
to enter a string using the cout statement
within the namespace std. The user input
string is read and stored in the str array
using cin.getline().
A pointer ptr is initialized to point to the
last character of the input string by
calculating the position using strlen(str) -
1.
The program then enters a loop where it
iterates over the string in reverse by
decrementing the pointer ptr until it
reaches the beginning of the string.
Within the loop, each character pointed to
by ptr is printed using cout.
Finally, the program displays the input
string in reverse order by printing each
character in reverse.
Output: