0% found this document useful (0 votes)
63 views6 pages

12 - Inheritance2

This document discusses object-oriented programming concepts like inheritance, class hierarchies, abstract base classes, public and private inheritance, and access specifiers. It provides examples using a Distance class and its derived DistSign class to demonstrate inheritance. It then defines classes like Employee, Manager, Scientist and Laborer to show a class hierarchy. Multiple levels of inheritance and access combinations are also explained with examples.

Uploaded by

Azhar Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

12 - Inheritance2

This document discusses object-oriented programming concepts like inheritance, class hierarchies, abstract base classes, public and private inheritance, and access specifiers. It provides examples using a Distance class and its derived DistSign class to demonstrate inheritance. It then defines classes like Employee, Manager, Scientist and Laborer to show a class hierarchy. Multiple levels of inheritance and access combinations are also explained with examples.

Uploaded by

Azhar Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

AJ/Handout 12 -1- Object-Oriented Programming

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

Inheritance in the English Distance Class


Here is somewhat more complex example of inheritance. In this program we will make the famous
distance class more comfortable by making the data members signed. For this we need not to alter
the base class and will make appropriate changes in the new derived class, where feet and inches
could be positive or negative.
#include <iostream>
using namespace std;
enum posneg {pos, neg};
///////////////////////////////////////////////////////////////
class Distance
{
protected:
int feet;
float inches;
public:
Distance ( ) : feet (0), inche (0.0) { }
Distance (int ft, float in) : feet (ft), inches (in) { }
void getdist( )
{
cout<<”Enter feet:”; cin>>feet;
cout<<”Enter inches:”; cin>>inches;
}
void showdist( ) const
{cout<<feet<<”\’-” <<inches;}
};
//////////////////////////////////////////////////
class DistSign : public Distance
{
private:
posneg sign;
public:
DistSign ( ) : Distance ( ) //no arg ctor, call base ctor and sets sign to +
{sign = pos;}
DistSign(int ft, float in, posneg sg=pos) : Distance (ft, in)
{sign = sg;} //2 or 3-arg ctor call base ctor sets the sign accordingly
void getdist ( )
{
Distance : : getdist ( ); //call base getdist( )
char ch;
AJ/Handout 12 -2- Object-Oriented Programming

cout<<( (sign = = pos ) ? “(+)” : “(-)” );


Distance : : showdist ( );
}
};
//////////////////////////////////////////////////////////
void main ( )
{
DistSign alpha; //no-arg ctor
alpha.getdist ( );

DistSign beta(11,6.25); //2-arg ctor


DistSign gamma(100, 5.5, neg); //3-arg ctor

cout<<”Alpha is:”; alpha.getdist();


cout<<”Beta is:”; beta.getdist();
cout<<”Gamma is:”; gamma.getdist();
}
In program listing the second constructor of the DistSign may have 2 or 3 arguments. If there
would be some argument passed then that will be considered otherwise default value pos will be
considered. Also ultimately the values will be forwarded to constructors in Distance.
Again the member functions in Distance can be accessed by scope resolution operator.

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<<”Enter data for manager 1:”;


m1.getdata();

cout<<”Enter data for manager 2:”;


m2.getdata();

cout<<”Enter data for scientist:”;


s1.getdata();

cout<<”Enter data for laborer:”;


l1.getdata();

cout<<”Data on manager 1:”;


m1.putdata;

cout<<”Data on manager 2:”;


m2.putdata;

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

Constructor and member functions


In above example we have not used any constructor either in base class or derived class, so the
compiler creates the objects of various classes automatically using the default constructor derived
class by calling default constructor of base class.

Public & Private Inheritance


C++ provides a number of ways to access the base class members. One of its factor is the way
derived class are declared. So far we have used statements like that;
class manager : public employee
What is meant by public keyword and what about rest of the keywords? The public keyword
provides the way to access public member functions of base class. If we use private keyword then
the objects of derived class can’t access the public member functions in base class. So derived
class objects cannot access the private or protected member functions of base class.

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

a=objC.protdataA; //error: not accessible


a=objC.pubdataA; //error: not accessible
}

Access Specifier: when to use what


In some condition where we want to use the basic structure to base class we would prefer to use
public access to base class like we did in Countdn and employee classes. And if we do not want to
use the original soul of base class but want to distinguish it from base class, then we can use
private access specifier.

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

Class A //base class A


{
};
Class B //base class B
{
};
Class C: public A, public B //C is derived from A and B
{
};

Member Function in Multiple Inheritance


class student
{ };
class employee
{ };
class manager : private employee, private student
{ };
class scientist : private employee, private student
{ };
class laborer : public employee
{ };

UML Diagram

Employee
Name
Number
Getdata()
Putdata()

Manager
Title Scientist Laborer
Dues Publications

You might also like