1,++运算符的重载
#include <iostream.h>
class Cincrease
{
public:
Cincrease(int x) : value(x) {}
Cincrease& operator ++ (); //前增量
Cincrease operator ++(int); //后增量
void display()
{
cout << "The value is " << value << endl;
}
private:
int value;
};
Cincrease& Cincrease::operator ++ ()
{
value++; //先增量
return * this; //再返回原对象
}
Cincrease Cincrease::operator ++(int)
{
Cincrease temp(*this); //临时对象存放原有对象值
value++; //原有对象增量修改
return temp; //返回原有对象值
}
int main()
{
Cincrease n(20);
n.display();
(n++).display(); //显示临时对象值
n.display(); //显示原有对象
++n;
n.display();
++(++n);
n.display();
(n++)++; //第二次增量操作对临时对象进行
n.display();
return 0;
}
输出如下:
The value is 20
The value is 20
The value is 21
The value is 22
The value is 24
The value is 25
或者
#include <iostream.h>
class Increase
{
public:
Increase(int x) : value(x) {}
friend Increase& operator ++ (Increase& ); //前增量
friend Increase operator ++(Increase&, int); //后增量
void display()
{
cout << "The value is " << value << endl;
}
private:
int value;
};
Increase& operator ++ (Increase& a)
{
a.value++; //前增量
return a; //再返回原对象
}
Increase operator ++(Increase& a, int)
{
Increase temp(a); //通过拷贝构造函数保存原有对象值
a.value++; //原有对象增量修改
return temp; //返回原有对象值
}
int main()
{
Increase n(20);
n.display();
(n++).display(); //显示临时对象值
n.display(); //显示原有对象
++n;
n.display();
++(++n);
n.display();
(n++)++; //第二次增量操作对临时对象进行
n.display();
return 0;
}
2,强制类型转换运算符重载
class Stonewt
{
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
public:
Stonewt(double lbs); // construct from double pounds
Stonewt(int stn, double lbs); // construct from stone, lbs
Stonewt(); // default constructor
~Stonewt();
// conversion functions
operator int() const;
operator double() const;
};
// construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
stone = int (lbs) / Lbs_per_stn; // integer division
pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt() // destructor
{
}
// conversion functions
Stonewt::operator int() const
{
return int (pounds + 0.5);
}
Stonewt::operator double()const
{
return pounds;
}
3,输入输出,下标运算符
class String
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char * s); // constructor
String(); // default constructor
String(const String &); // copy constructor
~String(); // destructor
int length () const { return len; }
// overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// overloaded operator friends
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
// static function
static int HowMany();
};
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char * s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
std::strcpy(str, s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 4;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char [len + 1]; // allot space
std::strcpy(str, st.str); // copy string to new location
}
String::~String() // necessary destructor
{
--num_strings; // required
delete [] str; // required
}
// overloaded operator methods
// assign a String to a String
String & String::operator=(const String & st)
{
if (this == &st)
return *this;
delete [] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete [] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
return str[i];
}
// overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2.str < st1.str;
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream & operator<<(ostream & os, const String & st)
{
os << st.str;
return os;
}
// quick and dirty String input
istream & operator>>(istream & is, String & st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
st = temp;
while (is && is.get() != '\n')
continue;
return is;
}