Title: Object Oriented Programming Part 6
1Object Oriented ProgrammingPart 6
2Inheritance Generalization / Specialization
Other terms for the same thing
Inheritance General case / Special case Super
class / Sub class Base class / Derived class
Base class / Inherited class "is" relation. The
object of derived class is an object of derived
class. Example (Inheritance) A circle is a s
pecial case of a point. A circle inherits a poin
t. class Circle public Point public
Circle(float x00, float y00, r00)
Circle(const Point kp0, r00)
Circle(const Circle c) private float
r See a separate example later.
3Constructors for derived class
There is no data member of type Point in the
class definition of Circle. Each circle instance
in memory still contains the point object with
all data members of Point. The problem is, how
to construct the base class data members inside
the derived class. The solution is that the const
ructor of base class is called in the
initialisation list of derived class
constructor. So far we have seen the initialisati
on list in a format datamemb1(initvalue1), data
memb2(params), Now the point inside the circle
has no name at all. The format we now have to use
in the initialisation list to construct the base
class part in the derived class is
BaseClassName(parameters) If the base class cons
tructor is not called explicitly in the
constructor of derived class, the compiler
generates the call of the default constructor of
base class. When an instance of derived class is
constructed the order is as follows
1) First the base class part of the object is
constructed. 2) Then the new parts of the derive
d class are constructed.
4Example Constructors of derived class
Example. (Circle is inherited from point)
class Circle public Point
public Circle(float x00, float y00, r00)
Circle(const Point kp0, r0) Circle(con
st Circle c) private float r Cons
tructor implementations CircleCircle(float x0
0, float y00, r00) Point (x0, y0)
r r0 CircleCircle(const Point cp0, floa
t r0 ) Point (cp0) r r0 //Copy constr
uctor CircleCircle(const Circle c0 ) Point (
c0) r c0.r See a separate complete exa
mple on page 8.
5Assignment compatibility
In the initialisation list of the copy
constructor of circle we pass circle object to
the copy constructor of the Point!
This can be done because of assignment
compatibility explained next. If we have declared
Point p Circle c Assignment c p is not
legal, and compiler gives an error message,
because point is not a circle.
On the other hand, the assignment p c
is OK and legal, because circle is " a point.
Data member radius is dropped out in the
assignment above, because p has no data member
for radius. If you try to calculate the area af
ter the assignment in the form p.area(), you ge
t an error message, because point does not have a
method area (or data member radius).
Because circle is a point, everything that we can
do with a point can be done with circle.
All the following ways to call the function
distance are legal (p1 and p2 are points and c1
and c2 are circles and distance is a member
function of Point that calculates the distance
between two points) p1.distance(p2) c1.dis
tance(c2) p1.distance(c2) c1.distance(p2)
Provided that assignment is not overloaded and
there is no suitable conversion constructor
6How features of base class can be used
- How to use properties of base class in derived
class
- 1) The inherited feature can be used as such
(methods getx, gety and move of class point are
valid and useful for circles as such). See
example on the next page. - Overriding member functions
- a) We can add additional functionality to the
inherited method in the derived class (method
read and operator example on the page 7. - b) We can replace the inherited method totally
with a new and different method.
-
- We can add a totally new methods (and data
members) to the inherited class (data member
radius and member function area and enlarge).
-
7How features of base class can be used
The inherited feature can be used as such.
If float getx() void move(float delta_x,
float delta_y) are member functions of Point an
d Circle is inherited from the Point then
Circle c(1.0, 2.0, 10) cout //getx returns x coordinate // of the center
point c.move(0.5, 0.5) // moves the circl
e! Functions getx and move need no declaratio
n in the class definition of Circle.
See a separate complete example on page 8.
8How features of base class can be used
Overriding member functions a) We can add additio
nal functionality to the inherited method in the
derived class. Lets assume that float dist
ance(const Point p2) const is a member of Poin
t and we inherit Circle from the Point. We need a
distance function for the class too, but the
formula is a bit different. Thats why we have to
modify the behaviour of function distance in the
class Circle. How do we do this?
1. Function prototype inside class definition of
Circle float distance(const Circle c2) const
2. Implementation of this function float Ci
rcledistance(const Circle c2) const
float dist dist Pointdistance(c2) //
use functionality //from the base class
dist dist radius c2.radius // add some
return dist functionality b) We can replace the inherite
d method totally with a new and different method
without using functionality from the base class.
See a separate complete example on page 8.
9Complete example 1 (1/4)
//Class Circle is derived (inherited) from class
point. This means that Circle
//is specialised point. It has all properties of
point and some additional //properties include
include include //definition of class Point class Point pu
blic Point(float xcoord0.0, float ycoord0.0)
//constructor Point(const Point p) /
/copy constructor void read(const char promp
t) float getx() const float gety()const
void move(float deltax, float deltay)
float distance(const Point p2) const
private float x float y //definitio
n of class Circle class Circle public Point
public Circle(float cpx0.0, float cpy0.0, f
loat r1.0) //constructor 1 Circle(const Point
cp, float r) //constructor 2
Circle(const Circle c) //copy constructor
void read(const char prompt)
float getRadius() const
float distance(const Circle p)
const float area() const private f
loat radius //prototypes of input and output
operators ostream operatort Pointp) ostream operatorst Circle c)
10Complete example 1 (2/4)
//Test application program void main(void) Po
int p, p3(3.0, 3.0) Circle c1, c2(2.0, 2.0, 20
.0) , c3(p3,30) c1.read("Enter circle 1")
cout nt of c1 is " c1.gety() endl cout c3.area() e between circles is " endl c3.move(0.5 , 0.5) co
ut ot possible. It causes compilation error
p c1 //circle can be assigned to a point
cout t cout //p is moved //cout not possible. It causes compilation error
11Complete example 1 (3/4)
// Methods of class Point PointPoint(float xcoo
rd, float ycoord) x xcoord y
ycoord Point Point(const Point p) //cop
y constructor x p.x y p.y
void Pointread(const char prompt)
cout cin x cout cin y float Pointgetx(void)const r
eturn x float Pointgety(void) const retu
rn y void Pointmove(float deltax, float del
tay) xdeltax ydeltay float Poin
tdistance(const Point p2) const
return sqrt( (p2.x-x)(p2.x-x) (p2.y -
y)(p2.y - y))
12Complete example 1 (4/4)
// Methods for class Circle Circle Circle (flo
at cpx, float cpy, float r0 ) Point(cpx, cpy),
radius(r0) Circle Circle(const Point cp
, float r0) Point(cp), radius(r0)
Circle Circle(const Circle c) Point (c),
radius(c.radius) void Circle read(const
char prompt) cout nEnter center point ") cout dius " cin radius float CirclegetRadi
us() const return radius float Circledis
tance(const Circle c2) const
float dist dist Pointdistance(c2)
//use functionality from the base class
dist dist - radius - c2.radius // add some
new functionality return dist float Circle area(void) const float a
rea area M_PI radius radius ret
urn area //Output operator functions. (They a
re not friends now.) ostream operatorout, const Pointp) out "," am operator out return out
13Access specifier protected
- When the meaning of access specifiers is
considered we have two different viewpoints
- Using an existing class as such.
- Using an existing class by inheriting from it.
- If the access specifier for a member is private,
there is no way to access that member in both
cases above.
- If the access specifier for a member is public,
it is accessible in both of cases above.
- So far we have seen only access specifiers
private and public.
- There is also a third access specifier protected.
- The effect of access specifier protected is as
follows
- a) It is not allowed to refer to the member in
the application (case 1 above). In that sense it
is same than private.
- b) It is allowed to refer to the member in
member functions of that class. In that sense it
is same than private.
- c) The difference (compared to private member)
is that it is possible to refer to the protected
member of the base class in the member function
of the derived class (case 2). - See complete example inheritance2.cpp
14Example 2 (1 / 3)
/ Class Circle is derived (inherited) from class
point. The meaning of access specifier protect
ed is illustrated/ include inclu
de //definition of class Point class Po
int public Point(floa
t xcoord0.0, float ycoord0.0) //constructor
Point(const Point p) //copy constructor
void read(const char prompt)
float getx() const float gety()const prot
ected float x float y //definition o
f class Circle class Circle public Point pu
blic Circle(float cpx0.0, float cpy0.0, floa
t r1.0) //constructor 1 Circle(const Point
cp, float r) //constructor 2
Circle(const Circle c) //copy constructor
void read(const char prompt)
float getRadius() const return radius
private float radius //prototypes of
input and output operators ostream operatortream out, const Pointp) ostream operatorstream out, const Circle c)
15Example 2 (2 / 3)
//Test application program void main(void)
Point p2(2.0, 2.0) Circle c1(1.0, 1.0, 10.
0), c2(p2, 20.0), c3(c2), c4 //cout is not allowed here because x (and y) is
protected //member (not public or private priv
ate member) //you have to use public access fu
nctions cout gety() 4") cout cout cout cout // Methods of class Point are exactly as they
were before //Output operator functions. (They
are not friends now.) ostream operatorm out, const Pointp) cout stream operator cout ) return out
16Example 2 (3 / 3)
// Methods for class Circle //Constructor version
1 / Circle Circle (float cpx, float cpy, flo
at r ) Point(cpx, cpy), radius(r)
/ //This is possible too, because x and y are
protected //Constructor version 2 Circle Circ
le (float cpx, float cpy, float r )
x cpx // this is now possible because
y cpy // x and y are protected (not
private) radius r Circle Circle(co
nst Point cp, float r) Point(cp)
//x cp.x // this is not allowed
//y cp.y // You can access only
protected members radius r // of Point
"inside" Circle. // Paramerer
cp is independent point object
Circle Circle(const Circle c) / Point(c)
/ x c.x // This is now allowed
y c.y radius c.radius void Circ
le read(const char prompt)
cout point " cin x //you can access protec
ted data member cin y //of base class in
a member function of derived class
cout radius
17Inheritance types
Keywords private, protected and public are used
as access specifiers. The same keywords words can
also be used as a type of inheritance.
In our first example of inheritance we used
public inheritance. class Circle public Point
This means that all public members of Poin
t are available for the user of Circle
Circle c cout c member of Point To be more precise it means tha
t all members of Point still have the same access
specifications in Circle. This also means, that
the instance of derived class is (is-relation) an
instance of the base class, because it can be
used like an instance of base class.
The inheritance type can be used to restrict the
access rights to the members of base class in the
derived class. A separate table illustrates the e
ffects of inheritance types to different data
members from base class. The example program furt
her illustrates the effect of inheritance type
(Inheritance4.cpp). Inheritance type public is us
ed to inherit interface. Inheritance type protect
ed and private are used to inherit
implementation.