0% found this document useful (0 votes)
45 views

Inheritance

The document describes an experiment on inheritance in object-oriented programming. It defines key concepts like single, multilevel, and multiple inheritance. It provides code examples and tasks for students to practice inheritance by creating classes for employees, students, and animals.

Uploaded by

UMAIR Khan
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)
45 views

Inheritance

The document describes an experiment on inheritance in object-oriented programming. It defines key concepts like single, multilevel, and multiple inheritance. It provides code examples and tasks for students to practice inheritance by creating classes for employees, students, and animals.

Uploaded by

UMAIR Khan
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/ 9

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL & MECHANICAL ENGINEERING

EXPERIMENT NO 6

Lab Title: Inheritance


Student Name: Reg. No:

Objective:

LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes (5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:


EXPERIMENT NO 6

Inheritance

Objectives
In this lab students will learn,
 The concepts of inheritance
 Base and derived classes
 Public inheritance
 Private inheritance
 Protected inheritance

Equipment required
 Visual Studio/ Dev C++/Eclipse installed in Windows/PC

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

Advantages

• Saves time
• Reuse of proven, debugged, high quality software
Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one subclass is
inherited by one base class only.

Syntax
Class derived_class_name: access-specifier base_class_name
{ //body }
Example

class A
{ ... };
class B: public A
{... .. ...};
Code Example

#include<iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{cout<< "This is a Vehicle\n";

}};
//sub class/derived from a single base class called Vehicle
class Car : public Vehicle {

};
// main function
int main()
{ // Creating object of sub class will
// It will invoke the constructor of base class
Car obj;
return 0;}

Multilevel Inheritance
In C++ programming, not only you can derive a class from the base class but you can also
derive a class from the derived class. This form of inheritance is known as multilevel
inheritance.

class A {
... .. ... };
class B: public A {
... .. ...};
class C: public B {
... ... ...
};
Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class.
i.e one subclass is inherited from more than one base class.

class subclass_name : access_mode base_class1, access_mode


base_class2{
// body of subclass
};
Syntax
class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};
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. It can be public, private, or
protected.

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 TASKS
Q1. Write a class Employee that contains attributes of employee id and his scale. The class
contains member functions to input and show the attribute. Write a child class Manager that
inherits Employee class. The child class has attributes of manager id and his department. It
also contains the member functions to input and show its attributes. Also create constructors
and destructors of both the classes. Write a main function to test this class.

Q2. Define a class Student as a base class with data members as admissionNo, Name, age
and address as protected type and getStudent function for setting values for these data
members. These members and functions are common to both derived classes. Make two
derived classes of Student class as undergraduate and graduate student each containing data
member degree_program "in which student is enrolled" and member functions getdegree
"set degree_program" and show "display whole information". Degree in which student is
enrolled and display the whole information of graduate and undergraduate student like this

Q3. Create two classes named Mammals and Marine-Animals. Create another class named
Blue-Whale 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 Marine-Animal by the object of Marine-Animal
3 - function of Blue-Whale by the object of Blue-Whale
4 - function of each of its parent by the object of Blue-Whale

HOME TASK

Q1. By using multiple inheritance, make a student class representing student with data
member roll_no and two member functions get_roll_no to get and display_roll_no to display
roll number of student. A test class (derived class of student) representing the scores of the
student in two subjects and sports class(derive class of stu) representing the score in sports.
The sports and test class should be inherited by a result class having the functionality to add
the scores of two subjects and sports score an then display the final result for students.

Q2.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.

You might also like