Assignment#4 (1)
Assignment#4 (1)
CLO 4
MARKS 40
DUE DATE : 12-DEC-23
Q1: (Account class)Create an Account class that a bank might use to represent customers’
bank accounts. Include a data member of type int to represent the account balance. Provide a
constructor that receives an initial balance and uses it to initialize the data member. The
constructor should validate the initial balance to ensure that it’s greater than or equal to 0.
If not, set the balance to 0 and display an error message indicating that the initial balance
was invalid. Provide three member functions. Member function creditshould add an amount to
the current balance. Member function debit should withdraw money from the Account and
ensure that the debit amount does not exceed the Account’s balance. If it does, the balance
should be left unchanged, and the function should print a message indicating "Debit
amount exceeded account balance."
Memberfunctionget_Balanceshouldreturnthecurrentbalance.Createaprogramthat creates two
Account objects and tests the member functions of classAccount.
Q2.Create an inheritance hierarchy that a bank might use to represent customers’ bank
accounts.All customers at this bank can deposit (i.e., credit) money intotheir accounts and
withdraw (i.e., debit) money from their accounts. More specific types of accounts also
exist.•Savings accounts, for instance, earn interest on the money they hold.•Checking accounts,
on the other hand, charge a fee per transaction (i.e., credit or debit).Create an inheritance
hierarchy containing:➢base class Account and➢derived classes SavingsAccount
andCheckingAccount that inherit from class Account. Base class Accountshould include one
data member of type double to represent the account balance. The class should provide a
constructor that receives an initial balance and uses it to initialize the data member. The
constructor should validate the initial balance to ensure that it’s greater than or equal to 0.0. If
not, the balanceshould be set to 0.0 and the constructor should display an error message,
indicating that the initial balance was invalid. The class should provide three member functions.
Member function credit should add an amount to the current balance. Member function debit
should withdraw money from the Account and ensure that the debit amount does not exceed
the Account’s balance. If it does, the balance should be left unchanged,and the function should
print the message "Debit amount exceeded account balance." Member function getBalance
should return the current balance.Derived class SavingsAccountshould inherit the functionality
of an Account, but also include a data member of type double indicating the interest rate
(percentage) assigned to the Account.SavingsAccount’s constructor should receive the initial
balance, as well as an initial value for the SavingsAccount’s interest rate. SavingsAccount should
provide a public member function calculateInterest that returns a double indicating the amount
of interest earned by an account. Member function calculateInterest should determine this
amount by multiplying the interest rate by the account balance.
[Note: SavingsAccount should inherit member functions credit anddebit as is without redefining
them.]Derived class CheckingAccountshould inherit from base class Account and include an
additional data member of type double that represents the fee charged per transaction.
CheckingAccount’s constructor should receive the initial balance, as well as a parameter
indicating a feeamount. Class CheckingAccount should redefine member functions credit and
debit so that theysubtract the fee from the account balance whenever either transaction is
performed successfully.CheckingAccount’s versions of these functions should invoke the base-
class Account version to perform the updates to an account balance. CheckingAccount’s debit
function should charge a feeonly if money is actually withdrawn (i.e., the debit amount does
not exceed the account balance).[Hint:Define Account’s debit function so that it returns a bool
indicating whether money waswithdrawn. Then use the return value to determine whether a
fee should be charged.]After defining the classes in this hierarchy, write a program that creates
objects of each classand tests their member functions. Add interest to the SavingsAccount
object by first invoking itscalculateInterest function, then passing the returned interest amount
to the object’s creditfunction.
Q3.{Abstractclass/Polymorphism)Using an abstract class with only pure virtual functions,
you canspecify similar behaviors for possibly disparate classes. Governments
andcompanies worldwide are becoming increasingly concerned with carbon footprints
(annual releasesof carbon dioxide into the atmosphere) from buildings burning various types of
fuels for heat, vehicles burning fuels for power, and the like. Many scientists blame these
greenhouse gases for the phenomenon called global warming. Create three small classes
unrelated by inheritance—classesBuilding, Car and Bicycle. Give each class some unique
appropriate attributes and behaviors thatit does not have in common with other classes.
Write an abstract class CarbonFootprint with onlya pure virtual
getCarbonFootprint method. Have each of your classes inherit from that abstract
classand implement the getCarbonFootprint method to calculate an appropriate carbon
footprint forthat class (check out a few websites that explain how to calculate carbon
footprints). Write an application that creates objects of each of the three classes, places
pointers to those objects in a vectorof CarbonFootprint pointers, then iterates through the
vector, polymorphically invoking each object’s getCarbonFootprint method. For each object,
print some identifying information and the object’s carbon footprint.
Q4.Develop class Polynomial. The internal representation of a Polynomial is an array of
terms. Each term contains a coefficient and an exponent. The term 2x4 has the coefficient
2 and the exponent 4. Develop a complete class containing proper constructor and
destructor functions as well as set and get functions. The class should also provide the
following overloaded operator capabilities:•Overload the addition operator (+) to add two
Polynomials.•Overload the subtraction operator (-) to subtract two Polynomials.•Overload the
assignment operator to assign one Polynomial to another.•Overload the multiplication
operator (*) to multiply two Polynomials.•Overload the addition assignment operator (+=),
subtraction assignment operator (-=), and multiplication assignment operator (*=).