基类的函数能够访问派生类的函数吗或者派生类继承基类的函数能够访问派生类的虚函数吗?解决办法
基类的函数能够访问派生类的函数吗或者派生类继承基类的函数能够访问派生类的虚函数吗?
请各位侠客基类的函数能够访问派生类的函数吗或者派生类继承基类的函数能够访问派生类的虚函数吗?
帮忙看下下面这个例子,怎么派生类继承基类的函数在调用时调用的是派生类的虚函数啊,基类的函数不是不可以调用派生类的函数吗?
class TestPaper
{
public:
void TestQuestion1()
{
cout<<"杨过得到玄铁,后来给了郭,炼成倚天剑,请问玄铁可能为() a 球磨矿, b 马口铁 c 高速合金钢, d 碳素纤维"<<endl;
cout<<"答案是"<<answer1()<<endl;
}
void TestQuestion2()
{
cout<<"杨过,程英,陆无双铲除了情花,造成了,() a 这种植物不再害人, b 使一种珍惜植物灭绝 c 破坏了生态平衡, d 造成该地区沙漠化"<<endl;
cout<<"答案是"<<answer2()<<endl;
}
void TestQuestion3()
{
cout<<"蓝凤凰致使华山师徒,桃谷六仙呕吐不止,如果你是医生,请问你要用什么药() a 阿斯匹林, b 牛黄解毒片 c 氲气, d 牛奶"<<endl;
cout<<"答案是"<<answer3()<<endl;
}
virtual ~TestPaper();
protected:
TestPaper();
virtual char answer1()=0;
virtual char answer2()=0;
virtual char answer3()=0;
};
class TestPaperA:public TestPaper
{
public:
TestPaperA();
~TestPaperA();
protected:
char answer1(){return 'b';}
char answer2(){return 'a';}
char answer3(){return 'c';}
};
class TestPaperB:public TestPaper
{
public:
TestPaperB();
~TestPaperB();
protected:
char answer1(){return 'c';}
char answer2(){return 'b';}
char answer3(){return 'a';}
};
int main()
{
cout<<"学生A的试卷"<<endl;
TestPaper *p1=new TestPaperA();
p1->TestQuestion1();
p1->TestQuestion2();
p1->TestQuestion3();
cout<<"学生B的试卷"<<endl;
TestPaper *p2=new TestPaperB();
p2->TestQuestion1();
p2->TestQuestion2();
p2->TestQuestion3();
return 0;
}
------解决方案--------------------
你真的明白你在问什么吗?
这个例子属于“基类的函数调用派生类的函数”吗?
所以说你理解错了.
class TestPaper
{
public:
void TestQuestion1()
{
...
cout<<"答案是"<<answer1()<<endl;
}
virtual char answer1()=0;
};
这个 answer1 定义在基类里,它就是基类的函数,基类当然可以调用。
只不过 virtual 这个描述给了派生类重新实现这个函数的权利。
如果
class A()
{
void m1(){ B::m2();}
};
class B:A
{
void m2();
};
这种情况才属于“基类的函数调用派生类的函数”,是非法的。
请各位侠客基类的函数能够访问派生类的函数吗或者派生类继承基类的函数能够访问派生类的虚函数吗?
帮忙看下下面这个例子,怎么派生类继承基类的函数在调用时调用的是派生类的虚函数啊,基类的函数不是不可以调用派生类的函数吗?
class TestPaper
{
public:
void TestQuestion1()
{
cout<<"杨过得到玄铁,后来给了郭,炼成倚天剑,请问玄铁可能为() a 球磨矿, b 马口铁 c 高速合金钢, d 碳素纤维"<<endl;
cout<<"答案是"<<answer1()<<endl;
}
void TestQuestion2()
{
cout<<"杨过,程英,陆无双铲除了情花,造成了,() a 这种植物不再害人, b 使一种珍惜植物灭绝 c 破坏了生态平衡, d 造成该地区沙漠化"<<endl;
cout<<"答案是"<<answer2()<<endl;
}
void TestQuestion3()
{
cout<<"蓝凤凰致使华山师徒,桃谷六仙呕吐不止,如果你是医生,请问你要用什么药() a 阿斯匹林, b 牛黄解毒片 c 氲气, d 牛奶"<<endl;
cout<<"答案是"<<answer3()<<endl;
}
virtual ~TestPaper();
protected:
TestPaper();
virtual char answer1()=0;
virtual char answer2()=0;
virtual char answer3()=0;
};
class TestPaperA:public TestPaper
{
public:
TestPaperA();
~TestPaperA();
protected:
char answer1(){return 'b';}
char answer2(){return 'a';}
char answer3(){return 'c';}
};
class TestPaperB:public TestPaper
{
public:
TestPaperB();
~TestPaperB();
protected:
char answer1(){return 'c';}
char answer2(){return 'b';}
char answer3(){return 'a';}
};
int main()
{
cout<<"学生A的试卷"<<endl;
TestPaper *p1=new TestPaperA();
p1->TestQuestion1();
p1->TestQuestion2();
p1->TestQuestion3();
cout<<"学生B的试卷"<<endl;
TestPaper *p2=new TestPaperB();
p2->TestQuestion1();
p2->TestQuestion2();
p2->TestQuestion3();
return 0;
}
------解决方案--------------------
你真的明白你在问什么吗?
这个例子属于“基类的函数调用派生类的函数”吗?
所以说你理解错了.
class TestPaper
{
public:
void TestQuestion1()
{
...
cout<<"答案是"<<answer1()<<endl;
}
virtual char answer1()=0;
};
这个 answer1 定义在基类里,它就是基类的函数,基类当然可以调用。
只不过 virtual 这个描述给了派生类重新实现这个函数的权利。
如果
class A()
{
void m1(){ B::m2();}
};
class B:A
{
void m2();
};
这种情况才属于“基类的函数调用派生类的函数”,是非法的。