马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
操作符重载
操作符重载(operator overoading)是一种情势的 C++多态。
第8章介绍了C++是如何使用户能够界说多个名称雷同但特性标(参数列表)不同的函数的。这被称为函数重载(function overloading)或函数多态(functional polymorphism),旨在让您能够用同名的函数来完成雷同的基本操作,即使这种操作被用于不同的数据范例
现实上,许多 C++(也包括C语言)操作符已经被重载。比方,将*操作符用于地址,将得到存储在这个地址中的值;但将它用于两个数字时,得到的将是它们的乘积。C++根据操作数的数目和范例来决定接纳哪种操作。
数组加法
op是将要重载的操作符。比方,operator +()重载+操作符,operator*()重载*操作符。op 必须是有效的 C++操作符,不能虚构一个新的符号。比方,不能有 operator@()这样的函数,由于 C++中没有@操作符。
操作符重载范例
mytime0.h
- // mytime0.h-- Time class before operator overloading
- #ifndef MYTIMEO H
- #define MYTIMEO H
- class Time
- {
- private:
- int hours;
- int minutes;
- public:
- Time();
- Time(int h, int m = 0);
- void AddMin(int m);
- void AddHr(int h);
- void Reset(int h = 0, int m = 0);
- Time Sum(const Time &t) const;
- void Show() const;
- };
- #endif
复制代码 Sum()函数的代码。注意参数是引用,但返回范例却不是引用。将参数声明为引用的目的是为了进步服从。如果按值传递Time 对象,代码的功能将雷同,但传递引用,速度将更快,使用的内存将更少。
mytime0.cpp
- // mytime0.cpp--implement Time methods
- #include <iostream>
- #include "mytime0.h"
- Time::Time()
- {
- hours = minutes = 0;
- }
- Time::Time(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- void Time::AddMin(int m)
- {
- minutes += m;
- hours += minutes / 60;
- minutes %= 60;
- }
- void Time::AddHr(int h)
- {
- hours += h;
- }
- void Time::Reset(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- Time Time::Sum(const Time &t) const
- {
- Time sum;
- sum.minutes = minutes + t.minutes;
- sum.hours = hours + t.hours + sum.minutes / 60;
- sum.minutes %= 60;
- return sum;
- }
- void Time::Show() const
- {
- std::cout << hours << " hours. " << minutes << "minutes";
- }
复制代码 usetime0.cpp
- #include <iostream>
- #include "mytime0.h"
- int main()
- {
- using std::cout;
- using std::endl;
- Time planning;
- Time coding(2, 40);
- Time fixing(5, 55);
- Time total;
- cout << "planning time =";
- planning.Show();
- cout << endl;
- cout << "coding time =";
- coding.Show();
- cout << endl;
- cout << "fixing time =";
- fixing.Show();
- cout << endl;
- total = coding.Sum(fixing);
- cout << "coding.Sum(fixing)=";
- total.Show();
- return 0;
- }
复制代码
添加加法操作符
将 Time 类转换为重载的加法操作符很容易,只要将 Sum()的名称改为 operator+()即可。这样做是对的,只要把操作符(这里为+)放到operator的背面,并将结果用作方法名即可。在这里,可以在标识符中使用字母、数字或下划线之外的其他字符。
mytime1.h
- // mytime0.h-- Time class before operator overloading
- #ifndef MYTIMEO H
- #define MYTIMEO H
- class Time
- {
- private:
- int hours;
- int minutes;
- public:
- Time();
- Time(int h, int m = 0);
- void AddMin(int m);
- void AddHr(int h);
- void Reset(int h = 0, int m = 0);
- // Time Sum(const Time &t) const;
- Time operator+(const Time &t) const;
- void Show() const;
- };
- #endif
复制代码 mytime1.cpp
- // mytime0.cpp--implement Time methods
- #include <iostream>
- #include "mytime0.h"
- Time::Time()
- {
- hours = minutes = 0;
- }
- Time::Time(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- void Time::AddMin(int m)
- {
- minutes += m;
- hours += minutes / 60;
- minutes %= 60;
- }
- void Time::AddHr(int h)
- {
- hours += h;
- }
- void Time::Reset(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- Time Time::operator+(const Time &t) const
- {
- Time sum;
- sum.minutes = minutes + t.minutes;
- sum.hours = hours + t.hours + sum.minutes / 60;
- sum.minutes %= 60;
- return sum;
- }
- void Time::Show() const
- {
- std::cout << hours << " hours. " << minutes << "minutes";
- }
复制代码 usetime1.cpp
- #include <iostream>
- #include "mytime0.h"
- int main()
- {
- using std::cout;
- using std::endl;
- Time planning;
- Time coding(2, 40);
- Time fixing(5, 55);
- Time total;
- cout << "planning time =";
- planning.Show();
- cout << endl;
- cout << "coding time =";
- coding.Show();
- cout << endl;
- cout << "fixing time =";
- fixing.Show();
- cout << endl;
- total = coding + fixing;
- cout << "coding + fixing=";
- total.Show();
- cout << endl;
- Time morefixing(3, 28);
- cout << "more fixing time = ";
- morefixing.Show();
- cout << endl;
- total = morefixing.operator+(total); // function notation
- cout << "morefixing.operator+(total)=";
- total.Show();
- cout << endl;
- return 0;
- }
复制代码
重载限制
重载的操作符(有些例外情况)不必是成员函数,但必须至少有一个操作数是用户界说的范例
其他重载操作符
时间的减和乘
mytime2.h
- // mytime0.h-- Time class before operator overloading
- #ifndef MYTIMEO H
- #define MYTIMEO H
- class Time
- {
- private:
- int hours;
- int minutes;
- public:
- Time();
- Time(int h, int m = 0);
- void AddMin(int m);
- void AddHr(int h);
- void Reset(int h = 0, int m = 0);
- // Time Sum(const Time &t) const;
- Time operator+(const Time &t) const;
- Time operator-(const Time &t) const;
- Time operator*(double n) const;
- void Show() const;
- };
- #endif
复制代码 mytime2.cpp
- // mytime0.cpp--implement Time methods
- #include <iostream>
- #include "mytime0.h"
- Time::Time()
- {
- hours = minutes = 0;
- }
- Time::Time(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- void Time::AddMin(int m)
- {
- minutes += m;
- hours += minutes / 60;
- minutes %= 60;
- }
- void Time::AddHr(int h)
- {
- hours += h;
- }
- void Time::Reset(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- Time Time::operator+(const Time &t) const
- {
- Time sum;
- sum.minutes = minutes + t.minutes;
- sum.hours = hours + t.hours + sum.minutes / 60;
- sum.minutes %= 60;
- return sum;
- }
- Time Time::operator-(const Time &t) const
- {
- Time diff;
- int tot1, tot2;
- tot1 = t.minutes + 60 * t.hours;
- tot2 = minutes + 60 * hours;
- diff.minutes = (tot2 - tot1) % 60;
- diff.hours = (tot2 - tot1) / 60;
- return diff;
- }
- Time Time::operator*(double mult) const
- {
- Time result;
- long totalminutes = (minutes + 60 * hours) * mult;
- result.minutes = totalminutes % 60;
- result.hours = totalminutes / 60;
- return result;
- }
- void Time::Show() const
- {
- std::cout << hours << " hours. " << minutes << "minutes";
- }
复制代码 usetime2.cpp
- #include <iostream>
- #include "mytime0.h"
- int main()
- {
- using std::cout;
- using std::endl;
- Time weeding(4, 35);
- Time waxing(2, 47);
- Time total;
- Time diff;
- Time adjusted;
- cout << "weeding time =";
- weeding.Show();
- cout << endl;
- cout << "waxing time =";
- waxing.Show();
- cout << endl;
- cout << "total time =";
- total = weeding + waxing;
- cout << "coding + fixing=";
- total.Show();
- cout << endl;
- diff = weeding - waxing;
- cout << "coding - fixing=";
- diff.Show();
- cout << endl;
- adjusted = total * 1.5;
- cout << "Adjusted work time = ";
- total.Show();
- cout << endl;
- return 0;
- }
复制代码
友元简介
C++控制对类对象私有部分的访问。通常,公有类方法提供惟一的访问途径,但是有时间这种限制太严格,以致上不恰当特定的编程题目。在这种情况下,C++提供了另外一种情势的访问权限:
友元。
友元有三种:
介绍如何成为友元之前,先介绍为何需要友元。在为类重载一元操作符时(带两个参数的操作符)常常需要友元。将Time对象乘以实数就属于这种情况
从概念上说,2.75*B应与 B*2.75 雷同,但第一个表达式不对应于成员函数,由于 2.75 不是 Time 范例的对象。记着,左侧的操作数应是调用对象,但2.75不是对象。因此,编译器不能使用成员函数调用来替换该表达式。
决这个难题的一种方式是,告知每个人(包括程序员本身),只能按B*2.75 这种格式编写,不能写成 2.75*B。这是一种对服务器友爱-客户警惕的(server-ffiendly,client-beware)办理方案,与OOP无关。
(无法直接访问B中的内容)
创建友元
创建友元函数的第是将其原型放在类声明中,并在原型声明前加上关键字friend
第二步是编写函数界说。由于它不是成员函数,以是不要使用Time:限定符。另外,不要在界说中使用关键字 friend,界说应该如下:
常用的友元:重载<<操作符
之以是可以这样做,是由于<<是可被重载的 C++操作符之一
前面讲过,cout 是一个 ostream 对象,它是智能的,能够辨认全部的 C++基本范例。这是由于对于每种基本范例,ostream类声明中都包含了相应的重载的 operator<<()界说
因此,要使 cout 能够辨认 Time 对象,一种方法是将一个新的函数操作符界说添加到 ostream 类声明中。但修改iostream 文件是个伤害的主意,这样做会在尺度接口上浪费时间。更好的办法是,通过Time类声明来让 Time 类知道如何使用 cout
<<第一种重载版本
司用 cout<<trip 应使用cout对象本身,而不是它的拷贝,因此该函数按引用(而不是按值)来传递该对象。这样,表达式 cout<<trip 将导致 os 成为 cout 的一个别名;而表达式 cenr <<trip 将导致 os 成为 cen
的一个别名。Time对象可以按值或按引用来传递,由于这两种情势都使函数能够使用对象的值。按引用传递使用的内存和时间都比按值传递少。
<<第二种重载版本
题目:
比方
修改方法
调用os类的引用以是
特殊的
头文件
- // mytime0.h-- Time class before operator overloading
- #ifndef MYTIME0_H_
- #define MYTIME0_H_
- class Time
- {
- private:
- int hours;
- int minutes;
- public:
- Time();
- Time(int h, int m = 0);
- void AddMin(int m);
- void AddHr(int h);
- void Reset(int h = 0, int m = 0);
- // Time Sum(const Time &t) const;
- Time operator+(const Time &t) const;
- Time operator-(const Time &t) const;
- Time operator*(double n) const;
- friend Time operator*(double m, const Time &t)
- {
- return t * m;
- } // inline definition
- friend std::ostream &operator<<(std::ostream &os, const Time &t);
- };
- #endif
复制代码 其中包括 operator*()和 operator<<()这两个友元函数。它将第-个友元函数作为内联函数,由于其代码很短(当界说同时也是原型时,就像这个例子中那样,要使用 fiend前缀)。
源代码
- // mytime0.cpp--implement Time methods
- #include <iostream>
- #include "mytime0.h"
- Time::Time()
- {
- hours = minutes = 0;
- }
- Time::Time(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- void Time::AddMin(int m)
- {
- minutes += m;
- hours += minutes / 60;
- minutes %= 60;
- }
- void Time::AddHr(int h)
- {
- hours += h;
- }
- void Time::Reset(int h, int m)
- {
- hours = h;
- minutes = m;
- }
- Time Time::operator+(const Time &t) const
- {
- Time sum;
- sum.minutes = minutes + t.minutes;
- sum.hours = hours + t.hours + sum.minutes / 60;
- sum.minutes %= 60;
- return sum;
- }
- Time Time::operator-(const Time &t) const
- {
- Time diff;
- int tot1, tot2;
- tot1 = t.minutes + 60 * t.hours;
- tot2 = minutes + 60 * hours;
- diff.minutes = (tot2 - tot1) % 60;
- diff.hours = (tot2 - tot1) / 60;
- return diff;
- }
- Time Time::operator*(double mult) const
- {
- Time result;
- long totalminutes = (minutes + 60 * hours) * mult;
- result.minutes = totalminutes % 60;
- result.hours = totalminutes / 60;
- return result;
- }
- std::ostream &operator<<(std::ostream &os, const Time &t)
- {
- os << t.hours << " hours, " << t.minutes << " minutes";
- return os;
- }
复制代码 源代码
- #include <iostream>
- #include "mytime0.h"
- int main()
- {
- using std::cout;
- using std::endl;
- Time aida(3, 35);
- Time tosca(2, 48);
- Time temp;
- cout << "Aida and Tosca:\n";
- cout << aida << ";" << tosca << endl;
- temp = aida + tosca; // operator+()
- cout << "Aida + Tosca:" << temp << endl;
- temp = aida * 1.17; // member operator*()
- cout << "Aida*l.17:" << temp << endl;
- cout << "10*Tosca:" << 10 * tosca << endl;
- return 0;
- }
复制代码
重载操作符:作为成员函数还是非成员函数
两种加法操作符
对于两种加法
界说操作符时间只能使用其中一种格式,否则会产生歧义。
重载:矢量类
头文件
- // vect.h--Vector class with<<,mode state
- #ifndef VECTOR_H_
- #define VECTOR_H_
- #include <iostream>
- namespace VECTOR
- {
- class Vector
- {
- private:
- double x;
- double y;
- double mag;
- char mode;
- double ang;
- void set_mag();
- void set_ang();
- void set_x();
- void set_y();
- public:
- Vector();
- Vector(double nl, double n2, char form = 'r');
- void set(double nl, double n2, char form = 'r');
- ~Vector();
- double xval() const { return x; } // report x value
- double yval() const { return y; } // report y value
- double magval() const { return mag; } // report magnitude
- double angval() const { return ang; } // report angle
- void polar_mode();
- void rect_mode(); // operator overloading
- Vector operator+(const Vector &b) const;
- Vector operator-(const Vector &b) const;
- Vector operator-() const;
- Vector operator*(double n) const;
- friend Vector operator*(double n, const Vector &a);
- friend std::ostream &operator<<(std::ostream &os, const Vector &v);
- };
- } // end namespace VECTOR
- #endif
复制代码 源代码
- // vector.cpp --methods for Vector class
- #include <cmath>
- #include "vector.h"
- #include <iostream>
- using std::atan2;
- using std::cos;
- using std::cout;
- using std::sin;
- namespace VECTOR
- {
- const double Rad_to_deg = 57.2957795130823;
- // private methods
- // calculates magnitude from x and y
- void Vector::set_mag()
- {
- mag = sqrt(x * x + y * y);
- }
- void Vector::set_ang()
- {
- if (x == 0.0 && y == 0.0)
- ang = 0.0;
- else
- ang = atan2(y, x);
- }
- // set xfrom polar coordinate
- void Vector::set_x()
- {
- x = mag * cos(ang);
- }
- // set y from polar coordinate
- void Vector::set_y()
- {
- y = mag * sin(ang);
- }
- // public methods
- Vector::Vector() // default constructor
- {
- x = y = mag = ang = 0.0;
- mode = 'r';
- }
- Vector::Vector(double n1, double n2, char form)
- {
- mode = form;
- if (form == 'r')
- {
- x = n1;
- y = n2;
- set_mag();
- set_ang();
- }
- else if (form == 'p')
- {
- mag = n1;
- ang = n2 / Rad_to_deg;
- set_x();
- set_y();
- }
- else
- {
- cout << "Incorrect 3rd argument to Vector()-- ";
- cout << "vector set to 0\n";
- x = y = mag = ang = 0.0;
- mode = 'r';
- }
- }
- // set vector from rectangular coordinates if form is r(the
- // default)or else from polar coordinates if form is p
- void Vector::set(double n1, double n2, char form)
- {
- mode = form;
- if (form == 'r')
- {
- x = n1;
- y = n2;
- set_mag();
- set_ang();
- }
- else if (form == 'p')
- {
- mag = n1;
- ang = n2 / Rad_to_deg;
- set_x();
- set_y();
- }
- else
- {
- cout << "Incorrect 3rd argument to Vector()-- ";
- cout << "vector set to 0\n";
- x = y = mag = ang = 0.0;
- mode = 'r';
- }
- }
- Vector::~Vector()
- {
- } // destructor
- void Vector::polar_mode() // set to polar mode
- {
- mode = 'p';
- }
- void Vector::rect_mode() // set to rectangular mode
- {
- mode = 'r';
- }
- // operator overloading
- // add two Vectors
- Vector Vector::operator+(const Vector &b) const
- {
- return Vector(x + b.x, y + b.y);
- }
- // subtract Vector b from a
- Vector Vector::operator-(const Vector &b) const
- {
- return Vector(x - b.x, y - b.y);
- }
- // reverse sign of Vector
- Vector Vector::operator-() const
- {
- return Vector(-x, -y);
- }
- // multiple vector by n
- Vector Vector::operator*(double n) const
- {
- return Vector(n * x, n * y);
- }
- // friend methods
- // multiply n byVector a
- Vector operator*(double n, const Vector &a)
- {
- return a * n;
- }
- // display rectangular coordinates if mode is r
- // else display polar coordinates if mode is p
- std::ostream &operator<<(std::ostream &os, const Vector &v)
- {
- if (v.mode == 'r')
- {
- os << "(x,y)=(" << v.x << "," << v.y << ")";
- }
- else if (v.mode == 'p')
- {
- os << "(m,a)=(" << v.mag << ","
- << v.ang * Rad_to_deg << ")";
- }
- else
- os << "Vector object mode is invalid";
- return os;
- }
- } // end namespace VECTOR
复制代码 使用状态成员
状态成员
为Vector类重载操作符
加法
可以使用的加法(无法使用极坐标)
改进
接纳的加法(简单)
乘法
友元内联函数来办理乘法交换次序的题目
对已重载的操作符进行重载
减法(二元操作符)
取反(一元操作符)
模拟随机游走
- // randwalk.cpp-…using the Vector class
- // compile with the vect.cpp file
- #include <iostream>
- // rand(),srand()prototypes// time()prototype
- #include <cstdlib>
- #include <ctime>
- #include "vector.h"
- int main()
- {
- using namespace std;
- using VECTOR::Vector;
- srand(time(0)); // seed random - number generator
- double direction;
- Vector step;
- Vector result(0.0, 0.0);
- unsigned long steps = 0;
- double target;
- double dstep;
- cout << "Enter target distance(q to quit):";
- while (cin >> target)
- {
- cout << "Enter step length:";
- if (!(cin >> dstep))
- break;
- while (result.magval() < target)
- {
- direction = rand() % 360;
- step.set(dstep, direction, 'p');
- result = result + step;
- steps++;
- }
- cout << "After " << steps << " steps, the subject"
- " has the following location : \n ";
- cout << result << endl;
- result.polar_mode();
- cout << " or\n " << result << endl;
- cout << " Average outward distance per step = "
- << result.magval() / steps << endl;
- steps = 0;
- result.set(0.0, 0.0);
- cout << "Enter target distance(qto quit):";
- }
- cout << "Bye!\n";
- return 0;
- }
复制代码
类的主动转换和强制范例转换
内置范例转换
将一个尺度范例变量的值赋给另一种尺度范例的变量时,如果这两种范例兼容,则C++主动将这个值转换为吸收变量的范例。
比方
不兼容的范例不能主动转换
但是可以进行强制范例转换
类的转换
可以将类界说成与基本范例或另一个类相关,使得从一种范例转换为另一种范例是故意义的。
stonewt.h
- // stonewt.h --definition forStonewtCass
- #ifndef STONEWT_H_
- #define STONEWT_H_
- 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); // constructor for double pounds
- Stonewt(int stn, double lbs); // constructor for stone,1bs
- Stonewt(); // default constructor
- ~Stonewt();
- void show_lbs() const;
- // show weight in pounds format
- void show_stn() const;
- // show weight in stone format}
- };
- #endif
复制代码
源文件
- #include <iostream>
- using std::cout;
- #include "stonewt.h"
- // 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 obiect 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
- {
- }
- // show weight in stones
- void Stonewt::show_stn() const
- {
- cout
- << stone << "stone," << pds_left << "pounds\n";
- }
- // show weight in pounds
- void Stonewt::show_lbs() const
- {
- cout << pounds << "pounds\n";
- }
复制代码 对于函数



继承两个参数的不可以不能作为转换函数

避免意外的范例转换
最新的C++实现新增了一个关键字(explicit),用来关闭这种主动特性。也就是说,可以这样声明构造函数:

但是仍可以进行显示强制范例转换

隐式转换
 不发生歧义下,会进行二步转换
下面两条语句都首先将int转换为double,然后使用 Stonewt(double)构造函数。

- #include <iostream>
- using std::cout;
- #include "stonewt.h"
- void display(const Stonewt &st, int n);
- int main()
- {
- Stonewt pavarotti = 260; // uses constructor to initialize
- Stonewt wolfe(285.7); // same as Stonewt wolfe =285.7
- Stonewt taft(21.8);
- cout << "The tenor weighed ";
- pavarotti.show_stn();
- cout << "The detective weighed ";
- wolfe.show_stn();
- cout << "The President weighed ";
- taft.show_lbs();
- pavarotti = 265.8;
- // uses constructor for conversion
- taft = 325; // same as taft= Stonewt(325);
- cout << "After dinner,the tenor weighed ";
- pavarotti.show_stn();
- cout << "After dinner,the President weighed ";
- taft.show_lbs();
- display(taft, 2);
- cout << "The wrestler weighed even more,\n";
- display(422, 2);
- cout << "No stone left unearned\n";
- return 0;
- }
- void display(const Stonewt &st, int n)
- {
- for (int i = 0; i < n; i++)
- {
- cout << "Wow!";
- st.show_stn();
- }
- }
复制代码
二步转换
转换函数
如果界说了转换函数下列就是可行的
转换函数声明
更改上述代码
- // stonewt.h --definition forStonewtCass
- #ifndef STONEWT_H_
- #define STONEWT_H_
- 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); // constructor for double pounds
- Stonewt(int stn, double lbs); // constructor for stone,1bs
- Stonewt(); // default constructor
- ~Stonewt();
- void show_lbs() const;
- // show weight in pounds format
- void show_stn() const;
- // show weight in stone format}
- // conversion functions
- operator int() const;
- operator double() const;
- };
- #endif
复制代码 源文件
- #include <iostream>
- using std::cout;
- #include "stonewt.h"
- // 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 obiect 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
- {
- }
- // show weight in stones
- void Stonewt::show_stn() const
- {
- cout
- << stone << "stone," << pds_left << "pounds\n";
- }
- // show weight in pounds
- void Stonewt::show_lbs() const
- {
- cout << pounds << "pounds\n";
- }
- Stonewt::operator double() const{ return pounds;}Stonewt::operator int() const{ return int(pounds + 0.5);}
复制代码 源文件
- #include <iostream>
- #include "stonewt.h"
- int main()
- {
- using std::cout;
- Stonewt poppins(9, 2.8); // 9 stone,2.8 pounds
- double p_wt = poppins; // implicit conversion
- cout << "Convert to double =>";
- cout << "Poppins:" << p_wt << " pounds.\n";
- cout << "Convert to int =>";
- cout << "Poppins:" << int(poppins) << " pounds.\n";
- return 0;
- }
复制代码
最好使用显式转换,而避免隐式转换。关键字 explicit 不能用于转换函数,但只需用一个功能雷同的非转换函数替换该转换数即可,但仅在被显式地调用时,该函数才会执行。也就是说,可以将:
转换函数和友元函数
重载加法的两种方式
成员函数
友元函数
下列合理
进行状态转换
只有友元函数答应
缘故起因:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |