Title: Prepared by
1Object-Oriented Programming(Part 2)
Programming Language Principles Lecture 31
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida
2The this Pointer
- To invoke a class member function, use the
''dot'' operator (structure field selection) - ltobject namegt . ltmember function namegt
- The this pointer an implicit parameter to each
class member function, of type (Object ). - Its value is the address of the current object,
through which the member function has been
invoked. - It can be used inside member functions. Examples
coming up.
3The const Qualifier
- Three places where the const qualifier can be
used - in a member function declaration
- class A
- ...
- const X f(const Y y) const
- // 1. Can't assign to object returned
- // 2. Can't change parameter y
- // 3. Can't change this.
-
-
4The const Qualifier (contd)
- A a
- ...
- a.f(y)
- // f is a member of class A.
- // a is an object of class A.
- // a.f(y) can't be assigned a value.
- // a.f(y) cannot change y.
- // a.f(y) cannot change a.
5Constructors
- class cell
- cell(int i, cell n) infoi nextn
- int info
- cell next
-
- cell a(1,0)
- //declare a and call cell(1,0)
- cell b(2,a)
- //declare b and call cell(2,a)
- These dont just declare a and b of type cell.
- They also call the constructor function cell.
6Overloading Constructors
- class cell
- cell (int i) infoi nextthis
- cell (int i, cell n)
- infoi nextn
-
- int info
- cell next
-
- cell c(7)
7Friend Classes and Information Hiding
- Class members are private by default.
- A friend declaration in a class allows functions
from another class to access members of the
current one. - class cell
- friend class circlist
- cell (int i) infoi nextthis
- cell (int i, cell n) infoi nextn
- int info
- cell next
-
- cell has no public interface. Its an
'auxiliary' data type, solely for use with
circlist.
8Friend Classes and Information Hiding
- class circlist
- private
- cell rear
- public
- circlist() rearnew cell(0)
- circlist()
- int empty() const return rearrear-gtnext
- int top() const return rear-gtnext-gtinfo
- void push (int)
- int pop()
- void enter(int)
-
- The class circlist has a total of seven member
- functions, four of which still need to be defined.
9Friend Classes and Information Hiding
- circlistcirclist()
- cell tmp1rear, tmp2rear-gtnext
- while (tmp2!rear)
- delete tmp1
- tmp1tmp2
- tmp2tmp1-gtnext
-
- delete tmp1
-
- void circlistpush(int x)
- rear-gtnext new cell (x, rear-gtnext)
10Friend Classes and Information Hiding
- void circlistenter(int x)
- rear-gtinfo x
- rear rear-gtnext new cell(0,rear-gtnext)
-
- int circlistpop()
- if ( empty() ) return 0
- cell front rear-gtnext
- rear-gtnext front-gtnext
- int x front-gtinfo
- delete front return x
-
11Friend Classes and Information Hiding
- Class circlist hides its internal details.
- Objects of type circlist are manipulated
exclusively through the public interface of
circlist. - Information hidden inside circlist circlist
based on cell, only rear is stored. - Should the implementation of circlist ever
change, users of circlist will be COMPLETELY
UNAFFECTED. - Three principles of Object-Oriented Programming
- Abstraction, Encapsulation, Information Hiding.
12Derived Classes
- Humans tend to abstract on two dimensions
- A part-of B, (a.k.a. has_a)
- A kind-of B, (a.k.a. is_a).
- ADT programming focuses on the has_a relation.
- OOP also includes the is_a abstraction.
- Support for the is_a abstraction implies
-
- if A is_a B, and B has_a X, then A has_a X.
- This is inheritance !!
13Derived Classes (contd)
- In general,
- class ltderived-classgt public ltbase-classgt
- ltmember-declarationsgt
-
- All members of ltbase-classgt are also members of
ltderived-classgt. - The ltderived-classgt may have additional members.
- public clause derived members retain their
attributes (public, private, or protected). - private clause derived members will be private.
14The circlist class, revisited
- class circlist
- public
- int empty () const return rearrear-gtnext
- int top() const return rear-gtnext-gtinfo
- // 'inspectors'
- protected
- circlist () rear new cell(0)
- circlist()
- void push(int)
- int pop()
- void enter(int) // 'mutators'
- private
- cell rear
-
15The circlist class, revisited
- Protected members behave as if they were private,
except that - they are visible to members (and friends) of
derived classes. - Some of the circlist functions are now protected
so they'll be inherited by the new class queue.
16Derived class queue
- class queue private circlist
- public
- queue()
- queue()
- void enqueue (int x) enter(x)
- int dequeue () return pop()
- circlistempty
- circlisttop
-
17Complete list of members of queue
- Public Functions
- queue new constructor
- queue new destructor
- enqueue new
- dequeue new
- empty, top inherited, explicitly made public
- Private Functions
- push inherited
- pop inherited
- enter inherited
- Private Variables (accessible to new functions of
queue) - none
- Private Variables (accessible only by inherited
functions) - rear inherited
18Another class derived from circlist
- class stack private circlist
- public
- stack ()
- stack ()
- void push (int x) circlistpush(x)
- int pop() return circlistpop()
- circlistempty
- circlisttop
-
19Sample Use of these Classes
- main ()
- stack s
- queue q
- s.push(1) s.push(2) s.push(3)
- q.enqueue(7) q.enqueue(8) q.enqueue(9)
- cout ltlt "Stack Top " ltlt s.top() ltlt endl
- cout ltlt "Queue Top " ltlt q.top() ltlt endl
- cout ltlt s.pop() ltlt " "
- cout ltlt s.pop() ltlt " "
- cout ltlt s.pop() ltlt endl
- cout ltlt q.dequeue() ltlt " "
- cout ltlt q.dequeue() ltlt " "
- cout ltlt q.dequeue() ltlt endl
-
Results Stack Top 3 Queue Top 7 3 2 1 7 8 9
20Class Hierarchies
- class employee / . . . /
- class manager
- public employee / . . . /
- class director
- public manager / . . . /
- class temporary / . . . /
- class secretary
- public employee / . . . /
- class tsec
- public temporary, //MULTIPLE INHERITANCE !!
- public secretary / . . . /
- class consultant
- public temporary,
- public manager / . . . /
21Class Hierarchy is a DAG
22Type Identification
- In our example, given a pointer of type
employee, it can point to - employee, secretary, tsec, manager, or director.
- Three solutions, in general, to this problem
- Only use pointers to classes that have no derived
classes. Sure ... - Use a variable to determine the type of the
object. Very easy to forget to check the type
variable. - Use virtual functions.
23Virtual Functions
- Allow print in both employee and manager, with
different - definitions. C will do the right thing,
based on the actual - object class.
24Virtual Functions (contd)
25Virtual Functions (contd)
26Virtual Functions (contd)
27Operator Overloading
- Allow intrinsic operators in C to be applied to
objects of new types. - Overloading is achieved by defining functions
named operatorXXX, where XXX is one of - Cant overload these
- Cant change precedence, arity or associativity.
Cant add new operators, either.?
28Operator Overloading (contd)
- Example the subscript operator, which returns
- a reference. First, without operator overloading
29Operator Overloading (contd)
- Now, simply replace elem with operator
30Overloading Operators ltlt and gtgt
- We wish to use ltlt and gtgt for user-defined
objects, the same way they are (normally) used
for "built-in" objects (e.g. int, char, etc.).
31Overloading Operators ltlt and gtgt
- Input is similar. The signature is
- To use them
32The Orthodox Canonical Form
- An idiom (pattern). The OCF is characterized
- by the presence of
- A default constructor
- XX()
- A copy constructor
- XX(const X)
- An assignment operator
- X operator(const X)
- A destructor
- XX()
33Object-Oriented Programming (Part 2)
Programming Language Principles Lecture 31
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida