Oop Lab 5
Oop Lab 5
Lab No 5: Constructors
Analysis
of data in
Modern Ethics and Individual
Viva / Quiz / Lab
Tool Safety and
Name Reg. No Lab Report
Usage Teamwork
Performance
Huzyepha 454573
Javaid
Table of Contents
Contents .………………………………………………………... 2
Introduction .………………………………………………………... 3
Task 1 …………………………………………………………. 4-6
Task 2 …………………………………………………………. 7-9
Task 3 …………………………………………………………. 10-13
Task 4 …………………………………………………………. 14-18
Conclusion …………………………………………………………. 18
Introduction
Objectives
Addcookinginstruct(myrecipe, instruction);
}
Displayrecipedetails(myrecipe);
Return 0;
}
Output
Task 2 :
Code
#include<iostream>
#include<string>
Using namespace std;
Struct Ingredient {
String name;
Int quantity;
String unit;
};
Class Recipe{
Private:
String name;
Ingredient ingredients[5];
String cookinginst[5];
Int ingredientcount = 0;
Int instructioncount = 0;
Public:
Recipe(string name) : name(name) {}
Void addingredient(string name, int quantity, string unit, Recipe& recipe) {
Ingredient newingred;
Newingred.name = name;
Newingred.quantity = quantity;
Newingred.unit = unit;
Recipe.ingredients[recipe.ingredientcount] = newingred;
Recipe.ingredientcount++;
}
Void addcookinginstruct(Recipe& recipe, string instruction) {
Recipe.cookinginst[recipe.instructioncount] = instruction;
Recipe.instructioncount++;
}
Void displayrecipedetails(Recipe& recipe) {
Cout ≪ “Recipe Name: “ ≪ name ≪ “\n\n”;
Cout ≪ “Ingredients:\n”;
For (int i = 0; i < recipe.ingredientcount; i++) {
Cout ≪ “- “ ≪ recipe.ingredients[i].name ≪ “: “
≪ recipe.ingredients[i].quantity ≪ “ “
≪ recipe.ingredients[i].unit ≪ “\n”;
}
Cout ≪ “\nCooking Instructions:\n”;
For (int i = 0; i < recipe.instructioncount; i++) {
Cout ≪ i + 1 ≪ “. “ ≪ recipe.cookinginst[i] ≪ “\n”;
}
}
};
Int main() {
Cout ≪ “Welcome to Recipe Management System\n”;
String recipename;
Cout ≪ “Enter the name of the recipe: “;
Getline(cin, recipename);
Recipe myrecipe(recipename);
Int ingredientcount;
Cout ≪ “Enter the number of ingredients: “;
Cin ≫ ingredientcount;
Cin.ignore();
For (int i = 0; i < ingredientcount; i++) {
String ingredientName;
Int quantity;
String unit;
class Account {
private:
float currentBalance;
public:
Account() : currentBalance(0) {
cout << "Account created with default balance of 0." << endl;
}
Account(float initialBalance) {
if (initialBalance >= 0) {
currentBalance = initialBalance;
} else {
currentBalance = 0;
cout << "Initial balance was invalid. Set to 0." << endl;
}
}
int main() {
float initBalance;
string command;
float creditAmount;
float debitAmount;
cout << "Enter your initial balance (or 0 for default): ";
cin >> initBalance;
Account myAccount(initBalance);
cin.ignore();
while (true) {
cout << "Enter 'deposit' to add credit, 'withdraw' to make a transaction,
'check' to check your balance, & 'exit' to exit:\n";
getline(cin, command);
if (command == "deposit") {
cout << "Enter the amount to deposit: ";
cin >> creditAmount;
myAccount.credit(creditAmount);
cin.ignore();
}
else if (command == "withdraw") {
cout << "Enter the amount to withdraw: ";
cin >> debitAmount;
myAccount.debit(debitAmount);
cin.ignore();
}
else if (command == "check") {
myAccount.printBalance();
}
else if (command == "exit") {
cout << "Exiting the system." << endl;
break;
}
else {
cout << "Invalid input! Try again." << endl;
}
}
return 0;
}
Output
Task 4
Code
#include <iostream>
#include <cmath>
#include <string>
private:
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
void reduce() {
numerator /= divisor;
denominator /= divisor;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
public:
if (denom == 0) {
exit(1);
reduce(); // Reduce the fraction immediately upon creation
return numerator;
return denominator;
}
void print() const {
cout << "Fraction: " << numerator << " / " << denominator << endl;
cout << "The number in floating point form: " << decimalNum << endl;
};
int main() {
cout << "Enter the numerator for the first number: ";
cout << "Enter the denominator for the first number: ";
cout << "Enter the numerator for the second number: ";
cout << "Enter the denominator for the second number: ";
sum.print();
difference.print();
product.print();
quotient.print();
numberOne.printFloat();
numberTwo.printFloat();
return 0;
Output
Conclusion
The lab enabled us to the concept of constructors and class to our best understanding.