Download this source file
// Using declaration test: Shows overloading, overriding, and hiding.
// From an example in the ISO C++ standard.
// The effect of commenting out the various using declarations is to
// hide the inherited functions in all cases.
// (C) 2000 by Wayne Pollock, Tampa FL USA. All Rights Reserved.
#include
using namespace std;
struct B {
void f(int) { cout << "B::f(int)\n"; }
void f(char) { cout << "B::f(char)\n"; }
void g(int) { cout << "B::g(int)\n"; }
void h(int) { cout << "B::h(int)\n"; }
};
struct D : B {
using B::f;
void f(int) {cout << "D::f(int)\n";} // overrides B::f(int)
using B::g;
void g(char) {cout << "D::g(char)\n";} // overloads B::g(int)
using B::h;
void h(int) {cout << "D::h(int)\n";} // hides B::h(int)
};
int main () {
D* p = new D; // Quiz: what's the output if p is a B* pointer instead?
cout << "calling p->f( 7 ): "; p->f(7);
cout << "calling p->f('a'): "; p->f('a');
cout << "calling p->g( 7 ): "; p->g(7);
cout << "calling p->g('a'): "; p->g('a');
cout << "calling p->h( 7 ): "; p->h(7);
return 0;
};
#ifdef COMMENTED_OUT
C:\Temp>make using.exe
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
bcc32 using.cpp
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
using.cpp:
Warning W8004 using.cpp 29: 'p' is assigned a value that is never used in function main()
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
C:\Temp>using
calling p->f( 7 ): D::f(int)
calling p->f('a'): B::f(char)
calling p->g( 7 ): B::g(int)
calling p->g('a'): D::g(char)
calling p->h( 7 ): D::h(int)
C:\Temp>
#endif