本文发表在 rolia.net 枫下论坛Try the following program, the visual C++(6.0 and .net) do not let it be compiled.
According to inheritance, overloading and overriding concepts,
b->display(a1) should bind to the b_class::display(a1);
b->play(a1) should bind to the b_class::play(a1); and
b->play(a2) should bind to the b_class::play(a2).
Do you agree?
Can you run it by GNU C++?
<pre>
#include <iostream>
using namespace std;
class A
{
public:
A(){x = 10, y =20;};
int x,y;
};
class B: public A
{
public:
B(){i = 10, j =20;};
int i,j;
};
class b_class
{public: virtual void display(A a)
{ cout<<"b_class, a= "<<a.x<<endl;
}
void play(A b)
{ cout<<"b_class, b = "<<b.y<<endl;
}
};
class d_class : public b_class
{public: void display(B f )
{ cout<<"d_class, f = "<<f.i<<endl;
}
void play(B g )
{ cout<<"d_class, g = "<<g.j<<endl;
}
};
void main()
{ b_class *a;
d_class *b, bo;
a = new b_class();
b = new d_class();
A a1,a2;
B b1,b2;
a->display(a1);
a->display(a2);
b->display(a1);//error?
a = b;
a->display(b1);
a->display(b2);
a->play(a1);
a->play(a2);
b->play(a1);//error?
b->play(a2);//error?
}
</pre>更多精彩文章及讨论,请光临枫下论坛 rolia.net
According to inheritance, overloading and overriding concepts,
b->display(a1) should bind to the b_class::display(a1);
b->play(a1) should bind to the b_class::play(a1); and
b->play(a2) should bind to the b_class::play(a2).
Do you agree?
Can you run it by GNU C++?
<pre>
#include <iostream>
using namespace std;
class A
{
public:
A(){x = 10, y =20;};
int x,y;
};
class B: public A
{
public:
B(){i = 10, j =20;};
int i,j;
};
class b_class
{public: virtual void display(A a)
{ cout<<"b_class, a= "<<a.x<<endl;
}
void play(A b)
{ cout<<"b_class, b = "<<b.y<<endl;
}
};
class d_class : public b_class
{public: void display(B f )
{ cout<<"d_class, f = "<<f.i<<endl;
}
void play(B g )
{ cout<<"d_class, g = "<<g.j<<endl;
}
};
void main()
{ b_class *a;
d_class *b, bo;
a = new b_class();
b = new d_class();
A a1,a2;
B b1,b2;
a->display(a1);
a->display(a2);
b->display(a1);//error?
a = b;
a->display(b1);
a->display(b2);
a->play(a1);
a->play(a2);
b->play(a1);//error?
b->play(a2);//error?
}
</pre>更多精彩文章及讨论,请光临枫下论坛 rolia.net