C++多态与继承,指针与虚函数。简单的题,求指教。

caosz 发布于 2013/05/29 15:14
阅读 184
收藏 0
#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的函数显示呀?为毛会说有歧义。
加载中
0
Ethan丶Lee
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;
}
caosz
caosz
我把run和stop设为虚函数之后就更好了。
0
kiwivip
kiwivip
刚毕业那会老复习这个,现在忘光了...
0
Ethan丶Lee
Ethan丶Lee
虚基类,虚继承问题,回去翻课本吧。
0
Ethan丶Lee
Ethan丶Lee
多重继承虚基类。
0
caosz
caosz

引用来自“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;
}
返回顶部
顶部