oj 贵州大学 第四次作业 (c++)(运算符重载)(第九题)

设计一个自己的字符串类MyString,内部有一个私有成员char *sptr;该成员用于创建字符串对象时,在内部使用动态内存分配的方法分配一个字符数组用于存储字符串的内容。

为该类设计构造函数、析构函数(对象析构时,要释放分配的数组)
为该类设计返回字符串实际长度的成员函数
为该类设计输出字符串内容的成员函数
为该类设计实现字符串连接和字符串复制的成员函数。字符串连接和字符串复制时,要求重新分配数组,并释放原有的数组。
main函数已经写好,请根据main函数的内容完成该类的设计:
int main(){
MyString s1;
MyString s2("Hello");
MyString s3(s2);
s1.printString();
s2.printString();
s3.printString();
cout<<s1.getSize()<<" "<<s2.getSize()<<" "<<s3.getSize()<<endl;
MyString s4("HiChina"); 
s2.stringCopy(s4);
s2.printString();
s3.stringCat(s4);
s3.printString(); 
return 0;
}

输入描述本题无输入

输出描述按样例输出

提示你需要提交除了main函数之外的其他代码,可以使用C中的字符串处理函数帮助你的处理

样例输入 

样例输出

Object Constructed. No Memory Allocated.
Object Constructed. 6 Bytes Allocated.
Object Constructed. 6 Bytes Allocated.
No Memory Allocated In This Object.
Hello
Hello
0 5 5
Object Constructed. 8 Bytes Allocated.
String Copy, 8 Bytes Reallocated And 6 Bytes Freed.
HiChina
String Connection, 13 Bytes Reallocated And 6 Bytes Freed.
HelloHiChina
Object Destructed. 8 Bytes Freed.
Object Destructed. 13 Bytes Freed.
Object Destructed. 8 Bytes Freed.
Object Destructed. No Memory Freed.
#include<stdio.h>
#include <iostream>
#pragma warning(disable:4996)
#include <string.h>
using namespace std;
class MyString
{
private:
    char* sptr;
public:
    MyString()
    {
        sptr = new char[1];
        sptr[0] = '\0';
        cout << "Object Constructed. No Memory Allocated." << endl;
    }
    MyString(const char* p)
    {
        sptr = new char[strlen(p) + 1];
        strcpy(sptr, p);
        cout << "Object Constructed. " << strlen(p) + 1 << " Bytes Allocated." << endl;
    }
    MyString(const MyString& str)
    {
        sptr = new char[strlen(str.sptr) + 1];
        strcpy(sptr, str.sptr);
        cout << "Object Constructed. " << strlen(str.sptr) + 1 << " Bytes Allocated." << endl;
    }
    ~MyString()
    {
        if (sptr[0] == '\0')
        {
            delete[]sptr;
            cout << "Object Destructed. No Memory Freed." << endl;
        }
        else
        {
            cout << "Object Destructed. " << strlen(sptr) + 1 << " Bytes Freed." << endl;
            delete[]sptr;
        }
            
        
    }
    void printString()
    {
        if (sptr[0] == '\0')cout << "No Memory Allocated In This Object." << endl;
        else cout << sptr << endl;
    }
    int getSize()
    {
        int p = (strlen(this->sptr));
        return p;
    }
    void stringCopy(const MyString& s)
    {
        cout << "String Copy, " << strlen(s.sptr) + 1 << " Bytes Reallocated And " << strlen(this->sptr) + 1 << " Bytes Freed." << endl;
        delete[]sptr;
        sptr = new char[strlen(s.sptr) + 1];
        strcpy(sptr, s.sptr);
    }
    void stringCat(const MyString& s)
    {
        cout << "String Connection, " << strlen(s.sptr) + 1 + strlen(this->sptr) << " Bytes Reallocated And " << strlen(this->sptr) + 1 << " Bytes Freed." << endl;
        int m1 = strlen(this->sptr);
        int m2 = strlen(s.sptr);
        char* temp = new char[m1 + m2 + 1];
        strcpy(temp, this->sptr);
        strcat(temp, s.sptr);
        delete[] sptr;
        sptr= new char[m1 + m2 + 1];
        strcpy(sptr, temp);
        delete[]temp;
    }
};
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值