64,648
社区成员
发帖
与我相关
我的任务
分享
#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;
}
vehicle::operator =(cc);//调用基类的重载操作符,因为cc换个对象一定含有基类私有成员的值,但是不知道你这么做有和意义,基类的私有成员虽然被派生类集成了,但是是不可访问的,意义何在?
if(this==&cc)//判断是否为自己赋值
{
return *this;
}
#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 setNum(const vehicle& v){cout<<"set num vehicle\n";num=v.num;}
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;
private:
void do_copy(const vehicle& vv)
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
}
};
class car :public vehicle
{
public:
car()
{
type=0;
cout<<"car constructor\n";
}
car(const car& cc)
{
setNum(cc);
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
cout<<"car copy constructor!"<<endl;
}
car& operator =(const car& cc)//如何实现vehicle类中 的private 变量num赋值?????
{
//
setNum(cc);
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;
}
//基类的
vehicle& operator =(const vehicle& vv)//"="重载
{
if(this==&vv)//判断是否为自己赋值
{
return *this;
}
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
return *this;
}
//派生的
car& operator =(const car& cc)
{
vehicle::operator =(cc);//调用基类的重载操作符,因为cc换个对象一定含有基类私有成员的值,但是不知道你这么做有和意义,基类的私有成员虽然被派生类集成了,但是是不可访问的,意义何在?
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
return *this;
}
//最后就是如果成员中没有动态申请空间的数据,完全没有必要定义深度拷贝构造函数和重载=操作符
//一个类如果开始什么都没有,编译器会自动生成
1 默认构造函数 2 默认析构函数 3 默认重载=操作符 4 默认生成拷贝构造函数 5 默认当前对象地址this
不存在指针类成员,无需实现所谓的深COPY,换句话说vehicle v3=v2; v3与v2没有彼此依存的关系
class C
{
private:
void do_copy( const C& c );//just make this one perfect
......
public:
C(){}
C(const C& c){do_copy(c);}
C& operator = ( const C& c){do_copy(c);}
......
};