III.
Function Overloading
& Default Argument
PBO III. Function & Operator Overloading
Function Overloading (FO)
The usage of the same function names to
perform different action
Example:
class Tcar Same “function name”
{ but
private:
char _Name[50]; Different
int _Gasoline_in_tank;“Argument Type”
public:
void set (const char* Name);
void set (const int Gas_in_tank);
};
PBO III. Function & Operator Overloading
FO Background: – Natural Func. Call
Function is a name of action
Problem in mapping of action-statement
in Human Language (HL) onto a
Programming Language (PL)
Example of action statement:
HL: Wash the dish, Wash the car
Old PL (C/Pascal) :
Wash1(the_dish), Wash2(the_car)
Wash_dish(the_dish), Wash_car(the_car) Unnatural
Washd(the_dish), Washc(the_car)
With C++ (FO):
Wash (the_dish), wash (the_car)
PBO III. Function & Operator Overloading
Background 2 – Constructor Needs
Rule: Constructor must have the same name
with the name of the class
If the overloading is not available then it
means only ONE constructor can be created.
If only one constructor available then it would
be impossible for users to initialize object in
several ways.
Conclusions: To be able to initialize object in
several ways, it is required more than one
constructor. To be able to create more than
one constructor then it is required Function
Overloading.
PBO III. Function & Operator Overloading
When will 2 functions with the same name will
be considered different by C++?
1. If the number of arguments are
different.
2. If the type of the arguments are
different.
PBO III. Function & Operator Overloading
Which functions are equal?
1. void draw(char shape, int n=1);
2. int draw(char shape, int n);
3. void draw(char shape2, int x);
4. void draw(char shape);
5. int draw(int shape, int n);
6. int draw(int shape);
PBO III. Function & Operator Overloading
Default Argument
Is the default value set to some arguments of
the member function, when these arguments
are not supplied with value.
Example:
Member function with default argument:
void set(const char *name, const
int GIT=40);
Can be called:
Car1.set(“Sephia”, 100);
Car2.set(“Peugeot”);
PBO III. Function & Operator Overloading
The Purpose of D.A.
Shorten the calling to the function with
lots of arguments
Reduce the number of overloading
functions (it is more appropriate to use
default argument for functions with the
similar action but different number of
argument)
PBO III. Function & Operator Overloading
Example
Overloading Function
//display a letter once
void display(char letter);
//display a letter t times
void display(char letter, int t);
Better implementation with Default
Argument
//display a letter t times. If t is not
//supplied with value, then it will
//display the letter just once.
void display(char letter, int t=1);
PBO III. Function & Operator Overloading
Some notes for D.A.
Default Argument are only placed in
the declaration of a function. You don’t
need to type the D.A. in the
implementation.
D.A. can be given to the arguments
from the most right side to the left one.
Watch out that two or more different
overloading function, may now become
equal if the D.A. is applied. However
the error will appear only when any of
these the member functions are used.
PBO III. Function & Operator Overloading
30 Minutes Exercise
1. Change the member function of your Tcomplex
class (see pre-course assignment for week 4) to
use the Function Overloading and Default
Argument.
2. Try creating 2 function that actually the same if
the default argument is used (see explanation in
classroom)
3. Try your new class’ member function in the main
program (incl the member functions created in
the #2)
PBO III. Function & Operator Overloading
Exercise : Complex Class
class Complex {
public:
// constructor
Complex (float r, float i);
// arithmetic
Complex add(Complex rhs);
Complex subtract(Complex rhs);
Complex multiply(Complex rhs);
Complex divide(Complex rhs);
void set_value(Complex c);
// leave the data members public in this example
float real;
float imaj;
};
PBO III. Function & Operator Overloading
Operator Overloading
Instead of using word, it is possible to use operator
as a function name
The basic reason is that the listing easier to write
and to understand
Only predefined operator can be overloaded (such
as: +, -, *, /, %,+=, -=, *=, /*, %, ^,<, >, ==,
!=, <<, >>, ~, ! etc)
The following operator can not be overloaded
(. , .*, :: , ?:)
The hierarchy of the operator can not be changed
(for ex: x = a + b * c will do the multiplication,
addition and finally the assignment)
PBO III. Function & Operator Overloading
at3 = at1 + at2;
or
at3.sum(at1, at2);
Everything you can do with an overloaded operator
you can also do with a function.
at3= at1.operator+(at2)
at3= at1 + at2
PBO III. Function & Operator Overloading
Example:
// Function name with ordinary word
class Tcomplex
{
double _Real, _Imaginary;
public:
Tcomplex add(Tcomplex number);
};
// Function name with overloading operator
class Tcomplex
{
double _Real, _Imaginary;
public:
Tcomplex operator+(Tcomplex number);
};
PBO III. Function & Operator Overloading
//Function call for ordinary function name
void main()
{
Tcomplex complex1;
complex3 = complex1.add(complex2);
}
PBO III. Function & Operator Overloading
Using Operator Overloading
class Complex{
private:
double real;
double imag;
public:
Complex(double re,double im){
real = re;
imag = im;
}
Complex operator+(Complex other){
double result_real = real + other.real;
double result_imaginary = imag + other.imag;
return Complex(result_real,
result_imaginary);
}
void print(){
printf("%lf %lf", real, imag);
}
};
PBO III. Function & Operator Overloading
void main(){
Complex a(1,2);
Complex b(2,2);
Complex c = a+b;
c.print();
}
PBO III. Function & Operator Overloading
Create CLOCK class with this criteria :
Constructor CLOCK (h,m,s)
Implement operator + and -
Print the clock, format :
hh:mm:ss if the hh and mm is not zero
mm:ss if the mm is not zero
ss if the hh and mm is zero
Test the CLOCK class in the void main, use the + and –
Clock c1(12,40,55); //12:40:55
Clock c2(4,55,14); //04:55:14
Clock C3 = c1+c2;
C3.print(); //17:36:09
Clock C4 = c1-c2;
C4.print(); //07:45:41
Kumpulkan ke kristo@petra.ac.id Maks 15/09/14
Format : PBO_B_Clock_nrp.zip