2016/10/19

来源:互联网 发布:ubuntu安装netsnmp 编辑:程序博客网 时间:2024/05/01 00:51
1631-5 黄加勉 <2016.10.19>
 连续第18天总结


A.今日任务
1.深拷贝与浅拷贝(100%)
2.对象指针(80%)


B.具体内容
1.浅拷贝不需要定义指针,深拷贝关键就是定义指针指向源成员函数;
2.试了一下对象成员的指针,不知为何,指针在赋值时会被要求写成数组的形式,估计是赋的值是一个对象,所以要求定义成数组吧
3.深拷贝也就包含了对象指针的运用


附代码:


#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//declare class
//component
class Component
{
public:
Component();
Component(string _model,string _size);
~Component();
Component(const Component &precom);
void set_model(string _model);
void set_size(int length, int width, int height);
string get_model();
string get_size();
private:
string m_strModel;
string m_iSize;
};
//tool
class Tool
{
public:
Tool();
~Tool();
Tool(const Tool &tool);
void set_com_model(string _model);
void set_com_size(int length, int width, int height);
void copy_com();
string get_com_model();
string get_com_size();
private:
Component com;
Component *p;
};
//main function
int main()
{
Tool tool;
tool.set_com_model("70tps14");
tool.set_com_size(1, 2, 3);
Tool tool2(tool);
tool2.set_com_model("70tps16");
cout << "model = " << tool.get_com_model() << "  " << "size = " << tool.get_com_size() << endl;
cout << "model = " << tool2.get_com_model() << "  " << "size = " << tool2.get_com_size() << endl;
tool.copy_com();
//cout << "model = " << tool.get_com_model() << "  " << "size = " << tool.get_com_size() << endl;
system("pause");
return 0;
}
//define functions
//component
Component::Component() :m_strModel("NULL"), m_iSize("NULL")
{
cout << "begin : component" << endl;
}
Component::Component(string _model, string _size) : m_strModel(_model), m_iSize(_size) {}
Component::~Component()
{
cout << "delete : component" << endl;
}
Component::Component(const Component &precom)
{
//string *p = new string();
//string *q = new string();
//*p = precom.m_strModel;
//*q = precom.m_iSize;
m_strModel = precom.m_strModel;
m_iSize = precom.m_iSize;
cout << "transfer : component&" << endl;
}
void Component::set_model(string _model)
{
m_strModel = _model;
}
void Component::set_size(int length, int width, int height)
{
stringstream s1, s2, s3;
string str1, str2, str3, size;
string space = ",";
s1 << length;
s2 << width;
s3 << height;
s1 >> str1;
s2 >> str2;
s3 >> str3;
size = str1 + space + str2 + space + str3;
m_iSize = size;
}
string Component::get_model()
{
return m_strModel;
}
string Component::get_size()
{
return m_iSize;
}
//tool
Tool::Tool()
{
cout << "begin : tool" << endl;
}
Tool::~Tool()
{
delete p;
p = NULL;
cout << "delete : tool" << endl;
}
Tool::Tool(const Tool &tool)
{
p = new Component[0];
p[0] = tool.com;
com = p[0];
cout << "transfer : tool&" << endl;
}
void Tool::set_com_model(string _model)
{
com.set_model(_model);
}
void Tool::set_com_size(int length, int width, int height)
{
com.set_size(length, width, height);
}
string Tool::get_com_model()
{
return com.get_model();
}
string Tool::get_com_size()
{
return com.get_size();
}
void Tool::copy_com()
{
Component com2(com);
com2.set_size(9, 10, 11);
cout << "model = " << com2.get_model() << "  " << "size = " << com2.get_size() << endl;
}



C.明日任务
1.对象成员指针
2.this指针
0 0