0% found this document useful (0 votes)
21 views9 pages

Oop 01

Uploaded by

kamran2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Oop 01

Uploaded by

kamran2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Namal University, Mianwali

Department of Electrical Engineering

CS-101 (L) Computer Programming

Lab 01

Structure in C++
Student Name Roll No.

Suleman Ghani NIM-BSEE-2021-42

Instructor: Ma’am Naureen Shauket


Objectives
This lab is designed to give students practical insight of structures in C++
programming language. This lab will give students insight of organizing and
managing real- word data of different data types.
2. Outcomes
2.1 The student will be able to declare and use structures.
2.2 The student will be able to get nested structure concept.
Introduction:
3.1 Structure:
A structure is a user defined data type. Through structures you have the ability
to define a new type of data considerably more complex than the types we have
been using. A structure is a combination of several different data types. It is
declared by using the keyword struct followed by the structure name.
Method 1:
struct struct_name
{ Data_type1 member_name1;
Data_type2 member_name2; s
Data_type3 member_name3; };
// structure variable is defined in main
Method 2:
struct struct_name
{ Data_type1 member_name1;
Data_type2 member_name2;
Data_type3 member_name3; }
variable_1; // structure variable is variable_1
3.2 Defining multiple structure variables
You can define multiple variables of same structure data type in a single
program. The name of variables must be unique.
Lab Task
5.1. Write a program that declares a structure to store book Id, price and pages of a book.

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.

a) Write a function to display the details of all employees.

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;
};

// Function to input book details


void inputBookDetails(BookStore &book) {
cout << "Enter the book name: ";
cin.ignore(); // Clear the input buffer
cin.getline(book.book_name, 50);
cout << "Enter the price of the book: ";
cin >> book.book_price;
cout << "Enter the number of pages in the book: ";
cin >> book.book_pages;
}

// Function to display book details


void displayBookDetails(const BookStore &book) {
cout << "Book Name / Title: " << book.book_name << endl;
cout << "Book Price: $" << fixed << setprecision(2) << book.book_price << endl;
cout << "Book Pages: " << book.book_pages << endl;
}

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]);
}

// Determine and display the most expensive book


BookStore *mostExpensiveBook = &books[0];
for (int i = 1; i < numBooks; ++i) {
if (books[i].book_price > mostExpensiveBook->book_price) {
mostExpensiveBook = &books[i];
}
}

// Display the details of the most expensive book


cout << "\n///////////////////////////////////////////////" << endl;
cout << "Details of the most expensive book in the store:" << endl;
displayBookDetails(*mostExpensiveBook);

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;
};

// Function to increase salary for HR employees


void increaseSalary(Employee &employee) {
if (employee.dept == "HR") {
employee.salary *= 1.10; // Increase salary by 10%
cout << "Salary increased for HR employee " << employee.name << "!" << endl;
} else {
cout << "No salary increase for employee " << employee.name << "." << endl;
}
}

// Function to display employee details


void displayEmployeeDetails(const Employee &employee) {
cout << "Name: " << employee.name << endl;
cout << "ID: " << employee.id << endl;
cout << "Dept: " << employee.dept << endl;
cout << "Salary: " << employee.salary << endl;
}

int main() {
const int numEmployees = 2; // You can change this number as needed
Employee employees[numEmployees];

// Input employee data


for (int i = 0; i < numEmployees; ++i) {
cout << "Enter details for employee " << (i + 1) << ":\n";
cout << "Name: ";
getline(cin, employees[i].name);
cout << "ID: ";
cin >> employees[i].id;
cin.ignore(); // Clear the input buffer
cout << "Department: ";
getline(cin, employees[i].dept);
cout << "Salary: ";
cin >> employees[i].salary;
cin.ignore(); // Clear the input buffer
}

// Process salary increases and display details


for (int i = 0; i < numEmployees; ++i) {
increaseSalary(employees[i]);
displayEmployeeDetails(employees[i]);
cout << "----------------------------------" << endl; // Separator for clarity
}

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;
}

// Function to display the employee's name if tenure is 3 or more years


void display_if_tenure_is_3_or_more(int current_day, int current_month, int current_year) {
if (calculate_tenure(current_day, current_month, current_year) >= 3) {
cout << name << " (Code: " << code << ")" << endl;
}
}
};

int main() {
int n;
cout << "Enter number of employees: ";
cin >> n;

Employee employees[n]; // Array of employees

// Input employee data


for (int i = 0; i < n; ++i) {
cout << "Employee " << i + 1 << ":\n";
cout << "Code: ";
cin >> employees[i].code;
cin.ignore(); // To ignore the newline left in the input buffer
cout << "Name: ";
getline(cin, employees[i].name);
cout << "Joining Date (dd mm yyyy): ";
cin >> employees[i].day >> employees[i].month >> employees[i].year;
}

// Get the current date


int current_day, current_month, current_year;
cout << "\nEnter the current date (dd mm yyyy): ";
cin >> current_day >> current_month >> current_year;
// Display employees with 3 or more years of tenure
cout << "\nEmployees with 3 or more years of tenure:\n";
for (int i = 0; i < n; ++i) {
employees[i].display_if_tenure_is_3_or_more(current_day, current_month, current_year);
}

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.

You might also like