
作业社区
探索学习新天地,共享知识资源!
wgf1209 的学生作业:
Demo_String.h class String { public: static void demo_string(void); void show(void);//输出字符串中的每个字符和对应的ASCII码 static void showMemoryUsed(void);//静态成员函数. private: char *str; static int memoryUseCount; //静态成员变量 private: String(const char *str = NULL);//有参构造函数 String(const String &obj);//拷贝构造 ~String();// 析构函数 }; extern String function_str(); Demo_String.cpp #include #include #include #include #include #include //标准算法头文件 #include "../head/Demo_String.h" /* @Author: wjhu8 @Time: 2024-11-05 13:34:22 @name: Demo_String.cpp */ //标准的命名空间 using namespace std; //内存使用计数 //int memoryUseCount = 0; //内存使用计数 int String::memoryUseCount = 0;//类外进行初始化 //有参构造函数 String::String(const char *str) { cout str, str); memoryUseCount += len; } else { // 如果 str 是 NULL,则 str 赋值 NULL, this->str = NULL; } } //拷贝构造 String::String(const String &obj) { cout str, obj.str); memoryUseCount += len; } else { //浅拷贝 this->str = obj.str; } } //输出字符串中的每个字符和对应的ASCII码 void String::show(void) { if (str) { for (char *p = str; *p; ++p) { cout





wgf1209 的学生作业:
class Test { public: Test(int size);//有参构造函数 Test(const Test &obj); //拷贝构造 ~Test(void); // 析构函数 private: int size; int *data; }; //有参构造函数 Test::Test(int size) { cout data = new int[size]; } Test::Test(const Test &obj) { //*this = obj; //需要对内存重新申请以免共用一块内存调用析构函数时,释放两次 // this->size = obj.size; // this->data = new int[size]; if (obj.data) { cout size = obj.size; memcpy(this->data, obj.data, obj.size * sizeof(int)); } } Test::~Test(void) { cout





wgf1209 的学生作业:
//标准的命名空间 using namespace std; class Test { public: Test(int size); ~Test(); void insert(int data); void show(void); private: int size; int index; int *data; }; Test::Test(int size) { this->index = 0; this->size = size; this->data = new int[size]; } Test::~Test() { delete[] this->data; } void Test::insert(int data) { if (index < size) { this->data[index++] = data; } else { cout




