You are on page 1of 18

ROLL NO :-AI-039

OOPS LAB 2

1) Create a class CALCULATOR


which add, subtract, divide and
multiply numbers based on user
input.

#include <iostream>
using namespace std;

class Calculator {
public:
float add(float num1, float num2) {
return num1 + num2;
}

float subtract(float num1, float num2) {


return num1 - num2;
}

float multiply(float num1, float num2) {


return num1 * num2;
}

float divide(float num1, float num2) {


if (num2 == 0) {
cout << "Cannot divide by zero!" << endl;
return 0; // Returning 0 as an error flag
}
return num1 / num2;
}
};
int main() {
Calculator calculator;

float num1, num2;


cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;

cout << "1. Addition" << endl;


cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;

char choice;
cout << "Enter your choice (1/2/3/4): ";
cin >> choice;
switch (choice) {
case '1':
cout << "Result: " << calculator.add(num1, num2)
<< endl;
break;
case '2':
cout << "Result: " << calculator.subtract(num1,
num2) << endl;
break;
case '3':
cout << "Result: " << calculator.multiply(num1,
num2) << endl;
break;
case '4':
cout << "Result: " << calculator.divide(num1,
num2) << endl;
break;
default:
cout << "Invalid input" << endl;
}
return 0;
}

2) You are a programmer for the ABC


Bank assigned to develop a class
that models the basic workings of a
bank account. The class should
perform the following tasks:
a) Save the account balance.
b) Save the number of transactions
performed on the account.
c) Allow deposits to be made to
the account.
d) Allow with drawls to be taken
from the account.
e) Calculate interest for the
period.
f) Report the current account
balance at any time.
g) Report the current number of
transactions at any time.

#include <iostream>
using namespace std;

class BankAccount {
private:
float balance;
int numTransactions;

public:
BankAccount() : balance(0), numTransactions(0) {}

// Function to deposit money into the account


void deposit(float amount) {
balance += amount;
numTransactions++;
cout << "Deposited $" << amount << " into the
account." << endl;
}

// Function to withdraw money from the account


void withdraw(float amount) {
if (balance >= amount) {
balance -= amount;
numTransactions++;
cout << "Withdrawn $" << amount << " from the
account." << endl;
} else {
cout << "Insufficient funds. Withdrawal failed." <<
endl;
}
}
// Function to calculate interest for a given period
float calculateInterest(float rate, int time) {
return (balance * rate * time) / 100.0;
}

// Function to report the current account balance


float getBalance() {
return balance;
}

// Function to report the current number of


transactions
int getNumTransactions() {
return numTransactions;
}
};

int main() {
BankAccount account;
char choice;

do {
cout << "Choose an option:" << endl;
cout << "1. Deposit" << endl;
cout << "2. Withdraw" << endl;
cout << "3. Show Transactions and Balance" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case '1': {
float depositAmount;
cout << "Enter the amount you want to deposit:
$";
cin >> depositAmount;
account.deposit(depositAmount);
break;
}
case '2': {
float withdrawAmount;
cout << "Enter the amount you want to
withdraw: $";
cin >> withdrawAmount;
account.withdraw(withdrawAmount);
break;
}
case '3':
cout << "Number of Transactions: " <<
account.getNumTransactions() << endl;
cout << "Current Balance: $" <<
account.getBalance() << endl;
break;
case '4':
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." <<
endl;
}
} while (choice != '4');

return 0;
}

3) Create a class called Employee that includes


three pieces of information as data members
—a first name (type char* (DMA)), a last
name (type string) and a monthly salary (type
int). Your class should have a setter function
that initializes the three data members.
Provide a getter function for each data
member. If the monthly salary is not positive,
set it to 0. Write a test program that
demonstrates class Employee’s capabilities.
Create two Employee objects and display
each object’s yearly salary. Then give each
Employee a 10 percent raise and display each
Employee’s yearly salary again. Identify and
add any other related functions to achieve
the said goal.

#include <iostream>
#include <string>
#include <cstring> // For strlen and strcpy
using namespace std;

class Employee {
private:
char* firstName; // Dynamic memory allocation for
first name
string lastName;
int monthlySalary;

public:
// Constructor
Employee(char* fName, string lName, int salary) {
firstName = new char[strlen(fName) + 1]; //
Allocating memory for first name
std::strcpy(firstName, fName);
lastName = lName;
setMonthlySalary(salary);
}

// Destructor to free allocated memory


~Employee() {
delete[] firstName;
}

// Setter function for first name


void setFirstName(char* fName) {
delete[] firstName; // Free previously allocated
memory
firstName = new char[strlen(fName) + 1]; // Allocate
new memory
std::strcpy(firstName, fName);
}

// Setter function for last name


void setLastName(string lName) {
lastName = lName;
}

// Setter function for monthly salary


void setMonthlySalary(int salary) {
monthlySalary = (salary > 0) ? salary : 0;
}

// Getter function for first name


char* getFirstName() const {
return firstName;
}

// Getter function for last name


string getLastName() const {
return lastName;
}

// Getter function for monthly salary


int getMonthlySalary() const {
return monthlySalary;
}

// Function to calculate yearly salary


int getYearlySalary() const {
return monthlySalary * 12;
}

// Function to give a raise


void giveRaise(double percentage) {
monthlySalary = static_cast<int>(monthlySalary * (1
+ percentage / 100));
}
};
int main() {
// Get employee information from the user for three
employees
for (int i = 0; i < 3; ++i) {
char firstNameInput[100];
string lastNameInput;
int salaryInput;

cout << "Enter details for Employee " << i + 1 << ":"
<< endl;
cout << "Enter first name: ";
cin.getline(firstNameInput, 100);

cout << "Enter last name: ";


getline(cin, lastNameInput);

cout << "Enter monthly salary: ";


cin >> salaryInput;
cin.ignore(); // Ignore newline character

// Creating Employee object


Employee emp(firstNameInput, lastNameInput,
salaryInput);

// Displaying yearly salary for the employee


cout << "Yearly salary for " << emp.getFirstName()
<< " " << emp.getLastName() << ": $" <<
emp.getYearlySalary() << endl;

// Giving a 10% raise


emp.giveRaise(10);

// Displaying yearly salary after raise


cout << "Yearly salary for " << emp.getFirstName()
<< " " << emp.getLastName() << " after raise: $" <<
emp.getYearlySalary() << endl;

cout << endl;


}

return 0;
}

You might also like