LAB# 08: Inheritance: Description
LAB# 08: Inheritance: Description
LAB# 08:
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.
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.
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 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
ii. 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 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.
• Derived class CreditCardAccount should inherit the functionality of an Account, but also
include a data memberpinnumberset by the customer.
• 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.
Note: In all the classes make the member functions & data members const ,static where
required.
THINK:
What is Inheritance?
What are advantages of inheritance write any 2?
What is syntax of doing inheritance?