![]() |
#2
promising2010-08-11 13:08
|

#include <iostream>
using namespace std;
class A
{
public:
int a;
void display( );
};
class B
{
public:
int a;
void display( );
};
class C :public A,public B
{
public:
int b;
void show( );
};
int main( )
{
int m,n;
C c1;
c1.A::a = 3;
c1.B::a = 8;
c1.A::display( );
c1.B::display( );
return 0;
}
上面的程序是谭浩强《C++程序设计》一书中的举例(P376页),按书中所说,这个程序应该可以运行,但实际上不能通过编译,问题就出在倒数的第三行(倒数第二行性质一样),我真不理解对象 c1 能访问基类数据,用同样的方法对象 c1 却不能访问基类的函数,虽然是同名函数,但是已经指明了所属的类呀。为什么?using namespace std;
class A
{
public:
int a;
void display( );
};
class B
{
public:
int a;
void display( );
};
class C :public A,public B
{
public:
int b;
void show( );
};
int main( )
{
int m,n;
C c1;
c1.A::a = 3;
c1.B::a = 8;
c1.A::display( );
c1.B::display( );
return 0;
}