完成mystring类
mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <string.h>
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<<s.str;
return output;
}
思维导图
特殊函数的继承
#include <iostream>
using namespace std;
class Father
{
protected:
string name;
public:
//无参构造
Father() {cout<<"Father::无参构造"<<endl;}
//有参构造
Father(string n):name(n) {cout<<"Father::有参构造"<<endl;}
//拷贝构造
Father(const Father&other):name(other.name) {cout<<"Father::拷贝构造"<<endl;}
//拷贝赋值
Father& operator=(const Father&other)
{
if(this != &other)
{
this->name = other.name;
}
cout<<"Father::拷贝赋值函数"<<endl;
return *this;
}
//析构函数
~Father() {cout<<"Father::析构函数"<<endl;}
};
class Son:public Father
{
private:
string toy; //玩具
public:
//子类无参构造
Son() {cout<<"Son::无参构造"<<endl;}
//子类有参构造
Son(string n, string t):Father(n), toy(t)
{cout<<"Son::有参构造"<<endl;}
//子类析构函数
~Son() {cout<<"Son::析构函数"<<endl;}
//子类的拷贝构造函数
Son(const Son &other):Father(other), toy(other.toy)
{
cout<<"Son::拷贝构造"<<endl;
}
//子类的拷贝赋值函数
Son &operator=(const Son &other)
{
if(this != &other)
{
//显性调用父类的拷贝赋值函数
Father::operator=(other); //完成对子类从父类继承下来成员的赋值工作
this->toy = other.toy; //自己拓展的成员,由自己的拷贝赋值函数完成
}
cout<<"Son::拷贝赋值函数"<<endl;
return *this;
}
void show()
{
cout<<"name = "<<name<<" toy = "<<toy<<endl;
}
};
int main()
{
Son s1; //无参构造
Son s2("zhangsan", "car"); //有参构造
Son s3(s2); //拷贝构造
s1 = s3; //拷贝赋值函数
s1.show();
cout<<"*******************************"<<endl;
return 0;
}