Title: The Objectoriented Programming Paradigm
1The Object-oriented Programming Paradigm
As implemented in C
Features
- Encapsulation of data and operations on that data
2Encapsulation of Data and Operations
More robust architecture
- Localizes effects of changes in a program
- Easier to maintain program composed of objects
is more stable under change than a program
composed of functions
- Off the shelf components can be built and used
3Encapsulation of Data and Operations
Programmer can define access to the attributes
and methods of the class
- private accessible only to member functions of
the class
- protected accessible to member functions of the
class and to operations in any descendant class
- public accessible to operations from all
classes in the program
4Encapsulation of Data and Operations
The programmer of a class can loosen the
restrictions on access to private or protected
features by declaring friend functions and friend
classes
class Node friend class LinkedList
private itemtype key Node next
5friend classes and functions
Friends have access to private and protected
features of a class
The status of friend can only be given by a
class. It cannot be claimed or inherited.
6Reasons for conferring friend status
Known symbiotic dependencies between classes.
Examples Link and LinkedList Link,
LinkedList, and ListIterator
Function whose source parameter is an object of
the class but whose target is a different object
of the same class
Example shown on next slide
7Reasons for conferring friend status (cont.)
class Account private double
balance, credit_limit public //other
operations void deposit(double amt) friend
void transfer (Account targ, Account source,
double amt
8Genericity
Container objects such as lists stacks and
queues Have the same behaviors regardless of what
kind of data object is being contained.
To support reuse, we need to be able to create
container classes that are capable of holding
objects of a variety of types.
This capability is provided in C by templates.
9Templates
Example
template ltclass Tgt
class Stack
public Stack(int cap) Stack( )
void push(T item) void pop( ) T
top ( ) boolean empty ( )
include Stack.cpp
10Implementation using Templates
template ltclass Tgt Stack ltTgt Stack (int cap)
buffer new Tcap first 0 capacity
cap
Qualifier Stack ltTgt specifies both class and
template parameter
buffer is an array of objects of type (class) T
where both the type of the contents T and the
capacity are specified by the client at the time
of compilation.
11Implementation using templates (cont.)
template ltclass Tgt void StackltTgtpush (T
item) if (first capacity) //raise
exception -- not shown in abbreviated .h file
error (OVERFLOW) bufferfirst
item first template ltclass Tgt T
StackltTgt top( ) if (first 0)
//raise UNDERFLOW error error
(UNDERFLOW) return bufferfirst
function()
12Limitation of Templates in Providing Genericity
If items in a container are ordered, they must be
able to respond to relational operators such as lt
or (Not all classes have objects that are
comparable because the relational operators have
not been overloaded in those classes or the
objects are inherently incomparable and
overloading the relational operators makes little
sense.)
13Polymorphism
Polymorphism is provided in C by the following
Overloading of functions and operators
- Same function may be found in two or more classes
- Function with the same name appears in one class
with different sets of arguments
Overriding methods in a descendant class to
augment or refine behavior defined in the parent
class
- May change the implementation, but should never
change the fundamental meaning of the method as
defined in the parent.
Substitution of one variable (object) type
(class) for another at runtime
- Dynamic bonding can be achieved in certain
circumstances
14Operator Overloading
Restrictions on overloading operators
You can overload any operator except .
? sizeof
You cannot define new operators by overloading
symbols that are not already operators.
At least one operand of an overloaded operator
must be an instance of a class.
You cannot change the precedence of a C
operator, or the number of its operands.
15Operator Overloading
Reasons for operator overloading
Overloading the operator allows for deep copy
of objects with dynamically allocated memory
Example Linked List
16Operator Overloading
Shallow copy using the default operator
B A
B.size A.size B.first A.first B.last
A.last
A
B
17Operator Overloading
Deep copy
List Listoperator (const List rhs)
//if the target is not empty, make it empty
while (first ! NULL) link temp
first first first -gt next delete temp
link ptr rhs.first while
(ptr) insert ( ptr -gt key) //assumes insert
at back of list ptr ptr -gt next
return this
Deep copy produces an independent chain of links
attached to the target List
18Operator Overloading
Consider the following example class Complex
(abridged)
class Complex private double re,
im public Complex ( ) //default
constructor Complex (double x, double
y) //other member functions
Complex operator (const Complex rhs)
friend Complex operator (const Complex
scr1, const Complex
scr2)
friend ostream operator ltlt (ostream out,
Complex rhs)
19Operator Overloading (cont.)
Complex Complex operator (const Complex
rhs) re rhs.re im rhs.im return
this
This is a member function of class Complex
- The left-hand side of the operator is the target
object that receives the message
Example Complex z1(2, 3) Complex z2 (21,
-24) z2 z1
20Operator Overloading (cont.)
Complex operator (const Complex src1,
const Complex src2) Complex z
z,re src1.re src2.re z,im
src1.im src2.im return z
Sources scr1 and scr2 are passed by reference,
but the const declarations prevents the
programmer from changing their state.
- This operator is not a member function of class
Complex -- just a friend - no qualifier is used in its header.
Return type is an object, not a reference --
each of the following assignments are permitted
Complex z1, z2(1, 2), z3(-2, 4) z1 z2
z3 z2 z2 z3 cout ltlt z2 z3
21Operator Overloading (cont.)
ostream operator ltlt (ostream out, const
Complex rhs) out ltlt ( ltlt rhs.re ltlt
, ltlt rhs.im ltlt ) return out
Returns a reference to the ostream object to
provide for the sequencing of stream insertion
operators in a single statement
Complex z1(1.2, -2.1), z2(24, 44) cout ltlt z1
ltlt ltlt z2 ltlt z1 z2 ltlt endl
22Substitution of types at Runtime
The substitute must be a subclass of the declared
type
The original class (type) and the substitute must
be referenced by a pointer
Methods overridden in the subclass must be
declared virtual in the parent.
23Substitution
void chase_cats(Cat acat)
Parent class
Descendant class
Dogs bark ruff, ruff
Animals are mute
24Substitution
Consider an array of (pointers to) Animals
Animal theBarn4
Now create some animals and put them in theBarn.
Animal MickeyMouse(Mickey) Cat Sly
(Sylvester) Dog Fred Dog Spot
(Spot) theBarn0 MickeyMouse theBarn1
Sly theBarn2 Fred theBarn3 Spot
25Substitutions
Now tell each animal to speak
for (int i 0 i lt 4 i) theBarni -gt
speak( )
The dogs will bark, the cat meow, and the mouse
say that he is mute.
But the dogs cannot chase cats!
26Substitution
The Farm
Farm objects have a data member called theBarn
that is an array of animal pointers
27Substitution
An example that illustrates polymorphism
Dog.h
Cat.h
Farm.h
Animal.h
farmyard.exe
farmyard.cpp
Strings.h
Copy these files and see what happens when you do
the following
- Remove virtual from in front of speak( ) in animal
- Try having a dog chase one of the cats
28Inheritance and Abstract Classes
Example
Shape is abstract It has no instances
29Abstract Classes
class Shape protected //data members
needed if used for substitution int X,
Y double R1, R2 public Shape (int
x, y, double u, v) virtual double area ( )
0 virtual double perimeter ( ) 0
If at least one method is undefined, the class is
abstract
The abstract class Shape provides the
specification for methods in the subclass, but no
actual implementation.
Two dimensional shapes have an area and a
perimeter, but only specific shapes have formulae
for computing them.
30Inheriting from Abstract Classes
class Circle public Shape
public Circle (int x, int y, double r)
X(x), Y(y), R1(r ), R2(0) double area ( )
return M_PI R1 R1 double perimeter ( )
return 2 M_PI R1 //other methods for
Circle
31Using Abstract Classes
Abstract classes provide a Framework for adding
application specific behavior
User must supply the implementation details for
all undefined methods.
Abstract classes are useful as generalizations
where common behavior of separate but similar
classes is abstracted out and placed in a common
parent
- Provides for design simplification
- Provides for run-time polymorphism
32Application using Template Class
List ltShape gt L Circle p new Circle(10,
12, 4.0) L.insert(p, 1) Circle p2 new
Circle (15, 24, 3.6) L.insert(p2, 2) Rectangle
p3 new Rectangle(21,36, 2.5,
4.1) L.insert(p3, 3) for (int i 1 i
ltL.length() i) Shape q
L.retrieve(i, q) q -gt area ( )