0% found this document useful (0 votes)
19 views19 pages

Lecture 15

Uploaded by

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

Lecture 15

Uploaded by

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

CSC241: Object Oriented Programming

Lecture No 15

1
Previous Lecture
• Function overriding
– Example program
– Stack class – stack2 class
• push() and pop()
– Distance class – Distsign class
• getdist() and setdist()
• Class hierarchy
– Employee program
• Employee class
• Scientist class
• Manager class
• Laberor class 2
Today’s Lecture
• Abstract class
• Public and private Inheritance
– Example program
• Level of inheritance

3
Abstract base class
• Classes used only for deriving other classes, as
employee, are sometimes loosely called
abstract classes, meaning that no actual
instances (objects) of this class are created

4
public and private Inheritance
• Class manager : public employee
• The keyword public specifies that objects of
the derived class are able to access public
member functions of the base class
• When this keyword is not used, objects of the
derived class cannot access public member
functions of the base class
• Result is that no member of the base class is
accessible to objects of the derived class
5
Cont.
• public inheritance represents is a relationship
Type of inheritance
Base class : Public Protected Private
access inheritance inheritance inheritance
specifier

Public Public in Protected in Private in


derived class derived class derived class
Protected Protected in Protected in Private in
derived class derived class derived class
Private Hidden in Hidden in hidden in
derived class derived class derived class
6
7
Access Combinations
class A { class B : public A { class C : private A {
private: public: public:
int privdataA; void access() { void access() {
protected: int x; int y;
int protdataA; x = privdataA; y = privdataA;
public: x = protdataA; y = protdataA;
int pubdataA; x = pubdataA; y = pubdataA;
}; } }
}; };

int z; z = objC.privdataA;
B objB; z = objC.protdataA;
z = objB.privdataA; z = objC.pubdataA;
z = objB.protdataA;
z = objB.pubdataA; Go to program 8
Cont.
• Derived class
• Functions can access protected and public member
in base class (in case of public, protected and
private inheritance)
• Objects cannot access private or protected
members of the base class (in case of public
inheritance)
• Objects cannot access public, private or protected
member of base class (in case of private or
protected inheritance)

9
Access Specifiers: When to Use What
• In most cases a derived class exists to offer an
improved—or a more specialized—version of the
base class
• We’ve seen examples of such derived classes
CountDn class that adds the decrement operator
to the Counter class and the manager class that is
a more specialized version of the employee class
• In such cases it makes sense for objects of the
derived class to access the public functions of the
base class if they want to perform a basic
operation
10
Cont.
• The derived class is created as a way of
completely modifying the operation of the base
class, hiding or disguising its original interface
• Examples
– Array class that acts like an array but provides
protection against out-of-bounds array indexes
– Objects of Stack2 should always be treated as if
they were stacks, using push() and pop()
• In this situation, private inheritance would
allow you to hide all the Array class functions
from objects of the derived Stack2 class
11
Levels of Inheritance
• A class can be derived from a class that are
themselves derived
• B is derived from A, and C is class A
derived from B. The process can { };
class B : public A
be extended to an arbitrary number { };
of levels—D could be derived from C, class C : public B
{ };
and so on
• As a more concrete example, suppose that we
decided to add a special kind of laborer called a
foreman to the EMPLOY program

12
UML class diagram – EMPLOY program

Foremen oversee the widget-


stamping operation, supervising
groups of laborers
13
C++ EMPLOY program
class foreman : public laborer { main()
private: {
float quotas; laborer l1;
public: foreman f1;
void getdata() cout << “\nEnter data for laborer 1”;
{ l1.getdata();
laborer::getdata(); cout << “\nEnter data for foreman 1”;
cout << “ Enter quotas: “; cin >> f1.getdata();
quotas; cout << “\nData on laborer 1”;
} l1.putdata();
void putdata() const cout << “\nData on foreman 1”;
{ f1.putdata();
laborer::putdata(); }
cout << “\n Quotas: “ << quotas;
}
}; Go to program
14
Multiple inheritance
• A class can be derived from more than one
base class. This is called multiple inheritance

class A {

};
class B {

};
class C : public A, public B{

};
15
Member Functions in Multiple Inheritance
• Suppose
– we need to record the educational experience of some of the
employees in the EMPLOY program
– In a different project, we’ve already developed a class called
student that models students with different educational
backgrounds
• Instead of modifying the employee class to incorporate
educational data, we will add this data by multiple
inheritance from the student class
• The student class stores the name of the school or
university last attended and the highest degree received
• Educational information is not relevant to every class of
employee
16
17
Miniprogram showing relationships
class student
{ };
class employee
{ };
class manager : private employee, private student
{ };
class scientist : private employee, private student
{ };
class laborer : public employee
{ };
18
19

You might also like