Oops Solution
Oops Solution
Assume that a bank maintains two kinds of accounts for customers, one called a savings
account and the other as a current account. The savings account provides compound
interest and withdrawal facilities but no cheque book facility, The current account
provides cheque book facility but no interest . Current account holders should also
maintain a minimum balance and if the balance and if the balance falls below this level ,
a service charge is imposed.
#include <iostream>
#include <cmath>
using namespace std;
class Account {
protected:
string accountHolder;
double balance;
public:
Account(string holder, double initialBalance) {
accountHolder = holder;
balance = initialBalance;
}
public:
SavingsAccount(string holder, double initialBalance, double rate)
: Account(holder, initialBalance), interestRate(rate) {}
public:
CurrentAccount(string holder, double initialBalance, double minBalance, double charge)
: Account(holder, initialBalance), minimumBalance(minBalance), serviceCharge(charge) {}
void checkBalance() {
if (balance < minimumBalance) {
balance -= serviceCharge;
cout << "Balance fell below minimum. Service charge of " << serviceCharge
<< " imposed. New Balance: " << balance << endl;
}
}
int main() {
SavingsAccount savings("John Doe", 5000.0, 5.0);
CurrentAccount current("Jane Doe", 2000.0, 1000.0, 50.0);
return 0;
}
2.Create a class account that stores customer name, account number and type of account .
From this derive the classes cur_act and sav_act to make them more specific to their
requirements . Include necessary member functions in order to achieve the following tasks:
1.Accept deposit from a customer and update the balance
2.Display the balance.
3.Permit withdrawal and update the balance.
4.Check for the minimum balance, impose penalty, necessary, and update the balance.
Do not use any constructor. Use member functions to initialize the class members.
#include <iostream>
using namespace std;
class Account {
protected:
string customerName;
int accountNumber;
string accountType;
double balance;
public:
void initializeAccount(string name, int accNum, string accType, double initialBalance) {
customerName = name;
accountNumber = accNum;
accountType = accType;
balance = initialBalance;
}
void displayBalance() {
cout << "Account Holder: " << customerName << ", Balance: " << balance << endl;
}
public:
void checkMinimumBalance() {
if (balance < minimumBalance) {
balance -= penalty;
cout << "Balance fell below minimum. Penalty of " << penalty
<< " imposed. New Balance: " << balance << endl;
}
}
};
public:
void setInterestRate(double rate) {
interestRate = rate;
}
int main() {
CurAct currentAccount;
SavAct savingsAccount;
return 0;
}
3. A bookshop maintains the inventory of books that are being sold at the shop. The list includes
details such as author , title,price,publisher and stock positions Whenever a customer wants a
book the sales person inputs the title and author and the system searches the list and displays.
If it is , then the system displays whether it is available or not. If it is not an appropriate message
is displayed . If it is then the system displays the book details and requests for the number of
copies required. If the requested copies are available, the total cost of the requested copies are
available , the total cost of the requested copies is displayed; otherwise the message”Required
copies not in stock” is displayed.
Design a system using a class called books with suitable member functions and constructors .
Use new operators in constructors to allocate memory space required.
#include <iostream>
#include <cstring>
using namespace std;
class Book {
char *title;
char *author;
char *publisher;
double price;
int stock;
public:
Book(const char *t, const char *a, const char *p, double pr, int s) {
title = new char[strlen(t) + 1];
author = new char[strlen(a) + 1];
publisher = new char[strlen(p) + 1];
strcpy(title, t);
strcpy(author, a);
strcpy(publisher, p);
price = pr;
stock = s;
}
~Book() {
delete[] title;
delete[] author;
delete[] publisher;
}
void displayDetails() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Publisher: " << publisher << endl;
cout << "Price: " << price << endl;
cout << "Stock: " << stock << endl;
}
int main() {
int n;
cout << "Enter the number of books: ";
cin >> n;
Book *inventory[n];
cout << "\nEnter details for Book " << i + 1 << ":\n";
cout << "Title: ";
cin.ignore();
cin.getline(title, 50);
cout << "Author: ";
cin.getline(author, 50);
cout << "Publisher: ";
cin.getline(publisher, 50);
cout << "Price: ";
cin >> price;
cout << "Stock: ";
cin >> stock;
if (!found) {
cout << "\nBook not found in the inventory." << endl;
}
return 0;
}
4.Write a Program in C++ to Design a class called Matrix that will have the following private
variables:
mat[][] : a 2d matrix of size 3x3
Declare two objects of the class and initialize the two matrices using a parameterized
constructor. Use operator overloading to evaluate and store the result of the addition of
the two matrices in the member variable of another object.
#include <iostream>
using namespace std;
class Matrix {
private:
int mat[3][3];
public:
Matrix(int arr[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
mat[i][j] = arr[i][j];
}
}
}
Matrix() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
mat[i][j] = 0;
}
}
}
void display() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
int arr1[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int arr2[3][3] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
Matrix matrix1(arr1);
Matrix matrix2(arr2);
Matrix result;
return 0;
}
5.Write a Program in C++ to design a class Thermometer that will the following private
data members:
float temp : to store the magnitude of temp
char scale : to store the scale in which it is measured
float result : to store the converted value after conversion
Initialize data to the object using constructor overloading.
Implement the required member functions to convert to the other scale , store the necessary
result and display.
Use switch case or ternary operator wherever necessary.
#include <iostream>
using namespace std;
class Thermometer {
private:
float temp;
char scale;
float result;
public:
Thermometer() {
temp = 0;
scale = 'C';
result = 0;
}
Thermometer(float t, char s) {
temp = t;
scale = s;
result = 0;
}
void convert() {
if (scale == 'C' || scale == 'c') {
result = (temp * 9 / 5) + 32; // Convert Celsius to Fahrenheit
cout << "Converted Temperature: " << result << " Fahrenheit" << endl;
}
else if (scale == 'F' || scale == 'f') {
result = (temp - 32) * 5 / 9; // Convert Fahrenheit to Celsius
cout << "Converted Temperature: " << result << " Celsius" << endl;
}
else if (scale == 'K' || scale == 'k') {
result = temp - 273.15; // Convert Kelvin to Celsius
cout << "Converted Temperature: " << result << " Celsius" << endl;
}
else {
cout << "Invalid scale entered!" << endl;
}
}
void display() {
cout << "Temperature: " << temp << " " << scale << endl;
}
};
int main() {
float temp;
char scale;
thermometer.display();
thermometer.convert();
return 0;
}