代码哪里错了
#include<iostream>using namespace std;
class annimal
{
public:
void age()
{
m_Age = 11;
}
int m_Age;
};
class sheep:public annimal
{
};
class tuo :public annimal
{
};
class sheeptuo :public sheep, public tuo
{
};
void test01()
{
sheeptuo st;
cout << st.sheep::m_Age << endl;
cout << st.tuo::m_Age << endl;
}
int main()
{
test01();
return 0;
} 在代码中并没有明确调用age()函数来设置m_Age的值,因此需要在test01()函数中调用age()函数来设置m_Age的值。修改后的代码如下:
#include<iostream>
using namespace std;
class annimal
{
public:
void age()
{
m_Age = 11;
}
int m_Age;
};
class sheep:public annimal
{
};
class tuo :public annimal
{
};
class sheeptuo :public sheep, public tuo
{
};
void test01()
{
sheeptuo st;
st.sheep::age();
st.tuo::age();
cout << st.sheep::m_Age << endl;
cout << st.tuo::m_Age << endl;
}
int main()
{
test01();
return 0;
}
页:
[1]