motorbye C++类程序实现问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 20:13:56
#include<iostream>
using namespace std;
class v
{
protected:
int m,w;
public:
void run(){cout<<"run is open";}
void stop(){cout<<"stop is open";}
v(int m1=0,int w1=0){m=m1,w=w1;
};
class b:virtual public v
{
protected:
int h;
public:
b();
};
class m:virtual public v
{
int s;
public:
m();

};
class motor:public m,public b
{
public:
motor()
{

cout<<m;
}
};
void main()
{
class v e;
class b pp;
class m pp1;
class motor yy;
}
这个程序说moto类不能访问m不是都设成了虚基类了吗?问题出在哪》怎么改??

改好了, 大致上有这么几个问题:
1. 类型v少了个}, 导致类型定义不完整; 不知道是不是你复制丢了?
2. 类型m和v的变量m发生命名冲突; 我把类型名全部改成大写了
3. 类型m和类型b缺少构造函数体; 各加一对{}就好了

#include<iostream>
using namespace std;
class V
{
protected:
int m,w;
public:
void run(){cout<<"run is open";}
void stop(){cout<<"stop is open";}
V(int m1=0,int w1=0){m=m1,w=w1;
}
};

class B:virtual public V
{
protected:
int h;
public:
B(){}
};
class M:virtual public V
{
int s;
public:
M(){}
};
class Motor:public M,public B
{
public:
Motor()
{

cout<< m;
}
};
void main()
{
V e;
B pp;
M pp1;
Motor yy;
}