Oopc Assignment 3
Oopc Assignment 3
class Person
{
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
public:
Student(string n, int a, string g) : Person(n, a), grade(g) {}
int main()
{
Student student("Alice", 20, "A");
student.display();
return 0;
}
Output:
Name: Alice, Age: 20
Grade: A
class BankAccount
{
protected:
int accountNumber;
double balance;
public:
BankAccount(int accNum, double bal = 0.0) : accountNumber(accNum), balance(bal) {}
public:
SavingsAccount(int accNum, double bal = 0.0, double rate = 0.05) : BankAccount(accNum, bal),
interestRate(rate) {}
void calculateInterest()
{
double interest = balance * interestRate;
balance += interest;
cout << "Interest added: $" << interest << ". New Balance: $" << balance << endl;
}
};
int main()
{
SavingsAccount savings(12345, 1000);
savings.deposit(500);
savings.withdraw(200);
savings.calculateInterest();
return 0;
}
Output:
Deposited: $500. New Balance: $1500
Withdrawn: $200. New Balance: $1300
Interest added: $65. New Balance: $1365
class Shape
{
protected:
string color;
public:
Shape(string c) : color(c) {}
};
public:
Circle(string c, double r) : Shape(c), radius(r) {}
int main()
{
Circle circle("Red", 5.0);
circle.calculateArea();
return 0;
}
Output:
Color: Red, Radius: 5, Area: 78.5398
class Account
{
protected:
double balance;
public:
Account(double bal = 0.0) : balance(bal) {}
public:
CheckingAccount(double bal = 0.0, double fee = 1.0) : Account(bal), transactionFee(fee) {}
int main()
{
CheckingAccount checking(1000, 1);
checking.deposit(500);
checking.withdraw(200);
checking.withdraw(1500);
return 0;
}
Output:
Deposited: $500. New Balance: $1500
Withdrawn: $200 with fee of $1. New Balance: $1299
Insufficient funds.
5. Objective: Access private members of the base class through getters and setters.
Code:
#include <iostream>
using namespace std;
class BankAccount
{
private:
double balance;
public:
BankAccount(double bal = 0.0) : balance(bal) {}
public:
SavingsAccount(double bal = 0.0, double rate = 0.05) : BankAccount(bal), interestRate(rate) {}
void applyInterest()
{
double interest = getBalance() * interestRate;
deposit(interest);
cout << "Interest applied at rate " << interestRate * 100 << "%." << endl;
}
};
int main()
{
SavingsAccount savings(1000);
savings.deposit(500);
savings.applyInterest();
cout << "Current Balance: $" << savings.getBalance() << endl;
return 0;
}
Output:
Deposited: $500. New Balance: $1500
Deposited: $75. New Balance: $1575
Interest applied at rate 5%.
Current Balance: $1575