0% found this document useful (0 votes)
17 views7 pages

4737 - Babar - Lab-Task-01

The document outlines a lab task submission that includes three C++ programming exercises. The first task involves creating a program to accept and display five integers using pointers, the second modifies this to display the integers in reverse order, and the third task involves creating an Inventory class with methods to add and view items, along with an Item class. Each solution is provided with code snippets demonstrating the implementation of the tasks.

Uploaded by

xowej23668
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)
17 views7 pages

4737 - Babar - Lab-Task-01

The document outlines a lab task submission that includes three C++ programming exercises. The first task involves creating a program to accept and display five integers using pointers, the second modifies this to display the integers in reverse order, and the third task involves creating an Inventory class with methods to add and view items, along with an Item class. Each solution is provided with code snippets demonstrating the implementation of the tasks.

Uploaded by

xowej23668
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/ 7

Lab Task 01

Submitted to: Shakeel Ahmad


Name: KHAWAJA BABAR NASEER
Reg.No: 4737-FOC-BSSE-F23-A
Date: 11- 2- 2025

1. Write a C++ program to accept five integer values from keyword.


The five values will be stored in an array using a pointer. Then
print the elements of the array on the screen.

Solution
#include <iostream>
using namespace std;

int main()
{

int arr[5];

int* ptr = arr;

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

cout << "Enter value at " << i << " index = ";

cin >> *(ptr + i);


}

cout << "Array Values" << endl;

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

cout << *(ptr + i) << " ";


}

return 0;
}
2. Modify the solution of task 1 to print the elements of the array in
reverse order using a pointer.
Solution
#include <iostream>

using namespace std;

int main()
{

int arr[5];

int *ptr = arr;

for (int i = 0; i < 5; i++)


{

cout << "Enter value at " << i << " index = ";

cin >> *(ptr + i);


}

cout << "Reversed Values" << endl;

for (int i = 4; i >= 0; i--)


{

cout << *(ptr + i) << " ";


}

return 0; }
3. Create a class called Inventory having relevant data members and
methods such as addToInventory () and viewInventory (). Then create a
class called Item having relevant data members as well as functions. An
Inventory has many items. By making use of class array/s and the
concept of has a relationship, write a program in C++.
Note: Constructors and getter/setters are mandatory.

Solution
#include <iostream>
#include <string>

using namespace std;

class Item
{
private: int
itemID; string
itemName; double
price; int
quantity;

public:
Item() : itemID(0), itemName(""), price(0.0), quantity(0) {}

Item(int id, string name, double p, int qty) : itemID(id), itemName(name),


price(p), quantity(qty) {}

void displayItem() const


{ cout << "Item ID: " <<
itemID << ", Name: " <<
itemName
<< ", Price: $" << price
<< ", Quantity: " << quantity << endl;
}
};
class Inventory
{
private: static const int
MAX_ITEMS = 100; Item
items[MAX_ITEMS]; int
itemCount;

public:
Inventory() : itemCount(0) {}

void addToInventory(int id, string name, double price, int qty)


{ if (itemCount <
MAX_ITEMS)
{ items[itemCount] = Item(id, name,
price, qty); itemCount++; cout <<
"Item added successfully!" << endl;
} else { cout
<< "Inventory is full!" << endl;
}
}

void viewInventory() const


{ if (itemCount
== 0)
{ cout << "Inventory is
empty!" << endl; return;
}

cout << "\nInventory List:" << endl;


for (int i = 0; i < itemCount; i++)
{
items[i].displayItem();
}
}
};

int main()
{
Inventory inventory;

inventory.addToInventory(101, "Laptop", 750.99, 5);


inventory.addToInventory(102, "Mouse", 20.49, 10);
inventory.addToInventory(103, "Keyboard", 35.99, 7);

inventory.viewInventory();

return 0;
}

You might also like