12 - Inheritance2
12 - Inheritance2
Lesson 12
Objectives
Inheritance in the English Distance Class
Class Hierarchies
Abstract Base Class
Public and Private Inheritance
Access Combination & Access Specifiers
Levels of Inheritance
UML diagram
Class Hierarchies
Another use of class is to provide hierarchy for different scenarios like employees could be a base
class and we can derive the subclasses, as manager, scientist and laborer. So in that way we can
link the programming with features of today’s life. That provides a database view. Where each
entity contains some attributes, like a scientist can have some publications, an employee can have
name and number etc. Our example program consists of a base class named employee, this class
handles employee’s name and number. From this class three classes are derived: manager, scientist
and laborer. The manager and scientist classes contain additional information about these
categories of employees, and member function to handle this information.
#include<iostream>
using namespace std;
const int LEN=80
///////////////////////////////////////////////////////////////
class employee
{
protected:
char name[LEN];
unsigned long number;
public:
void getdata();
{
cout<<”Enter name:”; cin>>name;
cout<<”Enter number:”; cin>>number;
}
void putdata()const
{
cout<<”Name:”<<name;
cout<<”Number:”<<number;
}
};
class manager : public employee
{
private:
char title[LEN];
double dues;
public:
AJ/Handout 12 -3- Object-Oriented Programming
void getdata()
{
employee :: getdata();
cout<<”Enter title:”; cin>>title;
cout<<”Enter dues:”; cin>>dues;
}
void putdata() const
employee::putdata();
cout<<”Title:”<<title;
cout<<”Dues:”<<dues;
}
};
////////////////////////////////////////////
class scientist : public employee
{
private:
int pubs;
public:
void getdata()
{
employee::getdata();
cout<<”Enter number of publications:”; cin>>pub;
}
void putdata() const
{
employee::putdata();
cout<<”Number of publications:”<<pubs;
}
};
/////////////////////////////////////////////////////
class laborer: public employee
{};
//////////////////////////
void main( )
{
manager m1,m2;
scientist s1;
laborer l1;
cout<<”Data on scientist:”;
s1.putdata;
cout<<”Data on Laborer:”;
l1.putdata;
}
Abstract Class
It can loosely be defined as, “the classes used only for deriving other classes, also no actual objects
of that class are created.” Like in above example employee was the abstract class.
AJ/Handout 12 -4- Object-Oriented Programming
Access Combinations
Let us look at the following example where we have used more than one access specifier.
class A
{
private:
int privdataA;
protected:
int protdataA;
public:
int pubdataA;
};
/////////////////////////////
class B : public A
{
Public:
void funct()
{
int a;
a=privdataA; //error: not accessible
a=protdataA; //ok
a=pubdataA; //ok
}
};
/////////////////////////////
class c : private A // privately derived class
{
public:
void funct()
{
int a;
a=privdataA; //error: not accessible
a=protdataA; //ok
a=pubdataA; //ok
}
};
//////////////////////////////
void main()
{
int a;
B objB;
a=objB.privdataA; //error: not accessible
a=objB.protdataA; //error: not accessible
a=objB.pubdataA; //ok (since A is public to B)
C objC;
a=objC.privdataA; //error: not accessible
AJ/Handout 12 -5- Object-Oriented Programming
Levels of inheritance
Classes can be derived from classes that are themselves derived. Here is a mini program that shows
the idea:
class A
{ };
class B : public A
{ };
class C : public B
{ };
Here B is derived from A, and C is derived from B. The process can be extended to an arbitrary
number of levels. Using previous example of employee class we can extend it by deriving another
class by laborer named foreman.
class laborer : public employee
{
};
//////////////////////////////////////////
class foreman : public laborer
private:
float quotas;
public:
void getdata()
{
laborer : : getdata();
cout<<”Enter Quotaz:”; cin >>quotas;
}
void putdata() const
{
laborer : : putdata();
cout<<”Quotas:”<<quotas;
}
};
Multiple Inheritance
A class can be derived from more than one base class. This is called multiple inheritance. Any
class C can be derived from A, B and some arbitrary number of classes.
AJ/Handout 12 -6- Object-Oriented Programming
UML Diagram
Employee
Name
Number
Getdata()
Putdata()
Manager
Title Scientist Laborer
Dues Publications