Lab 7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

LAB #07:Inheritance

Tool used: Microsoft Visual Studio

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.in inheritance, a class derives the behavior and structure
of another (existing) class.

The main advantages of inheritance are code reusability and readability. When child class inherits the
properties and functionality of parent class, we need not to write the same code again in child class. This
makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.

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 : access-specifier BaseClass

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously
defined class. If the access-specifier is not used, then it is private by default.
We can summarize the different access types according to - who can access them in the following way –

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no


A derived class inherits all base class methods with the following exceptions −
 Constructors, destructors and copy constructors of the base class.
 Overloaded operators of the base class.
 The friend functions of the base class.

Type of Inheritance
When deriving a class from a base class, the base class may be inherited through public, protected or
private inheritance. The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance, but public inheritance is commonly used. While using
different type of inheritance, following rules are applied −

Public Inheritance − When deriving a class from a public base class, public members of the base class
become public members of the derived class and protected members of the base class become protected
members of the derived class. A base class's private members are never accessible directly from a derived
class, but can be accessed through calls to the public and protected members of the base class.

Protected Inheritance − When deriving from a protected base class, public and protected members of
the base class become protected members of the derived class.

Private Inheritance − When deriving from a private base class, public and protected members of the
base class become private members of the derived class.

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.

i. Member function credit should add an amount to the current balance.


ii. 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.
iii. Member function getBalance should return the current balance.
iv. Member function getAccountNo.

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

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


ii. 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.
Appendix A

class C1 class C3: public C2


{ {
private: private:
int a; int g;
public: public:
intc,b; }; inth,i; };

class C2: Public C1 { class C4: private C3 {


private: intd,e; int j; public:
public: int f; }; intk,l;};

ii. Determine class member visibility for each variable in the class hierarchy provided in Appendix
A.Present your answer (in your answer booklet) in the form of a table like the one shown below,
using the following symbols: private (-), public (+), not visible (blank).protected( / )

You might also like