问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数operator=,以实现深层复制。
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//CDeepCopy.h
#include<iostream>
#include<string>
using namespace std;
class CDeepCopy

public:
int n; //动态数组的元素个数
int*p; //动态数组首地址
CDeepCopy(int);
~CDeepCopy();
CDeepCopy& operator=(const CDeepCopy&r);//赋值运算符函数
;
void writeToFile (char*);
//main.cpp
#include"CDeepCopy.h"
CDeepCopy::~CDeepCopy()delete[]p;
CDeepCopy::CDeepCopy(int k)n=k;p=new int[n];)//构造函数实现
CDeepCopy& CDeepCopy::operator=(const CDeepCopy&r)
//赋值运算符函数实现

//**********333**********
//**********666**********

int main()

CDeepCopy a(2),d(3);
a.p[0]=1;d.p[0]=666;//对象a,d数组元素的赋值

CDeepCopy b(3);
a.p[0]=88;b=a;//调用赋值运算符函数
cout<<b.p[0];//显示内层局部对象的数组元素

cout<<d.p[0];//显示d数组元素a.p[0]的值
cout<<"d fade away;\n";
cout<<a.p[0];//显示a数组元素a.p[0]的值
writeToFile(" ");
return 0;

【参考答案】

n=r.n; //把对象r字符长度赋值给n
delete[]p; //删除动态数组p
p=new i......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 80 150 100 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。 #include <iostream.h> class vehicle private: int MaxSpeed; int Weight; public: **********found********** vehicle (int maxspeed,intweight):______ ~vehicle(); int getMaxSpeed()return MaxSpeed; int getWeight()return Weight; ; **********found********** class bicycle:______public vehicle private: int Height; public: bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height) int getHeight()return Height;; ; **********found********** class motorcar:______public vehicle private: int SeatNum; public: motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum) int getSeatNum()return SeatNum;; ; **********found********** class motorcycle:______ public: motorcycle(int maxspeed,intweight,int height):vehicle (maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1) ; void main () motorcycle a(80,150,100); cout<<a.getMaxSpeed()<<endl; cout<<a.getWeight()<<endl; cout<<a.getHeight()<<endl; cout<<a.getSeatNum()<<endl;