Bahria University, Lahore Campus
Department of Computer Science
Mid Exam
(Fall 2024)
Course: Object Oriented Programming- Lab Date: _______________
Course Code: CSL-210 Max Marks: 30
Faculty’s Name: Rizwan Khalid
Name: _Rai Awais____________________ Enroll No: __03-134241-049_________________
Class: ______________
Task 1 Marks 20
A large e-commerce platform needs a discount calculation system that can handle different types
of discounts dynamically based on the customer type. The system should follow these rules:
• There is a base class Customer with a virtual function calculateDiscount(double
totalAmount) that calculates the discount for a given purchase amount.
• Three specific customer types exist:
• RegularCustomer: Gets a 5% discount on the total amount.
• PremiumCustomer: Gets a 10% discount on the total amount if it exceeds $500.
• VIPCustomer: Always gets a 20% discount regardless of the total amount.
Write code to implement the scenario using dynamic polymorphism and answer the following:
• Implement the class hierarchy to support this scenario, ensuring that each customer type
overrides the calculateDiscount() method appropriately.
• Write a test function to:
• Create a list of mixed customer types.
• Calculate and print the discount for each customer on a purchase amount of $600.
• Explain how adding a new customer type (e.g., SeasonalCustomer) can be easily
integrated into the system without modifying existing code.
#include <iostream>
#include <vector>
#include <memory>
class Customer {
public:
virtual double calculateDiscount(double totalAmount) const = 0;
virtual ~Customer() {}
};
class RegularCustomer : public Customer {
public:
double calculateDiscount(double totalAmount) const override {
return totalAmount * 0.05;
};
class PremiumCustomer : public Customer {
public:
double calculateDiscount(double totalAmount) const override {
return totalAmount > 500 ? totalAmount * 0.10 : 0;
};
class VIPCustomer : public Customer {
public:
double calculateDiscount(double totalAmount) const override {
return totalAmount * 0.20;
};
void testDiscountSystem() {
std::vector<std::shared_ptr<Customer>> customers = {
std::make_shared<RegularCustomer>(),
std::make_shared<PremiumCustomer>(),
std::make_shared<VIPCustomer>()
};
double purchaseAmount = 600;
for (const auto& customer : customers) {
std::cout << "Discount: " << customer->calculateDiscount(purchaseAmount) <<
std::endl;
int main() {
testDiscountSystem();
return 0;
•
Task 2 Marks 10
In a banking system, you need to model a network of bank accounts and a central bank for
internal operations. Each account should store private data about the owner's balance and account
number. The BankAccount class has no access to the internal data of other accounts. However,
the CentralBank class needs special access to facilitate several actions that require modifying
private data of individual accounts.
Implement the following classes and their interactions:
• BankAccount Class: This class should represent individual accounts with private data
members:
• accountNumber: A unique identifier for each account.
• balance: The current balance in the account.
Each account should have methods for:
• deposit() to add funds.
• withdraw() to remove funds if the balance allows.
This class should not directly access the data of other BankAccount instances.
• CentralBank Class: This class should act as an intermediary with friend access to
BankAccount. It should be able to:
• Perform internal bank operations that require access to account balances, such as
processing inter-account transfers.
• Perform interest calculations by adjusting balances for multiple accounts at once.
• Audit accounts to print account balances for reporting.
Requirements:
• Use friend class declarations to allow CentralBank access to private members
of BankAccount.
• Implement a transferFunds() function in CentralBank that can transfer an
amount from one BankAccount to another. This function should not be a member
of BankAccount, yet it should be able to modify the balance of both accounts.
• Implement an applyInterest() function in CentralBank that adds interest to all
accounts in the bank at a specified rate.
• Additional Requirements:
• Avoid giving any public methods in BankAccount direct access to data from other
BankAccount instances.
• Demonstrate proper use of the friend class concept without allowing public access
to private data.
#include <iostream>
#include <vector>
class BankAccount {
private:
int accountNumber;
double balance;
public:
BankAccount(int accNum, double initialBalance) : accountNumber(accNum),
balance(initialBalance) {}
void deposit(double amount) {
if (amount > 0) balance += amount;
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) balance -= amount;
friend class CentralBank;
};
class CentralBank {
private:
std::vector<BankAccount*> accounts;
public:
void addAccount(BankAccount* account) {
accounts.push_back(account);
void transferFunds(BankAccount& from, BankAccount& to, double amount) {
if (amount > 0 && from.balance >= amount) {
from.balance -= amount;
to.balance += amount;
void applyInterest(double rate) {
for (auto account : accounts) {
if (rate > 0) account->balance += account->balance * rate;
}
void auditAccounts() {
for (auto account : accounts) {
std::cout << "Account " << account->accountNumber << ": Balance = " <<
account->balance << std::endl;
};
int main() {
BankAccount acc1(101, 1000.0);
BankAccount acc2(102, 2000.0);
CentralBank bank;
bank.addAccount(&acc1);
bank.addAccount(&acc2);
bank.transferFunds(acc1, acc2, 500.0);
bank.applyInterest(0.05);
bank.auditAccounts();
return 0;
Lab Grading Sheet :
Max Obtained
Task Mark Comments(if any)
Marks
s
1. 20
2. 10
Total Signature
Note : Attempt all tasks and get them checked by your Lab Instructor.