Inheritance
Inheritance
EXPERIMENT NO 6
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
Data presentation
Experimental results
Conclusion
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.
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.
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 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.