Title: SEG4570 System Design and Implementation
1SEG4570System Design and Implementation
2Contents
- Polymorphism
- Friend function
- Operator overloading
3Polymorphism
- Object Pointers in C
- In C, a Base class pointer can point to any of
its derived class object. - However, the reverse case will incur an error.
class Grandpa print()... class Papa
public Grandpa print()... class Me public
Papa print()... main() Grandpa grandpa
Papa papa Me me Grandpa a3
a0grandpa a1papa a2me
for(int i0ilt3i) ai-gtprint() ...
4Polymorphism
- Binding in C
- Binding refer to the association of an object
pointer to its pointing object's method. - In the above example, binding is done at
compilation and the print method of the pointer
type class (Grandpa) will be invoked. - There are two kinds of bindings in C
- 1. Compile Time Binding
- Binding is done during compilation of your
program. - The method of the pointer class type will be
invoked. - i.e. Grandpaprint() will be called for all
- ai-gtprint()
5Polymorphism
- Binding in C
- 2. Run-time Binding
- Binding is done during execution of your program.
- The method of the pointing object will be
invoked. - i.e.
- Grandpaprint() will be called for
a0-gtprint() - Papaprint() will be called for a1-gtprint()
- Meprint() will be called for a2-gtprint()
- It is known as Polymorphism.
6Polymorphism
- How to Use Polymorphism?
- Make use of virtual methods. (Run-time Binding)
main() One one Two two Three three One
array3 array0 one array1 two
array2 three for(int i0ilt3i)
arrayi-gtwhoami()
7Polymorphism
One / Onewhoami() is invoked / Two /
Twowhoami() is invoked / Three /
Threewhoami() is invoked /
main() Two two Three three Two array2
array0 two array1 three for(int
i0ilt2i) arrayi-gtwhoami()
Result
Two Three
Since the virtual Onewhoami() is inherited from
One to Two. i.e. class Two also has a virtual
whoami() method.
8Polymorphism
- Remarks on Using Polymorphism
- Consider the following case
main() Grandpa grandpa Papa papa Me me
Grandpa family3 family0 grandpa
family1 papa family2 me for(int
i0ilt3i) familyi-gteat()
familyi-gtearn() familyi-gtplay()
ERROR !! Since no earn and play methods in
Grandpa.
9Polymorphism
- Error can be eliminated by declaring all the
virtual method in the base class.
class Me public Papa public void eat()
void earn() void play()
However, all the 3 classes will have the 3
methods even Grandpa does not earn and play.
10Name Hiding
- Members in the base class will be hidden by the
redefined members in the derived class. - Name hiding can occur with virtual as well as
nonvirtual methods - Example
int main( ) B b1 b1.print( ) //
OK b1.print(1) // Error
b1.Aprint(1) // OK return 0
Class A void print(int x)coutltltxltltendl
Class B public A void print(
)coutltltHiltltendl
11Abstract Base Class
- Used as based classes in inheritance situation
- No objects of an abstract base class can be
instantiated - Class is made abstract by declaring one or more
of its virtual functions to be pure - Example of pure virtual function
virtual double earnings( ) const 0 // pure
virtual
12Friend function
- C is very class conscious
- nonmember functions are barred from accessing
private members - to access the private members of a class, one
must either be - a member functions, or
- a non-member function, but must be a friend of
the class
13Friend function
- class JockeyClub
- private
- int swimming_pool
- string _name
- friend int foo( ) //foo( ) is a
friend - friend int AClassg( ) //only g( ) in
class A class is a friend - friend int BClass //all functions in B
class are friends - public
- . . .
-
- void foo()
- JockeyClub aClub
- aClub._name Hong Kong Jockey Club //OK
14Operator Overloading
- Why operator overloading?
- 1. Use same operator to deal with different data
type within a object. - 2. The overload operator can replace the
operation of original operator - Operators that can be overloaded
- Operators that cannot be overloaded
15Operator Overloading
- Class Vector
-
- private
- double x // horizontal value
- double y // vertical value
- public
- Vector(double h) x h y 0
// set x value - Vector(double h, double v) x h y v //
set x,y value - Vector operator(Vector b) // 1 argument only
and this argument is represent - Vector operator (double n) // the data on
the right side of operator -
- Vector Vectoroperator(Vector b)
-
- double sx, sy
- sx x b.x // x component of sum
- sy y b.y // y component of sum
- Vector sum Vector(sx,sy)
- return sum
16Operator Overloading
- The overloaded operator can be called by 2 way
- Vector v1(1,1), v2(2,2)
- Vector v3 v1.operator ( v2 ) // function
call syntax - or
- Vector v3 v1 v2 // alternative syntax
-
- Vector v4 v1 10 // OK
uses convert constructor - Vector v4 10 v1 // Error first arg
not a Vector object
17Operator Overloading
- Vector Vectoroperator (double n)
-
- double mx, my
- mx n x // x component of multiple
- my n y // y component of multiple
- Vector mult Vector(mx,my)
- return mult
-
- Vector A(3,3)
- Vector b A 2.0 // Valid
- Vector b A.operator (2.0) // Valid
- Vector b 2.0 A // Not Valid
18Constraints in Operator Overloading
- An operator must either be overloaded as method
or have at least one class object among its
arguments (Except for the memory management
operators, e.g. new, delete) - Operators ( ) -gt must be overloaded as a
member function (the leftmost operand must be a
class object of the operator class) - If leftmost operand must be an object of
different class, this operator function must be
implemented as a non-member function (it the
private and protected members needed to be
accessed directly, it needs to be a friend)
19Example of friend and operator overloading
- include ltiostreamgt
- class phone
- friend ostream operatorltlt( ostream, const
phone ) - private
- char line9
-
- ostream operatorltlt (ostream output, const phone
num) - outputltlt num.line ltltendl
- return output
-
-