7.20 作业

完成mystring类

mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H

#include 
#include 
using namespace std;

class myString
{
    char *str;//字符串
    unsigned long size;//实际大小
public:
    myString();//无参构造
    myString(const char *s);//有参构造
    ~myString();//析构
    myString(const myString &s);//拷贝构造
    bool expansion(unsigned long size);//扩容
    myString& operator=(const myString &s);//拷贝赋值
    bool isempty();//判空
    unsigned long length();//字符串长度
    const char *c_str();//返回char字符串
    char &at(int pos);//at
    const myString operator+(const myString &s)const;//+运算符重载
    myString& operator+=(const myString &s);//+=运算符重载
    char& operator[](int pos);//[]重载
    bool operator>(const myString &s)const;//>运算符重载
    bool operator>=(const myString &s)const;//>=重载
    bool operator<(const myString &s)const;//<重载
    bool operator<=(const myString &s)const;//<=重载
    bool operator==(const myString &s)const;//==重载
    bool operator!=(const myString &s)const;//!=重载
    friend istream& operator >> (istream& in,myString &s);//输出重载
    friend ostream& operator << (ostream& output,const myString &s);//输入重载
};

#endif // MYSTRING_H

mystring.c

#include "mystring.h"

myString::myString():size(15)
{
    str = new char[size];
    strcpy(str,"");
}

myString::myString(const char *s)
{
    size = strlen(s)+1;
    str = new char[size];
    strcpy(str,s);
}

myString::~myString()
{
    delete [] str;
}

myString::myString(const myString &s)
{
    size = s.size;
    str = new char[size];
    strcpy(str,s.str);
}

bool myString::expansion(unsigned long size)
{
    this->size = 2*this->size>size?2*this->size:size;
    char *t = new char[this->size];
    strcpy(t,str);
    delete [] str;
    str = t;
    return 1;
}

myString &myString::operator=(const myString &s)
{
    if(this != &s)
    {
        if(strlen(s.str)+1>size) expansion(strlen(s.str)+1);
        strcpy(str,s.str);
        return *this;
    }
}

bool myString::isempty()
{
    return !strlen(str);
}

unsigned long myString::length()
{
    return strlen(str);
}

const char *myString::c_str()
{
    return str;
}

char &myString::at(int pos)
{
    return str[pos];
}

const myString myString::operator+(const myString &s) const
{
    return myString(strcat(str,s.str));
}

myString &myString::operator+=(const myString &s)
{
    if(strlen(str)+strlen(s.str)+1>size) expansion(strlen(str)+strlen(s.str)+1);
    strcat(str,s.str);
    return *this;
}

char &myString::operator[](int pos)
{
    return str[pos];
}

bool myString::operator>(const myString &s) const
{
    return strcmp(str,s.str)>0;
}

bool myString::operator>=(const myString &s) const
{
    return strcmp(str,s.str)>=0;
}

bool myString::operator<(const myString &s) const
{
    return strcmp(str,s.str)<0;
}

bool myString::operator<=(const myString &s) const
{
    return strcmp(str,s.str)<=0;
}

bool myString::operator==(const myString &s) const
{
    return strcmp(str,s.str)==0;
}

bool myString::operator!=(const myString &s) const
{
    return strcmp(str,s.str)!=0;
}

istream& operator >>(istream &in,myString &s)
{
    in>>s.str;
    s.size = strlen(s.str);
    return in;
}

ostream& operator <<(ostream &output,const myString &s)
{
    output<

思维导图7.20 作业_第1张图片

 特殊函数的继承

#include 
using namespace std;

class Father
{
protected:
    string name;
public:
    //无参构造
    Father() {cout<<"Father::无参构造"<name = other.name;
        }
        cout<<"Father::拷贝赋值函数"<toy = other.toy;          //自己拓展的成员,由自己的拷贝赋值函数完成
        }

        cout<<"Son::拷贝赋值函数"<

你可能感兴趣的:(c++)