Professional Documents
Culture Documents
OOPS LAB 2
#include <iostream>
using namespace std;
class Calculator {
public:
float add(float num1, float num2) {
return num1 + num2;
}
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;
}
#include <iostream>
using namespace std;
class BankAccount {
private:
float balance;
int numTransactions;
public:
BankAccount() : balance(0), numTransactions(0) {}
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;
}
#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);
}
cout << "Enter details for Employee " << i + 1 << ":"
<< endl;
cout << "Enter first name: ";
cin.getline(firstNameInput, 100);
return 0;
}