Oop 01
Oop 01
Lab 01
Structure in C++
Student Name Roll No.
a) The structure should include functions to assign user defined values to each book and display the
record of costliest book.
5.2. Objective: Create a structure to store information about employees. The structure should include
fields like name, employee ID, department, and salary.
b) Write a function to increase the salary by 10% for employees in the "HR" department.
TASK 5.1
Code
#include <iostream>
#include <iomanip> // For std::fixed and std::setprecision
using namespace std;
struct BookStore {
char book_name[50];
float book_price;
int book_pages;
};
int main() {
const int numBooks = 3;
BookStore books[numBooks];
// Input details for each book
for (int i = 0; i < numBooks; ++i) {
cout << "\nEntering details for Book " << (i + 1) << ":\n";
inputBookDetails(books[i]);
}
return 0;
}
Description:
I have defined a structure called {book_store` in this code, which contains details about books such as
title, price, and page count. This code asks the user to enter the details of three or more books,
compares their prices, and displays the information for the most expensive book. To obtain the book
with the highest price and display its details appropriately, I have used basic conditional statements
(`if}, {else if}, and {else}) in this code.
.Output:
Code 5.2
#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
int id;
string dept;
float salary;
};
int main() {
const int numEmployees = 2; // You can change this number as needed
Employee employees[numEmployees];
return 0;
}
Description:
I have defined a structure called {one_piece} in this lab task to store employee data, such as name,
ID, department, and pay. Additionally, the code contains a function called {increase_salary} that
raises an employee's pay by 10% and determines if they are part of the "HR" department. The {main}
function applies the salary increase function and generates two employee instances.
, and then displays the updated details of both employees.
Output:
Home task:
Code:
#include <iostream>
#include <string>
using namespace std;
struct Employee {
int code;
string name;
int day, month, year; // Joining date (dd, mm, yyyy)
// Function to calculate the number of years between the current date and joining date
int calculate_tenure(int current_day, int current_month, int current_year) {
int years = current_year - year;
if (current_month < month || (current_month == month && current_day < day)) {
years--; // Adjust if the anniversary hasn't happened yet this year
}
return years;
}
int main() {
int n;
cout << "Enter number of employees: ";
cin >> n;
return 0;
}
Output:
Description:
In this lab task, I have defined a structure called {one_piece} to hold employee data, including name,
ID, department, and pay. The code also includes a function called {increase_salary} that checks if an
employee is under the "HR" department and increases their pay by 10%. Two employee instances are
created by the {main} function, which also applies the salary increase function.
Conclusions:
In this lab I had concluded that a variety of C++ programs in this lab that successfully show how to use
structures, functions, and fundamental control flow. This lab will help students gain an understanding
of C++ structures by showing them how to create data types with multiple attributes and behaviours
(functions). By going through these task I have gained practical experience in input/output operations,
structure, and conditional statements. All things considered, these exercises helped with
comprehension of the core ideas of C++ in OOP.