一道c++题目求大佬解释一下为什么是这个结果
#include<iostream>usingnamespacestd;classA{public:A(constchar*s){cout<<s<<endl;}~A(){...
#include<iostream>
using namespace std ;
class A
{ public :
A(const char *s) { cout << s << endl ; }
~A(){}
};
class B : virtual public A
{ public :
B(const char *s1, const char *s2) : A( s1 )
{ cout << s2 << endl ; }
};
class C : virtual public A
{ public :
C(const char *s1, const char *s2):A(s1)
{ cout << s2 << endl ; }
};
class D : public B, public C
{ public :
D( const char *s1, const char *s2, const char *s3, const char *s4 )
: B( s1, s2 ), C( s1, s3 ), A( s1 )
{ cout << s4 << endl ; }
};
int main()
{ D *ptr = new D( "class A", "class B", "class C", "class D" ) ;
delete ptr;
}
【解答】
class A
class B
class C
class D 展开
using namespace std ;
class A
{ public :
A(const char *s) { cout << s << endl ; }
~A(){}
};
class B : virtual public A
{ public :
B(const char *s1, const char *s2) : A( s1 )
{ cout << s2 << endl ; }
};
class C : virtual public A
{ public :
C(const char *s1, const char *s2):A(s1)
{ cout << s2 << endl ; }
};
class D : public B, public C
{ public :
D( const char *s1, const char *s2, const char *s3, const char *s4 )
: B( s1, s2 ), C( s1, s3 ), A( s1 )
{ cout << s4 << endl ; }
};
int main()
{ D *ptr = new D( "class A", "class B", "class C", "class D" ) ;
delete ptr;
}
【解答】
class A
class B
class C
class D 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询