{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Realbreal aimagbimag beijing university

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: operator<<(ostream&, const string&); } int main() { char* p = "Hello"; std::string s = "world"; std::cout << p << ", " << s << "!\n"; } // 本例没有使用 // using namespace std; // 而是使用了std::string和 // std::cout // 按照最好的方式行事,没 // 有污染全局名字空间,也 // 没有引进不必要的依赖性 Beijing University of Posts & Telecommunications Beijing [email protected] 二元运算符的解析方式 考虑二元运算符@,如果x的类型为X而y的类型为 Y,[email protected]将按照如下方式解析 若X是类,查询作为X的成员函数或者X的某个基类的成 员函数的[email protected] 在围绕[email protected]的环境中查询[email protected]的声明 若X在名字空间N里定义,在N里查询[email protected]的声明 若Y在名字空间M里定义,在M里查询[email protected]的声明 当查询到[email protected]重载时,按照重载规则处理 一元运算符的解析方式与之类似 Beijing University of Posts & Telecommunications Beijing [email protected] 11.3 一个复数类型 我们希望实现一个复数类,可以进行如下操作 此外,还希望提供一些运算符,例如以==做比 较,<<做输出等 void f() { complex a=complex(1,2); complex b=3; complex c=a+2.3; complex d=2+b; complex e=-b-c; b=c*2*c; } Beijing University of Posts & Telecommunications Beijing [email protected] 11.3.1 成员运算符和非成员符 作者喜欢让尽量少的函数能直接去操作对象 的表示,因此,作者只在类本身中定义那些 本质上需要修改第一个参数值的运算符,如 +=,而像+这样基于参数的值简单产生新 值的运算符,则在类之外利用基本运算符来 定义 Beijing University of Posts & Telecommunications Beijing [email protected] 成员运算符和非成员符示例 class complex { double re, im; public: complex& operator+=(complex a); }; complex operator+ (complex a, complex b) { complex r=a; return r+=b; } complex& complex::operator+= (complex a) { re += a.re; im += a.im; return *this; } void f(complex x, complex y, complex z) { complex r1=x+y+z; // r1 = operator+ // (operator+(x,y), z) complex r2=x; r2+=y; // r2.operator+=(y) r2+=z;} Beijing University of Posts & Telecommunications Beijing [email protected] 11.3.2 混合模式算术 为了处理complex d=2+b; 我们需要能够接受不 同类型的运算对象的+运算符,为此,可以简单地 增加运算符的版本来解决这个问题 class complex complex& operator+= { (double a); double re, im; { public: re += a; complex& operator+= return *this; (complex a) } { re += a.re; im += a.i...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern