//Surrogate.h
#include
using namespace std;
class Vihicle
{
public:
virtual void Start()=0;
virtual Vihicle* Copy() const;
virtual double Weight() const ;
virtual ~Vihicle(){}
};
//代理类
class VihicleSurrogate
{
public:
VihicleSurrogate():m_vp(0){}
VihicleSurrogate(const Vihicle &vp):m_vp(vp.Copy()){}
VihicleSurrogate(const VihicleSurrogate &vs)
:m_vp(vs.m_vp ? vs.m_vp->Copy() : 0 ){}
VihicleSurrogate & operator=(const VihicleSurrogate &vs)
{
//if( vs != *this )
//{
// delete m_vp;
//}
m_vp=vs.m_vp->Copy () ? vs.m_vp->Copy() : 0 ;
}
void Start()const
{
m_vp->Start();
}
private:
Vihicle *m_vp;
};
//派生类
class Truck : public Vihicle
{
public:
Truck():m_weight(0){}
Truck(const Truck &t):m_weight(t.m_weight){}
void Start()const;
Vihicle * Copy()const;
private:
int m_weight;
};
Vihicle* Truck::Copy () const
{
return new Truck(*this);
}
void Truck::Start () const
{
cout<<"Truck make"<}
#include"Surrogate.h"
int main()
{
VihicleSurrogate vs[10];
Truck t;
vs[0]=t;
vs[0].Start();
return 0 ;
}
有什么问题啊
怎么改啊
--------------------next---------------------
//Surrogate.h
#include ---------------------------------这句应该这么写:#include
using namespace std;------------------------你用的什么编译器,这句话是什么意思啊,我的编译器不认识;
class Vihicle
{
public:
virtual void Start()=0;---------------------这里是什么意思呢?是否应该去掉“=0”;
virtual Vihicle* Copy() const;
virtual double Weight() const ;
virtual ~Vihicle(){}
};
//代理类
class VihicleSurrogate
{
public:
VihicleSurrogate():m_vp(0){}
VihicleSurrogate(const Vihicle &vp):m_vp(vp.Copy()){}
VihicleSurrogate(const VihicleSurrogate &vs)
:m_vp(vs.m_vp ? vs.m_vp->Copy() : 0 ){}
VihicleSurrogate & operator=(const VihicleSurrogate &vs)----------这个函数需要返回值,加上return 0;提示信息是:
{ 等号两边的数据类型不同的,不能强行转换;
//if( vs != *this )
//{
// delete m_vp;
//}
m_vp=vs.m_vp->Copy () ? vs.m_vp->Copy() : 0 ;
}
void Start()const
{
m_vp->Start();
}
private:
Vihicle *m_vp;
};
//派生类
class Truck : public Vihicle
{
public:
Truck():m_weight(0){}
Truck(const Truck &t):m_weight(t.m_weight){}
void Start()const;
Vihicle * Copy()const;
private:
int m_weight;
};
Vihicle* Truck::Copy () const
{
return new Truck(*this);
}
void Truck::Start () const
{
cout<<"Truck make"<}
#include"Surrogate.h"--------------------------------这个调用的什么函数?我的编译器也不认识;你最好把这句写在顶端!
int main()
{
VihicleSurrogate vs[10];
Truck t;
vs[0]=t;
vs[0].Start();
return 0 ;
}
你最好把题目一起发上来,负责很难看懂得。;
--------------------next---------------------
我用的是vc++6.0
想输出 "Truck make"
用 VihicleSurrogate 来管理和控制 vihicle 及其子类
这样的
//Surrogate.h
#include
using namespace std;
class Vihicle
{
public:
virtual void Start()const = 0;
virtual Vihicle* Copy() const = 0;
virtual double Weight() const = 0 ;
virtual void SetWeight(int weight) = 0;
virtual ~Vihicle(){}
};
//代理类
class VihicleSurrogate
{
public:
VihicleSurrogate():m_vp(0){}
VihicleSurrogate(const Vihicle &vp):m_vp(vp.Copy()){}
VihicleSurrogate(const VihicleSurrogate &vs)
:m_vp(vs.m_vp ? vs.m_vp->Copy():0 ){}
VihicleSurrogate & operator=(const VihicleSurrogate &vs)
{
if( &vs != this )
{
delete m_vp;
}
m_vp=vs.m_vp->Copy () ? vs.m_vp->Copy() : 0 ;
return *this;
}
void Start()const
{
m_vp->Start();
}
double Weight() const
{
return m_vp->Weight();
}
void SetWeight(int w)
{
m_vp->SetWeight(w);
}
private:
Vihicle *m_vp;
};
//派生类
class Truck : public Vihicle
{
public:
Truck():m_weight(0){}
Truck(const Truck &t):m_weight(t.m_weight){}
void Start()const;
Vihicle * Copy()const;
double Weight() const;
void SetWeight(int weight);
private:
int m_weight;
};
//Truck的实现
Vihicle* Truck::Copy () const
{
return new Truck(*this);
}
void Truck::Start () const
{
cout<<"Truck make"<}
double Truck::Weight () const
{
return m_weight ;
}
void Truck::SetWeight (int weight)
{
m_weight = weight;
}
//main.cpp
#include"Surrogate.h"
int main()
{
VihicleSurrogate vs[10];
Truck t;
vs[0]=t;
vs[0].Start();
vs[0].SetWeight (20);
cout< return 0 ;
}
结果
Truck make
10
--------------------next---------------------
阅读(1205) | 评论(0) | 转发(0) |