0% found this document useful (0 votes)
18 views18 pages

Oop Lab 5

The document outlines a lab report for a course on Object Oriented Programming, focusing on the concept of constructors in programming. It includes tasks that involve creating and manipulating classes and objects, specifically for a Recipe Management System and an Account management system. The conclusion emphasizes the understanding gained from the lab regarding constructors and class usage.

Uploaded by

tahamahmood2903
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)
18 views18 pages

Oop Lab 5

The document outlines a lab report for a course on Object Oriented Programming, focusing on the concept of constructors in programming. It includes tasks that involve creating and manipulating classes and objects, specifically for a Recipe Management System and an Account management system. The conclusion emphasizes the understanding gained from the lab regarding constructors and class usage.

Uploaded by

tahamahmood2903
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/ 18

Department of Electrical Engineering

Faculty Member: Miss Ayesha Sarwar

Lab Engineer: Syed Zain-ul-Hassan Dated: 20/10/24

Semester: 3rd Section: BEE-15-D

CS-212 Object Oriented programming

Lab No 5: Constructors

PLO4/ CLO3 PLO10/ PLO8/ PLO9/


CLO4 CLO5 CLO6

Analysis
of data in
Modern Ethics and Individual
Viva / Quiz / Lab
Tool Safety and
Name Reg. No Lab Report
Usage Teamwork
Performance

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks


Hamza Jehangir 481329

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

Constructors are special methods in object-oriented programming that are


automatically called when an instance of a class is created. Their primary
objective is to initialize the object's properties and allocate resources necessary
for the object to function correctly. By providing default values or accepting
parameters, constructors ensure that objects start in a valid state. This concept
helps streamline code, enhance readability, and promote the encapsulation of
data and behavior within classes, ultimately leading to more robust and
maintainable software designs.

Objectives

• Initialization: Set initial values for object properties.

• Resource Allocation: Allocate resources needed for the object's operation.

• Encapsulation: Encapsulate data and behavior within the class.

• Overloading: Support multiple ways to create an object with different


initializations.

• Consistency: Ensure objects are in a valid state upon creation.

• Simplicity: Simplify object creation and improve code readability.

Tools/ Software Used :


Dev C++
Task 1 :
Code
#include <iostream>
#include <string>
Using namespace std;
Struct Ingredient {
String name;
Int quantity;
String unit;
};
Struct Recipe {
String recipename;
Ingredient ingredients[5];
String cookinginst[5];
Int ingredientcount = 0;
Int instructioncount = 0;
};
Recipe createrecipe(string name) {
Recipe newRecipe;
newRecipe.recipename = name;
return newRecipe;
}
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: “ ≪ recipe.recipename ≪ “\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 = createrecipe(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;

Cout ≪ “Enter ingredient name: “;


Getline(cin, ingredientName);

Cout ≪ “Enter quantity: “;


Cin ≫ quantity;

Cout ≪ “Enter unit (e.g., cups, grams): “;


Cin ≫ unit;
Cin.ignore();

Addingredient(ingredientName, quantity, unit, myrecipe);


}
Int instructioncount;
Cout ≪ “Enter the number of instructions: “;
Cin ≫ instructioncount;
Cin.ignore();
For (int i = 0; i < instructioncount; i++) {
String instruction;
Cout ≪ “Enter instruction “ ≪ (i + 1) ≪ “: “;
Getline(cin, instruction);

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;

Cout ≪ “Enter ingredient name: “;


Getline(cin, ingredientName);

Cout ≪ “Enter quantity: “;


Cin ≫ quantity;

Cout ≪ “Enter unit (e.g., cups, grams): “;


Cin ≫ unit;
Cin.ignore();

Myrecipe.addingredient(ingredientName, quantity, unit, myrecipe);


}
Int instructioncount;
Cout ≪ “Enter the number of instructions: “;
Cin ≫ instructioncount;
Cin.ignore();
For (int i = 0; i < instructioncount; i++) {
String instruction;
Cout ≪ “Enter instruction “ ≪ (i + 1) ≪ “: “;
Getline(cin, instruction);
Myrecipe.addcookinginstruct(myrecipe, instruction);
}
Myrecipe.displayrecipedetails(myrecipe);
Return 0;
}
Output
Task 3
Code
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

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

void credit(float amount) {


if (amount > 0) {
currentBalance += amount;
cout << "Amount credited: " << amount << endl;
} else {
cout << "Amount must be positive." << endl;
}
}

void debit(float amount) {


if (amount > currentBalance) {
cout << "Transaction failed: Insufficient balance." << endl;
} else if (amount > 0) {
currentBalance -= amount;
cout << "Amount debited: " << amount << endl;
} else {
cout << "Amount must be positive." << endl;
}
}

float getBalance() const {


return currentBalance;
}

void printBalance() const {


cout << "Current balance: " << fixed << setprecision(2) << getBalance() <<
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>

using namespace std;


class Rational {

private:

int numerator, denominator;

int commonDenom(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

void reduce() {

int divisor = commonDenom(abs(numerator), abs(denominator));

numerator /= divisor;

denominator /= divisor;

if (denominator < 0) {

numerator = -numerator;

denominator = -denominator;

public:

Rational(int num = 0, int denom = 1) : numerator(num), denominator(denom) {

if (denom == 0) {

cout << "Error: Denominator cannot be zero." << endl;

exit(1);
reduce(); // Reduce the fraction immediately upon creation

Rational operator+(const Rational& other) const {

int num = (numerator * other.denominator) + (other.numerator * denominator);

int denom = denominator * other.denominator;

return Rational(num, denom);

Rational operator-(const Rational& other) const {

int num = (numerator * other.denominator) - (other.numerator * denominator);

int denom = denominator * other.denominator;

return Rational(num, denom);

Rational operator*(const Rational& other) const {

return Rational(numerator * other.numerator, denominator * other.denominator);

Rational operator/(const Rational& other) const {

return Rational(numerator * other.denominator, denominator * other.numerator);

int getNumerator() const {

return numerator;

int getDenominator() const {

return denominator;

}
void print() const {

cout << "Fraction: " << numerator << " / " << denominator << endl;

void printFloat() const {

float decimalNum = static_cast<float>(numerator) / denominator;

cout << "The number in floating point form: " << decimalNum << endl;

};

int main() {

int num1, denom1, num2, denom2;

cout << "Enter the numerator for the first number: ";

cin >> num1;

cout << "Enter the denominator for the first number: ";

cin >> denom1;

cout << "Enter the numerator for the second number: ";

cin >> num2;

cout << "Enter the denominator for the second number: ";

cin >> denom2;

Rational numberOne(num1, denom1);

Rational numberTwo(num2, denom2);

Rational sum = numberOne + numberTwo;

Rational difference = numberOne - numberTwo;

Rational product = numberOne * numberTwo;

Rational quotient = numberOne / numberTwo;

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.

You might also like