0% found this document useful (0 votes)
18 views6 pages

Oopc Assignment 3

Uploaded by

FNStar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Oopc Assignment 3

Uploaded by

FNStar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment – 3

1. Objective: Implement single inheritance between two classes.


Code:
#include <iostream>
#include <string>
using namespace std;

class Person
{
protected:
string name;
int age;

public:
Person(string n, int a) : name(n), age(a) {}

void display() const


{
cout << "Name: " << name << ", Age: " << age << endl;
}
};

class Student : public Person


{
string grade;

public:
Student(string n, int a, string g) : Person(n, a), grade(g) {}

void display() const


{
Person::display();
cout << "Grade: " << grade << endl;
}
};

int main()
{
Student student("Alice", 20, "A");
student.display();
return 0;
}
Output:
Name: Alice, Age: 20
Grade: A

2. Objective: Demonstrate single inheritance with functionality extension.


Code:
#include <iostream>
using namespace std;

class BankAccount
{
protected:
int accountNumber;
double balance;

public:
BankAccount(int accNum, double bal = 0.0) : accountNumber(accNum), balance(bal) {}

void deposit(double amount)


{
balance += amount;
cout << "Deposited: $" << amount << ". New Balance: $" << balance << endl;
}

void withdraw(double amount)


{
if (amount <= balance)
{
balance -= amount;
cout << "Withdrawn: $" << amount << ". New Balance: $" << balance << endl;
}
else
{
cout << "Insufficient funds." << endl;
}
}
};

class SavingsAccount : public BankAccount


{
double interestRate;

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

3. Objective: Implement single inheritance with an area calculation.


Code:
#include <iostream>
#include <cmath>
using namespace std;

class Shape
{
protected:
string color;

public:
Shape(string c) : color(c) {}
};

class Circle : public Shape


{
double radius;

public:
Circle(string c, double r) : Shape(c), radius(r) {}

void calculateArea() const


{
double area = M_PI * radius * radius;
cout << "Color: " << color << ", Radius: " << radius << ", Area: " << area << endl;
}
};

int main()
{
Circle circle("Red", 5.0);
circle.calculateArea();
return 0;
}
Output:
Color: Red, Radius: 5, Area: 78.5398

4. Objective: Use single inheritance with a balance update.


Code:
#include <iostream>
using namespace std;

class Account
{
protected:
double balance;
public:
Account(double bal = 0.0) : balance(bal) {}

void deposit(double amount)


{
balance += amount;
cout << "Deposited: $" << amount << ". New Balance: $" << balance << endl;
}

virtual void withdraw(double amount)


{
if (amount <= balance)
{
balance -= amount;
cout << "Withdrawn: $" << amount << ". New Balance: $" << balance << endl;
}
else
{
cout << "Insufficient funds." << endl;
}
}
};

class CheckingAccount : public Account


{
double transactionFee;

public:
CheckingAccount(double bal = 0.0, double fee = 1.0) : Account(bal), transactionFee(fee) {}

void withdraw(double amount) override


{
if (amount + transactionFee <= balance)
{
balance -= (amount + transactionFee);
cout << "Withdrawn: $" << amount << " with fee of $" << transactionFee << ". New Balance: $" <<
balance << endl;
}
else
{
cout << "Insufficient funds." << endl;
}
}
};

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) {}

void deposit(double amount)


{
balance += amount;
cout << "Deposited: $" << amount << ". New Balance: $" << balance << endl;
}

double getBalance() const


{
return balance;
}
};

class SavingsAccount : public BankAccount


{
double interestRate;

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

You might also like