Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bsee21036 Lab11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Electrical Engineering Department - ITU

CS152L: Object Oriented Programming Lab

Course Instructor: Nadir Abbas Dated:

Lab Engineer: Usama Riaz Semester: Summer 2023

Session: Batch:

Lab 11. Solving Problems by Utilization of Structs

Obtained Marks/100
Name Roll number

Muhammad Sami Ullah BSEE21036

Checked on: ____________________________

Signature: ____________________________
Objective
The objective of this lab is to observe the basic knowledge of programming in C++.
Equipment and Component
Component Description Value Quantity
Computer Available in lab 1
Conduct of Lab
• Students are required to perform this experiment individually.
• In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab
attendants.

Theory and Background


In C++, a 1D array is declared by specifying the data type of the elements followed by the array
name and the size of the array in square brackets. For example, int numbers[5]; declares a 1D
integer array named numbers with a size of 5 elements. Elements in a 1D array can be accessed
using the array name and the index, starting from 0. For instance, numbers[0] refers to the first
element in the array.

A 2D array in C++ is essentially an array of arrays. It is declared by specifying the data type of
the elements, followed by the array name and the size of the rows and columns in square
brackets. For example, int matrix[3][4]; declares a 2D integer array named matrix with 3 rows
and 4 columns. Elements in a 2D array can be accessed using two indices, one for the row and
one for the column. For instance, matrix[1][2] refers to the element in the second row and third
column of the array.

In C++, a char array is used to represent a sequence of characters, typically used for strings. It
is declared by specifying the data type char, followed by the array name and the size of the array
in square brackets. For example, char name[10]; declares a char array named name with a size of
10 characters. Char arrays are terminated by a null character ('\0') placed at the end, indicating
the end of the string. Functions from the <cstring> library, such as strcpy, strlen, can be used to
manipulate char arrays.

In C++, a pointer is declared by specifying the data type followed by an asterisk () and the
pointer name. Pointers hold the memory address of another variable. For example, int* ptr;
declares a pointer named ptr that can hold the memory address of an integer variable. Pointers
are used to indirectly access variables and their values using the dereference operator (). Pointers
are particularly useful in dynamic memory allocation, accessing elements in arrays, and passing
arguments to functions by reference.

Structures in C++ provide a convenient way to group related variables together into a single
entity. By defining a structure, you can create a custom data type that represents a specific
concept or object in your program. The member variables within a structure can have different
data types, allowing you to store and organize diverse information. Structures are commonly
used to represent real-world entities such as people, products, or coordinates.

Lab Task
Part A [Marks: 5]

Please follow the following steps before starting below tasks:

• Create function.h file for declaration of all functions


• Create function.cpp file to define define all declared functions.
• Create main.cpp file for driving code/call functions.

Note: Make a menu driven program (compulsory).

Part B: Implementation of Structure with Pointers & with Arrays (DMA) [Marks:
35]

• Create a structure called "Employee" that represents an employee's information.

The structure should have the following member variables:

name: The name of the employee (a string).


employeeID: The unique ID of the employee (an integer).
department: The department where the employee works (a string).

Implement the following functions for the Employee structure:

CreateEmployee: Create an Employee object dynamically and initialize its member


variables with the provided values. Return a pointer to the created Employee object.
DisplayEmployeeDetails: Accept a pointer to an Employee object and display its name,
employee ID, and department.

In a program, create two Employee objects dynamically using the CreateEmployee


function, prompt the user to enter the details for each employee, and display their details
using the DisplayEmployeeDetails function.

• Create a structure called "Product" that represents product information.

The structure should have the following member variables:

name: The name of the product (a string).


price: The price of the product (a floating-point number).
quantity: The quantity of the product available (an integer).

Implement the following functions for the Product structure:

SetProductDetails: Set the details of the product (name, price, and quantity).
DisplayProductDetails: Display the details of the product (name, price, and quantity).

In a program, create an array of Product objects with a fixed size. Prompt the user to
enter the details for each product in the array and display their details using the
DisplayProductDetails function.

// Paste your code here


//Function.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include<iostream>
#include"Functions.h"
using namespace std;
template < typename A > // template function
struct Employee
{ // struct
string name; // declartion
A employeeID; // declartion
string department; // declartion
};

template < typename A > // template


Employee < A > *createEmployee (const string & name, A employeeID,
const string & department)
{ // function declaration
Employee < A > *employee = new Employee < A >;
employee->name = name;
employee->employeeID = employeeID;
employee->department = department;
return employee; // returning
}

template < typename A > void


displayemployeedetails (Employee < A > *employee)
{ // function
cout << "Name: " << employee->name << endl; // displaying the output and moving to next
line
cout << "Employee ID: " << employee->employeeID << endl; // displaying the output and
moving to next line
cout << "Department: " << employee->department << endl; // displaying the output and
moving to next line
}
template<typename A>// template function
struct Product {// struct
string name;// declartion
A price;// declartion
int quantity;// declartion
};

template<typename A>// template function


void ProductDetails(Product<A>& product, const string& name, A price, int quantity) {//
function
product.name = name;
product.price = price;
product.quantity = quantity;
}

template<typename A>
void displayProductdetails(const Product<A>& product) {
cout << "Name: " << product.name << endl;
cout << "Price: " << product.price << endl;
cout << "Quantity: " << product.quantity << endl;
}

#endif
//main.cpp
#include"Functions.h"
#include<iostream>
using namespace std;//cout library
int main() {// start of main function
int x;
cout<<"OPTINS:-\n 1)Task 01\n 2)Task02\n";
cout<<"Enter a number = ";
cin>>x;
switch(x)
{
case 1:
{
// start of main function
// Create two Employee objects dynamically
Employee < int >*employee1 = createEmployee ("Sami Ullah", 1, "Sales");
Employee < int >*employee2 = createEmployee ("Abdul Rahman", 2, "Marketing");

// user to enter details for each employee


string name; // declartion
int employeeID; // declartion
string department; // declartion

cout << "Enter details for 1st Employee :" << endl; // displaying the output and moving
to next line
cout << "Name: "; // displaying the output
cin>>name;
cout << "Employee ID: "; // displaying the output
cin >> employeeID; // input from user
cin.ignore ();
cout << "Department: "; // displaying the output
cin>>department;
cout << endl; // displaying the output

// Update the details of employee1


employee1->name = name;
employee1->employeeID = employeeID;
employee1->department = department;

cin.ignore (); // Ignore the newline character in the input buffer

cout << "Enter Details for 2nd Employee :" << endl; // displaying the output
cout << "Name:= "; // displaying the output
cin>>name;
cout << "Employee ID:= "; // displaying the output
cin >> employeeID;
cout << "Department:= "; // displaying the output
cin>>department;
cout << endl; // displaying the output
// Update the details of employee2
employee2->name = name;
employee2->employeeID = employeeID;
employee2->department = department;

// Display the details of both employees


cout << "details of employee 1:" << endl; // displaying the output
displayemployeedetails (employee1);
cout << endl;// displaying the output

cout << "details of employee 2:" << endl;


displayemployeedetails (employee2);

// Clean up memory by deleting the dynamically allocated objects


delete employee1;
delete employee2;

break;
}
case 2:
{

const int ARRAY_SIZE = 3;


Product<double> products[ARRAY_SIZE];

for (int i = 0; i < ARRAY_SIZE; i++) {


string name;
double price;
int quantity;

cout << "Enter details for Product " << i + 1 << ":" << endl;
cout << "Name: ";
cin>>name;
cout << "Price: ";
cin >> price;
cout << "Quantity: ";
cin >> quantity;
cin.ignore(); // Ignore the newline character in the input buffer
cout << endl;

ProductDetails(products[i], name, price, quantity);


}

cout << "Product details:" << endl;


for (int i = 0; i < ARRAY_SIZE; i++) {
cout << "Product " << i + 1 << " details:" << endl;
displayProductdetails(products[i]);
cout <<endl;
}

break;
}
default:
{
cout<<"You eneterd a wrong number !!";
}

}
return 0;
}
// Paste your output here

Assessment Rubric for Lab

Performance Max
Task CLO Description Exceeds expectation Meets expectation Does not meet expe
metric marks
Executes without errors Executes without errors, user
Does not execute du
excellent user prompts, prompts are understandable,
errors, runtime error
1. Realization good use of symbols, minimum use of symbols or
1 1 Functionality 40 prompts are mislead
of experiment spacing in output. Through spacing in output. Some
existent. No testing h
testing has been completed testing has been completed
completed (0-19)
(35-40) (20-34)
Actively engages and Cooperates with other group
Distracts or discoura
Group cooperates with other member(s) in a reasonable
2. Teamwork 1 3 5 group members from
Performance group member(s) in manner but conduct can be
the experiment (0-1)
effective manner (4-5) improved (2-3)
On Spot Able to make changes (8- Partially able to make
1 1 10 Unable to make chan
3. Conducting Changes 10) changes (5-7)
experiment Answered all questions (8- Unable to answer all
1 1 Viva/Quiz 10 Few incorrect answers (5-7)
10) (0-4)
4. Laboratory 1 3 Code 5 Comments are added and Comments are added and Comments are not ad
safety and commenting does help the reader to does not help the reader to
disciplinary understand the code (4-5) understand the code (2-3)
rules
Excellent use of white
space, creatively organized Includes name, and
Poor use of white sp
work, excellent use of assignment, white space
5. Data (indentation, blank l
1 3 Code Structure 5 variables and constants, makes the program fairly easy
collection code hard to read, di
correct identifiers for to read. Title, organized work,
and messy (0-1)
constants, No line-wrap (4- good use of variables (2-3)
5)
Solution is efficient, easy A logical solution that is easy
6. Data A difficult and ineffi
1 4 Algorithm 20 to understand, and maintain to follow but it is not the most
analysis solution (0-5)
(15-20) efficient (6-14)
Documentation
7. Computer
1 2 & GitHub 5 Timely (4-5) Late (2-3) Not done (0-1)
use
Submissions
  Max Marks (total): 100 Obtained M

You might also like