#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun(int num=10)
{
cout<<"i'm base fun with "<<num<<endl;
}
void fun1()
{
cout<<"i'm base fun with "<<0<<endl;
}
};
class Derived :public Base
{
public:
//fun是函数重写,实现多态,
virtual void fun(int num=20)
{
cout<<"i'm derived fun with "<<num<<endl;
}
void fun1()
{
cout<<"i'm derived fun with "<<1<<endl;
}
//本fun1是另一个fun1的函数重载
void fun1(int num)
{
cout<<"i'm derived fun with "<<num<<endl;
}
int fun1(int num)
{
cout<<"i'm derived fun with return "<<num<<endl;
return num;
}
};
int main(int argc, char **argv)
{
Derived d;
Base &b = d;
b.fun1();
d.fun1();
Base *pb = &d;
pb->fun1();
((Derived*)pb)->fun1();
d.fun1(10);
system("pause");
return 0;
}
类函数重写、重载、覆盖示例
最新推荐文章于 2023-08-25 17:38:35 发布