[C++]8.3-8.4 析构函数,复制构造函数(深拷贝)(作业)
看到了就点个赞赞嘛~ ヾ(≧▽≦*)o~
第七周作业
(
*初学C++,想要把每周老师布置的实验题和自己写的程序记录下来,方便期末整理复习,请多多指教!
老师要求用的版本比较老,是Microsoft Visual C++ 2010 Express。
*)
一、面向对象编程
【题目要求】
- 按以下描述和要求建立一个类Bottle,完善类中所有成员函数的代码。执行主函数对其测试。按每个函数的完成情况得分。
私有成员
float radii; //瓶底圆半径 float high; //瓶高 char
color; //颜色(允许的取值:’R’、’B’、’W’)
int amount; //数量
char *use; //用途(如:酒瓶、奶瓶、饮料瓶)
公有成员
Bottle (float r=0, float h=0,char c=’ ’,int a=0, char *u=NULL); //带缺省值的构造函数。
Bottle (Bottle &b); //拷贝构造函数
~ Bottle (); //析构函数,释放分配的内存
float cubage ( ); //计算一个瓶子的容积(底面积乘高)
void setuse(char *s); //用s值修改瓶子用途
void display(); //输出瓶子信息(各数据成员的值)
- 头文件包含语句为:
#include <iostream.h>
#include <string.h>
- 测试数据:
对象A:5.3, 8.2, ‘R’, 20, “酒瓶” 对象B:2.6, 4.8, ‘W’, 30, “饮料瓶” 对象C:由对象A拷贝生成。
对象D:初值为缺省值。
- 测试程序的主函数为:
void main()
{
Bottle A(5.3, 8.2, ‘R’, 20, “酒瓶”), B(2.6, 4.8, ‘W’, 30, “饮料瓶”),C(A),D;
cout<<“半径”<<’\t’<<“瓶高”<<’\t’<<“颜色”<<’\t’<<“数量”<<’\t’<<“用途”<<endl<<endl;
A.display();
B.display();
C.display();
D.display();
cout<<“A瓶子的容积:”<<A.cubage()<<endl;
cout<<“修改C瓶子的用途。”<<endl;
C.setuse(“可口可乐”);
C.display();
}
【相关提示】包含<string.h>头文件可使用串复制函数strcpy(),求串长度函数strlen()。指针成员需要动态分配内存。
#include <iostream>
#include <cstring>
using namespace std;
#define PI 3.1415926
class Bottle
{
float radii,high;
char color;
int amount;
char *use;
public:
Bottle(float r=0, float h=0,char c=' ',int a=0, char const *u=NULL)
{
radii=r;
high=h;
color=c;
amount=a;
if(u)
{
use=new char[strlen(u)+1];
strcpy(use,u);
}
else use=" ";
}
Bottle (Bottle &b);
~ Bottle ();
float cubage ();
void setuse(char *s)
{
strcpy(use,s);
}
void display();
};
Bottle::Bottle(Bottle &b)
{
radii=b.radii;
high=b.high;
color=b.color;
amount=b.amount;
if(b.use)
{
use=new char[strlen(b.use)+1];
strcpy(use,b.use);
}else use=0;
}
Bottle::~Bottle()
{
if(use) delete []use;
}
float Bottle::cubage()
{
return PI*radii*radii*high;
}
void Bottle::display()
{
cout<<"瓶底圆半径="<<radii<<'\t'<<"瓶高="<<high<<'\t'<<"颜色:"<<color;
cout<<"数量="<<amount<<'\t'<<"用途:"<<use<<endl;
}
int main()
{
Bottle A(5.3, 8.2, 'R', 20, "酒瓶"), B(2.6, 4.8, 'W', 30, "饮料瓶"),C(A),D;
cout<<"半径"<<'\t'<<"瓶高"<<'\t'<<"颜色"<<'\t'<<"数量"<<'\t'<<"用途"<<endl<<endl;
A.display();
B.display();
C.display();
D.display();
cout<<"A瓶子的容积:"<<A.cubage()<<endl;
cout<<"修改C瓶子的用途。"<<endl;
C.setuse("可口可乐");
C.display();
getchar();
getchar();
}
二、面向对象编程
【题目要求】
- 实现一类对字符串的管理,类定义如下:
class String
{
private:
char* data; //字符数组指针
int size;
public:
String(int n); //分配n个大小的字符数组
String(String& str);
void diaplay(); //显示该动态字符数组
~String(); //析构函数,释放动态字符数组
void Add(); //添加一个字符的函数
};
请完成以上未定义的函数体。 - 自行定义主函数,确保每个函数(包括构造函数)至少被使用一次。
【相关提示】包含<string.h>头文件可使用串复制函数strcpy(),求串长度函数strlen()。指针成员需要动态分配内存。
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char* data;
int size,Num;
public:
String(int n)
{
Num=0;
size=n;
data=new char[n];
}
String(String &str)
{
Num=str.Num;
size=str.size;
data=new char[size];
int i;
for(i=0;i<Num;i++)
data[i]=str.data[i];
}
void display()
{
int i;
for(i=0;i<Num;i++)
cout<<data[i]<<'\t';
cout<<endl;
}
~String(){delete []data;}
void Add(char a)
{
char* d;
if(Num==size)
{
int i;
d=new char[size+4];
for(i;i<Num;i++)
d[i]=data[i];
delete []d;
size=size+4;
data=d;
data[Num++]=a;
}
else
data[Num++]=a;
}
};
void main()
{
String S1(10);
int i;
for(i=0;i<10;i++)
S1.Add(char(65+i));
S1.display();
String S2(S1);
S2.display();
getchar();
getchar();
}
三、面向对象编程
【题目要求】
- 实现一类对整型数组的管理,类定义如下:
class intarray
{
private:
int* data; //数据指针
int size;
int num; //数据中数据的有效个数
public:
intarray(int n); //分配n个数据大小的动态数组
intarray(intarray& ia); //拷贝构造函数
void add(int n); //向动态数组中添加数据n
void diaplay(); //显示该动态数组中的有效数据
~intarray(); //析构函数,释放动态数组
};
请完成以上未定义的函数体。 - 自行定义主函数,确保每个函数(包括构造函数)至少被使用一次。
【相关提示】。指针成员需要动态分配内存。
#include<iostream>
#include<cstring>
using namespace std;
class intarray
{
int* data;
int size;
int num;
public:
intarray(int n)
{
size=n;
num=0;
data=new int[n];
};
intarray(intarray &ia)
{
size=ia.size;
num=ia.num;
data=new int[ia.size];
int i;
for(i=0;i<num;i++)
data[i]=ia.data[i];
}
void add(int n)
{
if(num==size)
{
int* d;
int i;
d=new int[size+4];
for(i=0;i<num;i++)
d[i]=data[i];
delete []data;
data=d;
size+=4;
data[num++]=n;
}
else data[num++]=n;
}
void display()
{
int i;
for(i=0;i<num;i++)
cout<<data[i]<<'\t';
cout<<endl;
}
~intarray(){delete []data;}
};
void main()
{
intarray ia1(10);
int i;
for(i=0;i<12;i++)
ia1.add(25+2*i);
ia1.display();
intarray ia2(ia1);
ia2.display();
getchar();
getchar();
}