#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str;
int size;
public:
myString():size(10)
{
str = new char[size];
strcpy(str, "");
}
//有参构造
myString(const char *s):str(new char[strlen(s) + 1]), size(strlen(s) + 1)
{
strcpy(this->str, s);
}
//拷贝构造
myString (const myString &other):str(new char[other.size + 1]), size(other.size)
{
strcpy(this->str, other.str);
}
//析构函数
~myString()
{
delete [] str;
}
//拷贝赋值函数
myString &operator= (const myString &other)
{
if (this != &other)
{
delete [] str; //由于需要重新申请空间,所以要释放之前使用的空间
this->size = other.size;
this->str = new char[size + 1];
strcpy(this->str, other.str);
}
return *this;
}
//判空函数
bool empty()
{
return size == 0;
}
//size函数
int str_len()
{
return size;
}
//c_str函数
char *c_str()
{
return str;
}
//at函数
char &at(int pos)
{
if (pos >= 0 || pos < this->size)
{
return str[pos];
}
}
//成员函数实现加号运算符重载:
const myString operator+ (const myString &R) const
{
myString temp;
delete [] temp.str;
temp.size = this->size + R.size;
temp.str = new char[temp.size + 1];
*temp.str = '\0';
strcat(temp.str, this->str);
strcat(temp.str, R.str);
return temp;
}
//成员函数实现加号运算符重载:
myString &operator+= (const myString &R)
{
char *temp = this->str;
delete [] str; //由于需要重新申请空间,所以要释放之前使用的空间
this->size = R.size + this->size;
this->str = new char[this->size + 1];
strcpy(this->str, temp);
strcpy(this->str, R.str);
return *this;
}
//关系运算符重载(>)
bool operator> (const myString &R) const
{
return strcmp(this->str, R.str) > 0;
}
//关系运算符重载(>=)
bool operator>= (const myString &R) const
{
return strcmp(this->str, R.str) >= 0;
}
//关系运算符重载(<)
bool operator< (const myString &R) const
{
return strcmp(this->str, R.str) < 0;
}
//关系运算符重载(<=)
bool operator<= (const myString &R) const
{
return strcmp(this->str, R.str) <= 0;
}
//==重载 若两个字符串等价返回true
bool operator==(const myString &R)const
{
return strcmp(this->str, R.str) == 0;
}
// !=重载 若两个字符串等价返回true
bool operator!=(const myString &R)const
{
return strcmp(this->str, R.str) != 0;
}
//[]运算符重载 返回下标处的字母
char &operator[](const int index)
{
if (index < 0 || index > this->size)
{
cout << "illegal index" << endl;
}
return this->str[index];
}
friend ostream &operator<<(ostream &L, const myString &R);
friend istream &operator>>(istream &L, myString &R);
};
// <<重载
ostream &operator<<(ostream &L, const myString &R)
{
L << R.str;
return L;
}
//>>重载
istream &operator>>(istream &L, myString &R)
{
string s;
L >> s;
delete [] R.str;
R.size = s.size();
R.str = new char[R.size + 1];
strcpy(R.str, s.data());
return L;
}
int main()
{
//有参构造
myString s1("hello world");
cout << s1.c_str() << endl;
cout << "===============================" << endl;
//拷贝构造
myString s2(s1);
cout << s2.c_str() << endl;
cout << "===============================" << endl;
//拷贝赋值
myString s3;
s3 = s1;
cout << s3.c_str() << endl;
cout << "===============================" << endl;
//+重载
myString s4 = s1 + s3;
cout << s4.c_str() << endl;
cout << "===============================" << endl;
//>重载
myString s5("He");
if (s1 > s5)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
cout << "===============================" << endl;
//[]重载
myString s6("abcde");
cout << s6[4] << endl;
cout << "===============================" << endl;
//==重载
if (!(s6 == s5))
{
cout << "yes" << endl;//两字符串不等价
}
// !=重载
if (s6 != s5)
{
cout << "yes" << endl;//两字符串不等价
}
cout << "===============================" << endl;
// <<重载
cout << s6 << endl;
cout << "===============================" << endl;
// >>重载
myString s7;
cin >> s7;
cout << s7 << endl;
return 0;
}
#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 << "析构函数" << 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()
{
string s("hello");
s.size();
Son s1; //无参构造‘
Son s2("zhangsan", "car"); //有参构造
cout << "+====================================+" << endl;
Son s3(s2);
s1 = s3; //拷贝赋值函数
s1.show();
cout << "+====================================+" << endl;
return 0;
}