#include
using namespace std;
int maxt(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int maxt(int a, int b,int c)
{
if (a > b && a > c)
{
return a;
}
else if (b > a && b > c)
{
return b;
}
return c;
}
/*这样声明会报错的
double maxt(int a, int b, int c)
{
if (a > b && a > c)
{
return a;
}
else if (b > a && b > c)
{
return b;
}
return c;
}
*/
class A
{
public:
void display()
{
cout << “A display()” << endl;
}
int maxt(int a)
{
cout << “A maxt()” << endl;
return a;
}
};
class B:public A
{
public:
void display()
{
cout << “B display()” << endl;
}
double maxt(int a)
{
cout << “B maxt()” << endl;
return a;
}
};
int main()
{
cout <<"maxt(5, 4)= "<<maxt(5, 4) << endl;
cout << "maxt(4, 5, 8)= "<<maxt(4, 5, 8) << endl;
B *b=new B();
A *a = b;
//类之间不是虚函数,函数名相同就是重写(覆盖)
b->display();
a->display();
b->maxt(5);
a->maxt(4);
system(“pause”);
return 0;
}
执行结果:
C++重载
最新推荐文章于 2024-01-24 17:26:37 发布