

1,定义一个Mystring类代替string的功能
#include <iostream>
#include <string.h>
using namespace std;
class Mystring
{
friend ostream &operator<<(ostream &cout,const Mystring &s);
friend istream &operator>>(istream &cin,Mystring &s);
private:
char *str;
public:
Mystring(char *str=nullptr)
{
this->str = new char[sizeof(str)];
strcpy(this->str,str);
}
Mystring(const Mystring &other)
{
str = new char[sizeof(other.str)];
strcpy(str,other.str);
}
Mystring &operator=(const Mystring &s)
{
str = new char[sizeof(s.str)];
str = s.str;
return *this;
}
~Mystring()
{
delete []str;
}
Mystring &operator=( char *s)
{
str = s;
return *this;
}
long size()
{
return strlen(str);
}
Mystring operator+(const Mystring &s) const
{
Mystring temp = *this;
strcat(temp.str,s.str);
return temp;
}
bool operator>(const Mystring &s)const
{
if(strcmp(str,s.str)>0)
{
return true;
}
else
{
return false;
}
}
bool operator==(const Mystring &s)const
{
if(strcmp(str,s.str)==0)
{
return true;
}
else
{
return false;
}
}
bool operator<(const Mystring &s)const
{
if(strcmp(str,s.str)<0)
{
return true;
}
else
{
return false;
}
}
void show()
{
cout << str << endl;
}
};
ostream &operator<<(ostream &cout,const Mystring &s)
{
cout << s.str << endl;
return cout;
}
istream &operator>>(istream &cin,Mystring &s)
{
cout << "please input:";
cin >> s.str;
return cin;
}
int main()
{
char buf[100]="123456";
Mystring s1(buf);
s1.show();
Mystring s2 = s1;
s2.show();
Mystring s3 = s1+s2;
s3.show();
if(s1>s3)
{
cout << "s1>s3"<<endl;
}
if(s1==s3)
{
cout << "s1=s3" << endl;
}
else
{
cout << "s1<s3" << endl;
}
cin >> s3;
cout << s3;
return 0;
}

2,借书的类
#include <iostream>
#include <vector>
using namespace std;
class Book
{
friend void add_book(vector<Book> &v);
friend istream &operator>>(istream &cin,Book &b);
friend void borrow_book(vector<Book> &v);
friend ostream &operator<<(ostream &cout,Book &b);
friend void back_book(vector<Book> &v);
private:
string book;
string name;
static int count;
public:
Book(){}
Book(string book,string name):book(book),name(name){}
};
int Book::count=0;
istream &operator>>(istream &cin,Book &b)
{
cout << "input book ,name:" << endl;
cin >> b.book >> b.name ;
return cin;
}
ostream &operator<<(ostream &cout,Book &b)
{
cout << b.book << " " << b.name;
return cout;
}
void add_book(vector<Book> &v)
{
Book b;
cin >> b;
v.push_back(b);
b.count++;
}
void borrow_book(vector<Book> &v)
{
string a;
cout << "book name:";
cin >> a;
for(int i=0;i<Book::count;i++)
{
if(a==v[i].book)
{
Book::count--;
v.erase(v.begin()+i);
}
}
cout << "not found" << endl;
}
void back_book(vector<Book> &v)
{
Book b;
cin >> b;
v.push_back(b);
}
void show(vector<Book> &v)
{
for(Book b:v)
{
cout << b << endl;
}
}
int main()
{
vector<Book> v;
while(1)
{
cout << "add:1" << endl;
cout << "borrow:2" << endl;
cout << "check:3" << endl;
cout << "back:4" << endl;
cout << "exit:0" << endl;
int flag;
cin >> flag;
switch (flag)
{
case 1:
add_book(v);
break;
case 2:
borrow_book(v);
break;
case 3:
show(v);
break;
case 4:
back_book(v);
break;
case 0:
return 0;
}
}
return 0;
}

3,登陆界面设置
