Vous êtes sur la page 1sur 4

LAB# 06: Inheritance

Description:

Inheritance enables reusability and helps in enhancing the functionality of any class. Inheritance is one of
the major pillars of Object Oriented programming and aids in code maintainability and avoids
redundancy.

1. Inheritance

Inheritance is a form of software reuse in which you create a class that absorbs an existing class’s data
and behavior and enhances them with new capabilities.Ininheritance, a class derives the behavior and
structure of another (existing) class.

 Advantages
o Saves time
o Reuse of proven, debugged, high quality software

Figure 1: Derived & Base class relationship

Data members in the base class are part of the derived class. Behaviors defined in the base class are part
of the derived class. Note that private aspects of the base class are part of the child, but are not (directly)
accessible within the derived class.

classDerivedClass : kind BaseClass

Where kind is one of public, private or protected.


2. Order of Constructors & Destructors

When a program creates a derived-class object, the derived-class constructor immediately calls the base-
class constructor; the base-class constructor’s body executes, then the derived-class’s member initializers
execute and finally the derived-class constructor’s body executes. This process cascades up the hierarchy
if it contains more than two levels.

When a derived-class object is destroyed, the program calls that object’s destructor. This begins a chain
(cascade) of destructor calls in which the derived-class destructor and the base destructors execute in
reverse of the order in which the constructors executed.

LAB TASK

i. Create an inheritance hierarchy that a bank might use to represent customers' bank accounts.
All customers each having an account no. at this bank can deposit (i.e., credit) money into
their accounts and withdraw (i.e., debit) money from their accounts. More specific types of
accounts also exist. CreditCardAccount, for instance, provide the user the facility to make
money transactions using ATM 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
CreditCardAccount and CheckingAccount that inherit from class Account. Base class
Account should include one data member of type double to represent the account balance.
Customer’s name and account no.
The account no. should be unique and assigned in the order in which instances are created 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 is greater than or
equal to 0.0. If not, the balance should be set to 0.0 and the constructor should display an error
message, indicating that the initial balance was invalid.The class should provide following
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.

 Member function getAccountNo.

• Derived class CreditCardAccount should inherit the functionality of an Account, but also
include a data memberpinnumberset by the customer.

 Constructor should receive the initial balance, as well as pin number.

 It should provide a public member function resetpin

• Derived class CheckingAccount should inherit from base class Account and include an
additional data member of type double that represents the fee charged per transaction to all the
customers.

 CheckingAccount's constructor should receive the initial balance, as well as a parameter


indicating a fee amount.

 Class CheckingAccount should redefine member functions credit and debit so that they
subtract 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 fee only if money is actually withdrawn (i.e., the debit amount
does not exceed the account balance). After defining the classes in this hierarchy, write a
program that creates objects of each class and tests their member functions.

Note: In all the classes make the member functions & data members const ,static where required.
ii. Create two classes named Mammals and MarineAnimals. Create another class
named BlueWhale which inherits both the above classes. Now, create a
function in each of these classes which prints "I am mammal", "I am a marine
animal" and "I belong to both the categories: Mammals as well as Marine
Animals" respectively. Now, create an object for each of the above class and
try calling
1 - function of Mammals by the object of Mammal
2 - function of MarineAnimal by the object of MarineAnimal
3 - function of BlueWhale by the object of BlueWhale
4 - function of each of its parent by the object of BlueWhale

iii. Perform the task of multiple inheritance diamond problems(TA class). Both with and without
solution to the problem occur due to multiple inheritance.

Vous aimerez peut-être aussi