代码大部分来自零点学通C++
// dd.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <cstring> #include <iomanip> using namespace std; class String { public: String(); ~String(); String(const char *const ch); String(const String &t); char &operator[](unsigned short int length); char operator[](unsigned short int length)const; String operator+(const String &a); String &operator=(const String &a); void operator+=(const String &a); friend ostream &operator<<(ostream &os,const String &t) { os<<t.str; return os; } friend istream &operator>>(istream &in,String &t) { in>>t.str; return in; } friend bool operator<(const String &a,const String &b) { if (strcmp(a.str,b.str)<0) return 1; else return 0; } friend bool operator>(const String &a,const String &b) { if (strcmp(a.str,b.str)>0) return 1; else return 0; } friend bool operator==(const String &a,const String &b) { if (strcmp(a.str,b.str)==0) return 1; else return 0; } unsigned short int getlength()const{return length;} const char *getstr()const{return str;} private: int length; char *str; String(unsigned short int); }; String::String(unsigned short int len) { str = new char[len+1]; for (int i=0;i<=len;i++) { str[i] = '\0'; } length = len; } String String::operator+(const String&t) { int length_total = length + t.getlength(); String temp(length_total); int i,j;//////////////// for (i=0;i<length;i++) { temp[i] = str[i]; } for(j=0;j<t.getlength();j++,i++) temp[i] = t[j]; temp[length_total] = '\0'; return temp; } ////////////// char &String::operator[](unsigned short int len) { if (len>length) return str[length-1]; else return str[len]; } char String::operator[](unsigned short int len)const { if(len>length) return str[length-1]; else return str[len]; } String::~String() { delete []str; length = 0; } /////////////// String::String() { length = 0; str = new char[1]; str[0] = '\0'; } ///////////////////// String::String(const char *const ch) { length = strlen(ch);//将得到的字符数组长度给length str = new char[length+1];// for (int i=0;i<length;i++) str[i] = ch[i]; str[length] = '\0';//这一步非常重要,表示复制的字符串结束了 } String::String(const String &t) { length = t.getlength(); str = new char[length+1]; for(int i=0;i<length;i++) str[i] = t[i]; str[length]='\0'; } String &String::operator=(const String & s) { if (this == &s) { return *this; } delete []str; length = s.getlength(); str = new char[length +1]; for(int i=0;i<length;i++) { str[i] = s[i]; } str[length] = '\0'; return *this; } ///////////////// void String::operator+=(const String &t) { int length_total = length+t.getlength(); String temp(length_total); int i,j; for (i=0;i<length;i++) { temp[i] = str[i]; } for(j=0;j<t.getlength();j++,i++) temp[i] = t[j]; temp[length_total] = '\0'; *this = temp; } class book { public: book(); book(char *,char *,char *,float ); const String &gettitle()const{return title;} const String &getauthor()const{return author;} const String &getnumber()const{return number;} float getprice()const{return price;} void settitle(const String &Stitle){title = Stitle;} void setauthor(const String & sauthor){author = sauthor;} void setnumber(const String & snumber){number = snumber;} void setprice(float sprice){price = sprice;} friend ostream &operator<<(ostream &os,const book &t) { os<<t.title<<","<<t.author<<","<<t.number<<","<<t.price<<endl; return os; } void settotal(const String &st,const String &sa,const String &sn,float sp) { title = st; author = sa; number = sn; price = sp; } private: String title; String author; String number; float price; }; book::book():title("no title"),author("no author"),number("no number"),price(0){} book::book(char *_title,char *_author,char *_number,float _price): title(_title),author(_author),number(_number),price(_price){} void print(int a) { for (int i=0;i<a;i++) { cout<<' '; } } void main() { book love("love","Jack","001",33.4); cout<<love<<endl; love.settotal("English","Mr.li","002",22.1); cout<<"book name:"; print(15-sizeof("book name:"));//不知道怎么才能让其左对齐没办法只能自己写个简单的打印空格的函数 cout<<love.gettitle()<<endl; cout<<"book author:"; print(15-sizeof("book author:")); cout<<love.getauthor()<<endl; cout<<"book number:"; print(15-sizeof("book number:")); cout<<love.getnumber()<<endl; cout<<"book price:"; print(15-sizeof("book price:")); cout<<love.getprice()<<endl; system("pause"); }