5
回答
C++多态与继承,指针与虚函数。简单的题,求指教。
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   
#include <iostream>
using std::cout;
using std::endl;
class vehicle 
{
public:
	void run() {cout<<"vehicle is running"<<endl;}
	void stop() {cout<<"vehicle is stopped"<<endl;}
};
class bicycle:public vehicle
{
public:
	void run() {cout<<"bicycle is running"<<endl;}
	void stop() {cout<<"bicycle is stopped"<<endl;}
};
class motorcar:public vehicle
{
public:
	void run() {cout<<"motorcar is running"<<endl;}
	void stop() {cout<<"motorcar is stopped"<<endl;}
};
class motorcycle:public bicycle,public motorcar
{
public:
	void run() {cout<<"motorcycle is running"<<endl;}
	void stop() {cout<<"motorcycle is stopped"<<endl;}
};
int main() {
	vehicle a;
	bicycle b;
	motorcar c;
	motorcycle d;
	vehicle * vehicles[]={&a,&b,&c,&d};
	for (int n=0;n<3;n++) {
		vehicles[n]->run();
		vehicles[n]->stop();
	}
	return 0;
}
lab8_2.cpp: 在函数‘int main()’中:
lab8_2.cpp:33:35: 错误: ‘vehicle’是‘motorcycle’的有歧义的基类
按照我的想法,应该是最终全部按照vehicle的函数显示呀?为毛会说有歧义。
<无标签>
举报
caosz
发帖于5年前 5回/179阅
共有5个答案 最后回答: 5年前
#include <iostream>
using std::cout;
using std::endl;
class vehicle 
{
public:
	void run() {cout<<"vehicle is running"<<endl;}
	void stop() {cout<<"vehicle is stopped"<<endl;}
};
class bicycle:virtual public vehicle
{
public:
	void run() {cout<<"bicycle is running"<<endl;}
	void stop() {cout<<"bicycle is stopped"<<endl;}
};
class motorcar:virtual public vehicle
{
public:
	void run() {cout<<"motorcar is running"<<endl;}
	void stop() {cout<<"motorcar is stopped"<<endl;}
};
class motorcycle:public bicycle,public motorcar
{
public:
	void run() {cout<<"motorcycle is running"<<endl;}
	void stop() {cout<<"motorcycle is stopped"<<endl;}
};
int main() {
	vehicle a;
	bicycle b;
	motorcar c;
	motorcycle d;
	vehicle * vehicles[]={&a,&b,&c,&d};
	for (int n=0;n<3;n++) {
		vehicles[n]->run();
		vehicles[n]->stop();
	}
	return 0;
}
--- 共有 1 条评论 ---
caosz我把run和stop设为虚函数之后就更好了。 5年前 回复

引用来自“Ethan.Lee”的答案

#include <iostream>
using std::cout;
using std::endl;
class vehicle 
{
public:
	void run() {cout<<"vehicle is running"<<endl;}
	void stop() {cout<<"vehicle is stopped"<<endl;}
};
class bicycle:virtual public vehicle
{
public:
	void run() {cout<<"bicycle is running"<<endl;}
	void stop() {cout<<"bicycle is stopped"<<endl;}
};
class motorcar:virtual public vehicle
{
public:
	void run() {cout<<"motorcar is running"<<endl;}
	void stop() {cout<<"motorcar is stopped"<<endl;}
};
class motorcycle:public bicycle,public motorcar
{
public:
	void run() {cout<<"motorcycle is running"<<endl;}
	void stop() {cout<<"motorcycle is stopped"<<endl;}
};
int main() {
	vehicle a;
	bicycle b;
	motorcar c;
	motorcycle d;
	vehicle * vehicles[]={&a,&b,&c,&d};
	for (int n=0;n<3;n++) {
		vehicles[n]->run();
		vehicles[n]->stop();
	}
	return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class vehicle 
{
public:
	virtual void run() {cout<<"vehicle is running"<<endl;}
	virtual void stop() {cout<<"vehicle is stopped"<<endl;}
};
class bicycle:virtual public vehicle
{
public:
	void run() {cout<<"bicycle is running"<<endl;}
	void stop() {cout<<"bicycle is stopped"<<endl;}
};
class motorcar:virtual public vehicle
{
public:
	void run() {cout<<"motorcar is running"<<endl;}
	void stop() {cout<<"motorcar is stopped"<<endl;}
};
class motorcycle:public bicycle,public motorcar
{
public:
	void run() {cout<<"motorcycle is running"<<endl;}
	void stop() {cout<<"motorcycle is stopped"<<endl;}
};
void fun1(vehicle * ptr) {
	ptr->run();
}
void fun2(vehicle * ptr) {
	ptr->stop();	
}
int main() {
	vehicle a;
	bicycle b;
	motorcar c;
	motorcycle d;
	fun1(&a);
	fun1(&b);
	fun1(&c);
	fun1(&d);
	fun2(&a);
	fun2(&b);
	fun2(&c);
	fun2(&d);
	return 0;
}
顶部