学习C++——类的自动转换和强制类型转换
2015-01-13 11:22
141 查看
类的自动转换和强制类型转换
/* 本程序主要的用法是: 1、类型转换函数的定义以及使用。 (double类型赋值给对象类型,此时构造函数可以实现; 对象类型赋值给double类型,此时用转换函数来实现。) 2、成员函数实现加法的重载。 3、友元函数实现加法的重载。 */ #ifndef STONEWT_H_ #define STONEWT_H_ class Stonewt { private: enum {Lbs_per_stn = 14};//每14磅等于1英石 int stone;//英石 double pds_left;// double pounds;//磅 public: Stonewt(double lbs);//单位是磅 Stonewt(int stn, double lbs);//单位是英石和磅 Stonewt(); ~Stonewt(); void show_lbs() const; void show_stn() const; /* 转换函数:因为上面可以知道,存在Stonewt wolfe(25.6); 也就是可以将一个double类型赋值给Stonewt对象,但是能否将 一个Stonewt对象赋值给一个double类型,即double host =? wolfe; 这就需要用到转换函数了。转换函数的原型是:operator double(); operator int();转换函数是类方法,需要通过类对象来调用,从而告知函数要转换的值。 */ //转换函数的声明 operator double () const; operator int () const; //使用成员函数 //Stonewt operator+(const Stonewt & st) const; //使用友元函数来重载加法 friend Stonewt operator+(const Stonewt & st1, const Stonewt & st2); }; #endif
#include <iostream> using std::cout; #include "16.h" Stonewt::Stonewt(double lbs)//构造函数 { stone = int (lbs) / Lbs_per_stn; pds_left = int (lbs) % Lbs_per_stn + lbs - int (lbs); pounds = lbs; } Stonewt::Stonewt(int stn, double lbs)//构造函数 { stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs; } Stonewt::Stonewt()//默认构造函数 { stone = pounds = pds_left = 0; } Stonewt::~Stonewt()//析构函数 { } void Stonewt::show_lbs() const//显示函数,显示磅 { cout << pounds << " pounds\n"; } void Stonewt::show_stn() const//显示函数,显示英石 { cout << stone << " stone, " << pds_left << " pounds\n"; } //转换函数的定义 Stonewt::operator double () const { return pounds; } Stonewt::operator int() const { return int (pounds + 0.5); } //使用成员函数重载加法 /*Stonewt Stonewt::operator+(const Stonewt & st) const { double pds = pounds + st.pounds; Stonewt sum(pds); return sum; }*/ //使用友元函数重载加法 Stonewt operator+(const Stonewt & st1, const Stonewt & st2) { double pds = st1.pounds + st2.pounds; Stonewt sum(pds); return sum; }
#include <iostream> #include "16.h" using std::cout; void display(const Stonewt & st, int n); int main() { Stonewt incognito = 275; Stonewt wolfe(287.7); Stonewt taft(21, 8); cout << "The incognito weighed "; incognito.show_stn(); cout << "The wolfe weighed "; wolfe.show_stn(); cout << "The taft weighed "; taft.show_lbs(); incognito = 276.8; /* incognito = 276.8;使用接收double参数的构造函数, 将276.8转换为一个Stonewt值,这将把incognito的 pound成员设置为276.8。因为语句使用了构造函数, 所以还将设置shone,pds_left成员。 */ taft = 325; /* 将一个int值转换为double类型,然后使用Stonewt(double) 来设置全部3个成员 */ cout << "\nincognito = 276.8;the incognito weighed "; incognito.show_stn(); cout << "taft = 325;the taft weighed "; taft.show_lbs(); cout << "\ndisplay(taft, 2);显示的结果:\n"; display(taft, 2); cout << "\ndisplay(422, 2);显示的结果:\n"; display(422, 2); /* 先将422,转换为一个double数,然后使用Stonewt(double) 将其转换为一个Stonewt对象。 */ cout << "\n验证转换函数,定义了double变量poun:\n"; double poun; poun = taft; cout << "poun = taft = " << poun << "\n"; cout << "\n成员函数重载加法:\n"; Stonewt st1 (12.3); Stonewt st2 (11.4); Stonewt sum_st; sum_st = st1 + st2; /* 使用友元函数或者是使用成员函数实现加法重载, 但是不能同时定义两个函数,只能定义其中一个。 */ cout << "sum_st = " ; sum_st.show_lbs(); std::cin.get(); return 0; } void display(const Stonewt & st, int n) { for(int i = 0; i<n; i++) { cout << "wow! "; st.show_stn(); } }
相关文章推荐
- AVA学习笔记之变量类型转换和类型自动提升
- 【JAVA学习】java基本数据类型与字符串之间的转换(基本数据类型、对象封装类、自动装箱、自动拆箱)
- java学习笔记(常量 二进制 其他进制 负数进制 变量 自动类型提升 强制类型转换 )
- Struts 1 学习笔记-4-3(Struts中ActionForm的类型自动转换)
- C++ 类的自动转换与强制类型转换
- Hibernate学习-11:持久化对象状态及状态转换、持久态对象自动更新数据库
- 类的自动转换和强制类型转换
- 第十一章:运算符重载,友元函数,重载<<运算符,类的自动转换和强制类型转换
- java学习之旅10--基本数据类型自动转换_强制转型_类型提升问题
- java重载类型自动转换学习
- 【android学习】EditText输入监听,小写字母自动转换为大写字母
- 黑马程序员—Java基础学习笔记之数据类型转换(自动转换和强制转换)
- 黑马程序员—C学习笔记—取余和除法运算符、自动和强制类型转换
- 《C++ primer plus》:类的自动转换和强制类型转换
- C++类的自动转换和强制类型转换
- 类的自动转换和强制类型转换
- 【Java学习3.5.1】自动类型转换2
- 类的自动转换和强制类型转换
- CXF学习(4) 处理无法自动转换的复合数据类型
- 数据类型转换,自动转换和强制类型转换实例