C++ 5_类与对象 拷贝构造函数
题1 . 在下面的程序中,完善构造函数的编写,理解拷贝构造函数的作用和调用时机。要求回答:
(1) 程序没有显式调用student1.upeerName(),为什么输出结果是全大写的”SUN WUKONG”?
student1.name与student2.name共用一块存储空间,在调用student2.upeerName()后,student1.name已修改为大写
(2) 程序出错的原因是?
student1.name与student2.name共用一块存储空间,主函数结束时,由于系统调用析构函数将student2.name的空间释放,也就是student1.name的空间已被释放,再次释放就出错了
(3) 给Student类添加拷贝构造函数Student(Student& s);再次运行程序
#include<iostream>
using namespace std;
class Student
{
private:
char* name; //
public:
Student(char* newName); //
~Student(); //
void upperName(); //
void show(); //
};
Student::Student(char* newName = NULL) //
{
}
Student::~Student() //
{
if (name != NULL)
{
cout << name << "的析构函数被调用(注意大小写)" << endl;
delete[] name; //
}
}
void Student::show() //
{
if (name != NULL)
{
cout << name << endl;
}
else
name = NULL;
}
void Student::upperName() //此函数的作用是:
{
if (name != NULL)
{
int len = strlen(name); //
for (int i = 0; i < len; i++)
{
name[i] = toupper(name[i]); //查百度,toupper()的功能为:
}
}
}
int main()
{
Student student1("sun wukong");//
Student student2 = student1; //
student2.upperName(); //
student1.show(); //
return 0;
}
程序
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
private:
char* name; //字符串指针
public:
Student(char* newName);//构造函数
Student(const Student &obj);//拷贝构造函数
~Student();//析构函数
void upperName(); //转换大写
void show(); //输出
};
Student::Student(char* newName = NULL) //构造函数
{
if (newName)
{
name = new char[strlen(newName) + 1];
strcpy(name, newName);
}
else name = NULL;
}
Student::Student(const Student &obj)//拷贝构造函数
{
if (obj.name)
{
name = new char[strlen(obj.name) + 1];
strcpy(name, obj.name);
}
else name = NULL;
}
Student::~Student() //析构函数
{
if (name != NULL)
{
cout << name << "的析构函数被调用(注意大小写)" << endl;
delete[] name; //撤销动态存储空间
}
}
void Student::show() //输出
{
if (name != NULL)
{
cout << name << endl;
}
else
name = NULL;
}
void Student::upperName() //此函数的作用是:字符串全变为大写
{
if (name != NULL)
{
int len = strlen(name); //计算字符串长度
for (int i = 0; i < len; i++)
{
name[i] = toupper(name[i]); //查百度,toupper()的功能为:转换为大写
}
}
}
int main()
{
Student student1("sun wukong");//调用构造函数
Student student2 = student1; //调用拷贝构造函数
student2.upperName(); //调用转换大写函数
student1.show(); //调用输出函数
return 0;
}
题 求平面上二点间的距离,要求使用对象成员的概念来设计。
程序
#include<iostream>
#include<math.h>
using namespace std;
class Point
{
double x, y,z;
public:
Point(int a = 0, int b = 0,int c=0)
{
x = a;
y = b;
z = c;
cout << x << ',' << y <<','<<z<< "构造Point\n";
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
int Getz()
{
return z;
}
~Point()
{
cout << x << ',' << y<<','<<z<<"析构Point\n";
}
};
class Line
{
int width, color;
Point p1, p2;
public:
Line(int x1, int y1,int z1, int x2, int y2,int z2,int w, int c) :p1(x1, y1,z1), p2(x2, y2,z2)
{
width = w;
color = c;
cout << width << ',' << color << "构造Line\n";
}
double LineLen()
{
double len;
double x1, y1,z1,x2, y2,z2;
x1 = p1.Getx();
y1 = p1.Gety();
z1 = p1.Getz();
x2 = p2.Getx();
y2 = p2.Gety();
z2 = p2.Getz();
len = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
return(len);
}
~Line()
{
cout << width << ',' << color << "析构Line\n";
}
};
void main()
{
Line Li(0, 0,0, 1,1, 1, 3, 6);
cout << "长度=" << Li.LineLen() << endl;
}