Created
April 12, 2013 14:54
-
-
Save GianpaMX/5372612 to your computer and use it in GitHub Desktop.
How to ovearload functions in C++ and PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class A { | |
public: | |
void f(const string &arg1, const string &arg2) { | |
cout << arg1 << ", " << arg2 << endl; | |
} | |
}; | |
class B : public A { | |
public: | |
void f(const string &arg1) { | |
A::f(arg1, "I'm B"); | |
} | |
}; | |
int main() { | |
A a; | |
a.f("Hello", "World"); | |
B b; | |
b.f("foo"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A { | |
public function f() { | |
$args = func_get_args(); | |
$arg1 = $args[0]; | |
$arg2 = $args[1]; | |
echo "$arg1, $arg2\n"; | |
} | |
} | |
class B extends A { | |
public function f() { | |
$args = func_get_args(); | |
$arg1 = $args[0]; | |
parent::f($arg1, "I'm B"); | |
} | |
} | |
$a = new A(); | |
$a->f("Hello", "World"); | |
$b = new B(); | |
$b->f("foo"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment