手机版
你好,游客 登录 注册
背景:
阅读新闻

C++中使用类(重载,友元函数,转换函数等)

[日期:2012-08-14] 来源:Linux社区  作者:paul_wuhaha [字体:大 中 小]
类的自动转换就是,如果你定义了一个只接受一个参数的构造函数,那么在调用构造函数时,如果碰到匹配的参数,直接可以将一个基本类型转换为相应的类类型。其中有几种隐式转换的情况。

1.将某个对象初始化为某个值时。

2.将某个值赋值某个对象。

3.将某值传递给接收某对象参数的函数时。

4.返回值被声明为某个对象的函数返回一个某值时。

5.以上情况中,如果另一个值可以转化为某值时。

使用explicit关键字可以强制使用显式构造函数,可以避免把某值转化为某对象。

如果想进行相反的转换,那么就需要使用C++操作符函数-------转换函数。转换函数是用户自定义的强制类型转换。使用转换函数得注意以下三点:

1.转换函数必须是类方法。

2.转换函数不能指定返回类型。

3.转换函数不能有参数。

同样的,也上三段代码。

stonewt.h
  1. #ifndef STONEWT_H_   
  2. #define STONEWT_H_   
  3. class Stonewt  
  4. {  
  5. private:  
  6.     enum{Lbs_per_stn = 14};  
  7.     int stone;  
  8.     double pds_left;  
  9.     double pounds;  
  10. public:  
  11.     Stonewt(double lbs);  
  12.     Stonewt(int stn, double lbs);  
  13.     Stonewt();  
  14.     ~Stonewt();  
  15.     void show_lbs()const;  
  16.     void show_stn()const;  
  17.   
  18.     // 转换函数,将对象转换为内置类型   
  19.     operator int()const;  
  20.     operator double()const;  
  21. };  
  22.   
  23. #endif  
stonewt.cpp
  1. #include <iostream>   
  2. using std::cout;  
  3. #include "stonewt.h"   
  4.   
  5. Stonewt::Stonewt(double lbs)  
  6. {  
  7.     stone = int(lbs)/Lbs_per_stn;  
  8.     pds_left = int(lbs)%Lbs_per_stn + lbs - int(lbs);  
  9.     pounds = lbs;  
  10. }  
  11.   
  12. Stonewt::Stonewt(int stn, double lbs)  
  13. {  
  14.     stone = stn;  
  15.     pds_left = lbs;  
  16.     pounds = stn * Lbs_per_stn + lbs;  
  17. }  
  18.   
  19. Stonewt::Stonewt()  
  20. {  
  21.     stone = pounds = pds_left = 0;  
  22. }  
  23.   
  24. Stonewt::~Stonewt()  
  25. {  
  26. }  
  27.   
  28. void Stonewt::show_stn()const  
  29. {  
  30.     cout << stone << " stone." << pds_left << " pounds\n";  
  31. }  
  32.   
  33. void Stonewt::show_lbs()const  
  34. {  
  35.     cout << pounds << " pounds\n";  
  36. }  
  37.   
  38. // 转换方法 不带参数,不带返回值类型   
  39. Stonewt::operator int() const  
  40. {  
  41.     return int(pounds+0.5);  
  42. }  
  43.   
  44. Stonewt::operator double() const  
  45. {  
  46.     return pounds;    
  47. }  
stone.cpp
  1. #include <iostream>   
  2. using std::cout;  
  3. #include "stonewt.h"   
  4. void display(const Stonewt st, int n);  
  5. int main()  
  6. {  
  7.     Stonewt pavarotti = 260;        //调用构造函数,因为260能转化为浮点型   
  8.     Stonewt wolfe(285.7);       //类似Stonewt wolfe = 285.7;   
  9.     Stonewt taft(21,8);  
  10.   
  11.     cout << "The tenor weighted ";  
  12.     pavarotti.show_stn();  
  13.     cout << "THe detective weighted ";  
  14.     wolfe.show_stn();  
  15.     cout << "The President weighed ";  
  16.     taft.show_lbs();  
  17.     pavarotti = 256.8;          //构造函数进行初始化   
  18.     taft = 325;             //和 taft = Stonewt(325);一样   
  19.     cout << "After dinner, the tenor weighed ";  
  20.     pavarotti.show_stn();  
  21.     cout << "After dinner, the president weighed ";  
  22.     taft.show_lbs();  
  23.     display(taft, 2);  
  24.     cout << "The wrestler weighted even more.\n";  
  25.     display(422, 2);            //将422转化乘 Stonewt对象   
  26.     cout << "No stone left unearned\n";  
  27.   
  28.     // 使用转换函数将类对象转化为基本类型   
  29.     Stonewt poppins(9, 28);  
  30.     double p_wt = poppins;  
  31.     cout << "COnvert to double => ";  
  32.     cout << "Poppins: " << p_wt << " pounds.\n";  
  33.     cout << "Convert to int => ";  
  34.     cout << "Poppins: " << int(poppins) << " pounds.\n";  
  35.     return 0;  
  36. }  
  37.   
  38. void display(const Stonewt st, int n)  
  39. {  
  40.     for (int i = 0; i < n; i++)  
  41.     {  
  42.         cout << "Wow! ";  
  43.         st.show_stn();  
  44.     }  
  45. }  
linux
相关资讯       C++ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款