几个典型操作符重载例子
//* char operator [] (int i);
const operator [] (int i) const;
//* String & operator = (const String &);
String & operator = (const char *);
//* friend bool operator < (const String & st,const String & st2);
//* friend bool operator > (const String & st,const String & st2);
//* friend bool operator == (const String & st,const String & st2);
//* friend std::ostream & operator << (std::ostream & os,const String & st);
//* friend std::istream & operator >> (std::istream & is,String & st);
///////////////////////////////////////
///////////main.cpp////////////////////
#include <iostream>
#include "strngbad.h"
const int ArSize=10;
const int MaxLen=81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout<<"hi,what'your name?\n>>";
cin>>name;
cout<<name<<",please enter up to "<<ArSize<<" short saying <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for (i=0;i<ArSize;i++)
{
cout<<i+1<<": ";
cin.get(temp,MaxLen);
while (cin&&cin.get()!='\n')
continue;
if(!cin||temp[0]=='\0')
break;
else
sayings[i]=temp;
}
int total=i;
cout<<"here are your sayings:\n";
for (i=0;i<total;i++)
cout<<sayings[i][0]<<": "<<sayings[i]<<endl;
int shortest=0;
int first=0;
for (i=1;i<total;i++)
{
if(sayings[i].length()<sayings[shortest].length())
shortest=i;
if(sayings[i]<sayings[first])
first=i;
}
cout<<"short: "<<sayings[shortest]<<endl;
cout<<"first: "<<sayings[first]<<endl;
cout<<String::howmany()<<endl;
return 0;
}
/////////////////////////////////////////
/////////////////strngbad.h//////////////
#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class String
{
private:
char * str;
int len;
static int num_strings;
// static const int CinLim=80;
enum {CinLim=80};
public:
String(const char * s);
String();
String(const String &);
~String();
int length()const {return len;}
String & operator = (const String &);
String & operator = (const char *);
char & operator [] (int i);
const char & operator [] (int i) const;
friend bool operator < (const String & st,const String & st2);
friend bool operator > (const String & st,const String & st2);
friend bool operator == (const String & st,const String & st2);
friend std::ostream & operator << (std::ostream & os,const String & st);
friend std::istream & operator >> (std::istream & is,String & st);
static int howmany();
};
#endif
///////////////////////////////////////////////////////////
//////////////////////////strngbad.cpp/////////////////////
#include <cstring>
#include "strngbad.h"
using std::cout;
using std::cin;
int String::num_strings=0;
String::String(const char * s)
{
len=strlen(s);
str=new char [len+1];
strcpy(str,s);
num_strings++;
}
String::String()
{
len=4;
str=new char [1];
str[0]='\0';
num_strings++;
}
String::String(const String & st)
{
len=st.len;
str=new char [len+1];
strcpy(str,st.str);
num_strings++;
}
String::~String()
{
--num_strings;
delete [] str;
}
String & String::operator = (const String & st)
{
if (this==&st)
return *this;
delete [] str;
len=st.len;
str=new char [len+1];
strcpy(str,st.str);
return *this;
}
String & String::operator = (const char * st)
{
delete [] str;
len=strlen(st);
str=new char [len+1];
strcpy(str,st);
return *this;
}
char & String::operator [] (int i)
{
return str[i];
}
const char & String::operator [] (int i) const
{
return str[i];
}
bool operator < (const String & st,const String & st2)
{
return (strcmp(st.str,st2.str)<0);
}
bool operator > (const String & st,const String & st2)
{
return st2.str<st.str;
}
bool operator == (const String & st,const String & st2)
{
return (strcmp(st.str,st2.str)==0);
}
std::ostream & operator << (std::ostream & os,const String & st)
{
os<<st.str;
return os;
}
std::istream & operator >> (std::istream & is,String & st)
{
char temp[String::CinLim];
// char temp[CinLim];
is.get(temp,String::CinLim);
if(is)
st=temp;
while (is&&is.get()!='\n')
continue;
return is;
}
int String::howmany()
{
return num_strings;
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
////////////////////////////////用指针实现///////////////
////////////////////////////////////////////////////////
////////////////main.cpp////////////////////////////////
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "string1.h"
const int ArSize=10;
const int MaxLen=81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout<<"hi,what'your name?\n>>";
cin>>name;
cout<<name<<",please enter up to "<<ArSize<<" short saying <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for (i=0;i<ArSize;i++)
{
cout<<i+1<<": ";
cin.get(temp,MaxLen);
while (cin&&cin.get()!='\n')
continue;
if(!cin||temp[0]=='\0')
break;
else
sayings[i]=temp;
}
int total=i;
if (total>0)
{
cout<<"here are your sayings:\n";
for (i=0;i<total;i++)
cout<<sayings[i]<<"\n";
String * shortest=& sayings[0];
String * first=& sayings[0];
for (i=1;i<total;i++)
{
if (sayings[i].length()<shortest->length())
shortest=&sayings[i];
if (sayings[i]<*first)
first=&sayings[i];
}
cout<<"shortest saying: \n"<<*shortest<<endl;
cout<<"first alphabetically: \n"<<*first<<endl;
srand(int(time(0)));
int choice =rand()%total;
String * favorite=new String (sayings[choice]);
cout<<"my favorite saying: \n"<<*favorite<<endl;
delete favorite;
}
else
cout<<"no much to say,eh?\n";
cout<<"bye.\n";
return 0;
}


浙公网安备 33010602011771号