博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Visual C++ 2008入门经典 第八章深入理解类(二)
阅读量:5764 次
发布时间:2019-06-18

本文共 8548 字,大约阅读时间需要 28 分钟。

//第八章深入理解类//8.9 C++/CLI编程/*C++/CLI类支持运算符的重载,但它和本地C++类有一些区另应该掌握,首先是C++/CLI类与本地C++类中运算符重载之间的基本区别有两个区别前面已经介绍过了决不能在数值类中重载赋值运算符,因为将数值类对像赋值给另一个同类对像的过程已经被定义为一个成员一个成员的进行复制这一点不能改变,前面还提到,与本地类不同,引用类没有默认的赋值运算符如果我们需要以赋值运算符来处理引用类对像,则需要实现适当的函数另一个与本地C++类的区别是,C++/CLI类中实现的运算符重载的函数可以是类的静态成员,也可以是实例成员这意味着除了在本地C++的下下文中看到的运算符函数可以是带一个形参的实例函数或带两个形参的非成员函数这两种可能性以外,我们在C++/CLI为右还可以选择以带两个形参的静态成员函数的形式实现二元运算符同样,在C++/CLI中可以将前缀一运算符实现为不带形参的静态成员函数,最后,虽然本地C++允许重载new运算符但是C++/CLI类中我们却不能重载gcnew运算符*///8.9.1 在数值类中重载运算符//没看过书哦,少写了一章,value就是定久数值类的一个关键字吗?#include "stdafx.h"#include 
#include
using namespace std;using namespace System;/*value class Length{private: int feet; int inches;public: static initonly int inchesPerFoot = 12; //initonly意味着该常量不能被修改, Length(int ft, int ins):feet(ft), inches(ins) { } virtual String^ ToString() override { return feet+L" feet "+inches+" inches "; } //重载运算符operator+ Length operator+(Length len) { int inchTotal = inches + len.inches + inchesPerFoot * (feet+len.feet); //重新构造对像返回 return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); } //将operator+()函数定义成Length类的静态成员 static Length operator+(Length len1, Length len2) { //int inchTotal = inches + len.inches + inchesPerFoot * (feet+len.feet); int inchTotal = len1.inches + len2.inches + inchesPerFoot * (len1.feet+len2.feet); //重新构造对像返回 return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); }};*//*value class Length{private: int feet; int inches;public: static initonly int inchesPerFoot = 12; //initonly意味着该常量不能被修改, Length(int ft, int ins):feet(ft), inches(ins) { } virtual String^ ToString() override { return feet+L" feet "+inches+" inches "; } //重载运算符operator+ Length operator+(Length len) { int inchTotal = inches + len.inches + inchesPerFoot * (feet+len.feet); //重新构造对像返回 return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); } static Length operator*(double x, Length len); static Length operator*(Length len, double x);};int main(array
^args){ Length len1 = Length(6, 9); Length len2 = Length(7, 8); Console::WriteLine(L"{0} plus {1} is {2}",len1, len2, len1+len2); double factor = 2.5; Console::WriteLine(L"{0} times {1} is {2}", factor, len2, factor*len2); Console::WriteLine(L"{0} times {1} is {2}", factor, len2, len2*factor); system("pause"); return 0;}Length Length::operator*(double x, Length len){ int ins = safe_cast
(x*len.inches + x*len.feet*inchesPerFoot); return Length(ins/12, ins%12);}Length Length::operator*(Length len, double x){ return operator*(x, len); //颠倒一下实参,然后调用数值在前的乘法运算符版本}*///包含重载运算符的数值类/*#include "stdafx.h"#include
#include
using namespace std;using namespace System;value class Length{private: int feet; int inches;public: static initonly int inchesPerFoot = 12; Length(int ft, int ins):feet(ft), inches(ins) { } virtual String^ ToString() override { return feet+" feet "+inches+" inches "; } Length operator+(Length len) { int inchTotal = inches + len.inches + inchesPerFoot *(feet*len.feet); return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); } static Length operator/(Length len, double x) { int ins = safe_cast
((len.feet*inchesPerFoot + len.inches)/x); return Length(ins/inchesPerFoot, ins%inchesPerFoot); } static Length operator%(Length len1, Length len2) { int ins =(len1.feet * inchesPerFoot + len1.inches) % (len1.feet*inchesPerFoot + len2.inches); return Length(ins/inchesPerFoot, ins%inchesPerFoot); } static Length operator*(double x, Length len); static Length operator*(Length len, double x);}; Length Length::operator*(double x, Length len){ int inchs = safe_cast
(x*len.inches + x*len.feet * inchesPerFoot); return Length(inchs/inchesPerFoot, inchs%inchesPerFoot);}Length Length::operator*(Length len, double x){ return operator*(x, len);}int main(array
^args){ Length Len1 = Length(6, 9); Length Len2 = Length(7, 8); double factor = 2.5; Console::WriteLine(L"{0} + {1} = {2}", Len1, Len2, Len1+Len2); Console::WriteLine(L"{0} * {1} = {2}", factor, Len1, factor*Len2); Console::WriteLine(L"{0} * {1} = {2}", Len1, factor, factor*Len2); Console::WriteLine(L"({0} + {1}) / {2} = {3}", Len1, Len2, factor, (Len1+Len2)/factor); system("pause"); return 0;}*///8.9.2 重载递增和递减运算符/*#include "stdafx.h"#include
#include
using namespace std;using namespace System;value class Length{private: int feet; int inches;public: static initonly int inchesPerFoot = 12; Length(int ft, int ins):feet(ft), inches(ins) { } virtual String^ ToString() override { return feet+" feet "+inches+" inches "; } Length operator+(Length len) { int inchTotal = inches + len.inches + inchesPerFoot *(feet*len.feet); return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); } static Length operator/(Length len, double x) { int ins = safe_cast
((len.feet*inchesPerFoot + len.inches)/x); return Length(ins/inchesPerFoot, ins%inchesPerFoot); } static Length operator%(Length len1, Length len2) { int ins =(len1.feet * inchesPerFoot + len1.inches) % (len1.feet*inchesPerFoot + len2.inches); return Length(ins/inchesPerFoot, ins%inchesPerFoot); } static Length operator++(Length len) { ++len.inches; len.feet += len.inches/len.inchesPerFoot; len.inches %= len.inchesPerFoot; return len; } //前缀和后缀递增操作使用的应该是Length类中同一个运算符函数,因此输出证实两者都能正确工作 //是因为编译器能够确定是在递增操作数之前,还是在递增操作数之后在周围的静达式中使用该操作数的值 static Length operator*(double x, Length len); static Length operator*(Length len, double x);}; Length Length::operator*(double x, Length len){ int inchs = safe_cast
(x*len.inches + x*len.feet * inchesPerFoot); return Length(inchs/inchesPerFoot, inchs%inchesPerFoot);}Length Length::operator*(Length len, double x){ return operator*(x, len);}int main(array
^args){ Length Len1 = Length(1, 11); Console::WriteLine(Len1++); Console::WriteLine(++Len1); system("pause"); return 0;}*///8.9.3 在引用类中重载运算符#include "stdafx.h"#include
#include
using namespace std;using namespace System;ref class Length{private: int feet; int inches;public: static initonly int inchesPerFoot = 12; Length(int ft, int ins) : feet(ft), inches(ins){ } virtual String^ ToString() override { return feet+L" feet " + inches + L" inches"; } Length^ operator+(Length^ len) { int inchTotal = inches + len->inches + inchesPerFoot *(feet+len->feet); return gcnew Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot); } static Length^ operator/(Length^ len, double x) { int ins = safe_cast
((len->feet*inchesPerFoot + len->inches)/x); return gcnew Length(ins/inchesPerFoot, ins%inchesPerFoot); } static int operator/(Length^ len1, Length^ len2) { return (len1->feet*inchesPerFoot + len1->inches)/(len2->feet*inchesPerFoot + len2->inches); } static Length^ operator%(Length^ len1, Length^ len2) { int ins =(len1->feet * inchesPerFoot + len1->inches) % (len2->feet*inchesPerFoot + len2->inches); return gcnew Length(ins/inchesPerFoot, ins%inchesPerFoot); } static Length^ operator++(Length^ len) { Length^ temp = gcnew Length(len->feet, len->inches); ++temp->inches; temp->feet += temp->inches/temp->inchesPerFoot; temp->inches %= temp->inchesPerFoot; return temp; } //前缀和后缀递增操作使用的应该是Length类中同一个运算符函数,因此输出证实两者都能正确工作 //是因为编译器能够确定是在递增操作数之前,还是在递增操作数之后在周围的静达式中使用该操作数的值 static Length^ operator*(double x, Length^ len); static Length^ operator*(Length^ len, double x);}; Length^ Length::operator*(double x, Length^ len){ int ins = safe_cast
(x*len->inches +x*len->feet*inchesPerFoot); return gcnew Length(ins/inchesPerFoot, ins%inchesPerFoot);}Length^ Length::operator*(Length^ len, double x){ return operator*(x, len);}int main(array
^args){ //Length^ Len1 = gcnew Length(2, 6); //Length^ Len2 = gcnew Length(3, 5); //Length^ Len3 = gcnew Length(14, 6); Length^ Len1 = gcnew Length(2,6); // 2 feet 6 inches Length^ Len2 = gcnew Length(3,5); // 3 feet 5 inches Length^ Len3 = gcnew Length(14,6); // 14 feet 6 inches //Length^ total = 12 * (Len1 + Len2 + Len3) + (Len3 /gcnew Length(1,7))*Len2; Length^ total = 12*(Len1+Len2+Len3) + (Len3/gcnew Length(1,7))*Len2; Console::WriteLine(total); //Console::WriteLine(L"{0} can be cut into {1} pieces {2} long with {3} left over.", // Len3, Len3/Len1, Len1, Len3 %Len1); Length^ len5 = gcnew Length(1, 11); Console::WriteLine(len5++); Console::WriteLine(++len5); Console::WriteLine(len5); system("pause"); return 0;}//8.9.4 实现引用类型的赋值运算符Length% operator=(const Length% len){ if(this != %len){ feet = len.feet; inches = len.inches; } return *this;}

  

转载地址:http://hdgkx.baihongyu.com/

你可能感兴趣的文章
解决tomcat占用CPU过高
查看>>
Powershell进阶学习(4) Powershell强大的利器“管道”
查看>>
关于GNU GPL
查看>>
Entity Framework Code First实体对象变动跟踪
查看>>
request.getServletPath()和request.getPathInfo()用法
查看>>
nginx在响应request header时候带下划线的需要开启的选项
查看>>
工(程师)欲善其事,必先利其(编译)器——《Android Studio实战——快速、高效地构建Android应用》...
查看>>
Linux下DHCP服务器配置
查看>>
css相对定位和绝对定位
查看>>
计算机进阶推荐书单
查看>>
MEGAMALL OpenCart 自适应主题模板 ABC-0692
查看>>
6.1 压缩打包介绍;6.2 gzip压缩工具;6.3 bzip2压缩工具;6.4 xz压缩工具
查看>>
TCP 协议之三次握手(一)
查看>>
【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题
查看>>
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
mybatis学习笔记之学习目录(1)
查看>>
我的IDEA配置
查看>>
myeclipse显示行号
查看>>
Pro ASP.NET Core MVC 第6版 第二章(前半章)
查看>>
编写高性能的java程序
查看>>