#include <iostream>
using namespace std;
class A
{
private:
int a;
public:
A(int i) : a(i) { }
virtual void print( )
{
cout<<"AAAA\n";
cout << a << endl;
}
int get_a( )
{
return a;
}
};
class B
{
private:
int b;
public:
B(int j) : b(j) { }
void print( )
{
cout<<"BBBB\n";
cout << b << endl;
}
int get_b( )
{
return b;
}
};
class C : public A, public B
{
int c;
public:
C(int i, int j, int k) : A(i), B(j), c(k) { }
void print( )
{
cout<<"CCC\n";
A::print( );
B::print( );
}
// use print( ) with scope resolution
void get_ab( )
{
cout << get_a( ) << " " << get_b( ) << endl;
}
// use get_a( ) and get_b( ) without scope resolution
};
int main( )
{
C x(5, 8, 10);
A* ap = &x;
B* bp = &x;
C* cp = &x;
ap -> print( ); // use C::print( ); //???????????????????????/
cout<<"!!!!!!!!!!!!!!!!!!!!!\n";
bp -> print( ); // use B::print( );
cp -> print();
cout<<"------------------\n";
// bp -> A::print( ); // as if x is inherited from B only,
// cannot access A::print( );
x.A::print( ); // use A::print( );
cout<<"~~~~~~~~~~~~~~~~~~~~\n";
x.get_ab( );
return 0;
}
不明白為什么上面//????的地方是呼叫C::print();而下一句確實B::print.
求高手指點。
uj5u.com熱心網友回復:
這是多型啊.轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/102993.html
標籤:基礎類
