"=" 重载,如何深度copy
#include <iostream>
using namespace std;
class vehicle
{
public:
vehicle()
{
mMaxSpeed=0;mMaxWeight=0;num=0;
cout<<"vehicle constructor"<<endl;
}
vehicle(int sp,int we,int n)
{
mMaxSpeed=sp;mMaxWeight=we;num=n;
}
~vehicle(){cout<<"vehicle unconstructor"<<endl;}
vehicle(const vehicle& vv)
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
cout<<"vehicle copy-constructor"<<endl;
}
vehicle& operator =(const vehicle& vv)//"="重载
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
return *this;
}
virtual int getMaxSpeed(){cout<<"get Maxspeed vehicle\n";return mMaxSpeed;}
virtual int getMaxWeight(){cout<<"get MaxWeight vehicle\n";return mMaxWeight;}
virtual void setNum(int n){cout<<"set num vehicle\n";num=n;}
virtual void setMaxSpeed(int speed){cout<<"set Maxspeed vehicle\n"; mMaxSpeed=speed;}
virtual void setMaxWeight(int weight){cout<<"set MaxWeight vehicle\n"; mMaxWeight=weight;}
protected:
int mMaxSpeed;
int mMaxWeight;
private:
int num;
};
class car :public vehicle
{
public:
car()
{
type=0;
cout<<"car constructor\n";
}
car(const car& cc):vehicle(cc)
{
/*mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;*/
type=cc.type;
cout<<"car copy constructor!"<<endl;
}
car& operator =(const car& cc)//如何实现vehicle类中 的private 变量num赋值?????
{
//
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
return *this;
}
private:
int type;
};
int main()
{
//test vehicle the base class;
vehicle v;
vehicle v1(v);
vehicle v2;
v2.setMaxSpeed(33);
v2.setMaxWeight(55);
vehicle v3=v2;
vehicle v4(v2);
int speed=v2.getMaxSpeed();
int weight=v2.getMaxWeight();
v=v2;
//////////////////////test over!
car c;
car c1;
c.setMaxSpeed(12);
c.setMaxWeight(33);
c.setNum(11);
c1=c;
car c2(c);
car c3=c;
car c4;
c4=c;
}
...全文
相关推荐
struct2官方api,很详细,可离线
struct2的入门和api解释,入门必看
高质量C++/C编程指南
高质量C++/C编程指南:林锐 博士
面向对象程序设计英文教学课件:08 Copy Control.ppt
面向对象程序设计英文教学课件:08 Copy Control.ppt
QImage 类翻译
学习Qt的第一课就是各种显示图片的问题,使用的比较多个人先从翻译开始的。现在学会了,把以前翻译的类给大家分享。让英语不好的人可以直接看懂如何使用
Luabind 使用手册 中文版 高清PDF v0.9.1
Luabind 使用手册(http://www.rasterbar.com/products/luabind/docs.html) 中文版 高清PDF 一年前传过一个当时Luabind版本才0.8,后来出了0.9.1,今天有空了就更新了一下。有些语句意思是懂了,但却不知道怎么用中文表达,可能不好理解。


