#include
<iostream>
using
namespace
std;
class
Base
{
public
:
virtual
void
print() {
//如果不加virtual,输出的全是基类
cout <<
"print() in Base."
<< endl;
}
void
print(
int
a
) {
cout <<
"print(int a) in Base."
<< endl;
}
void
print(
string
s
) {
cout <<
"print(string s) in Base."
<< endl;
}
};
class
Derived
:
public
Base
{
public
:
void
print() {
cout <<
"print() in Derived."
<< endl;
}
};
int
main() {
Base
b;
Derived
d;
Base
* bp = &d;
b.print();
//调用的是基类的print()
b.print(8);
b.print(
"a"
);
bp->print();
//调用的是派生类的print()
bp->print(10);
bp->print(
""
);
d.print();
//调用的是派生类的print()
system(
"pause"
);
return
0;
}