当前位置:   article > 正文

C++类与对象(5)—流运算符重载、const、取地址

流运算符重载

目录

一、流输出

1、实现单个输出

2、实现连续输出

二、流输入

  总结:

三、const修饰

四、取地址

.取地址及const取地址操作符重载

五、[ ]运算符重载


一、流输出

1、实现单个输出

创建一个日期类。

  1. class Date
  2. {
  3. public:
  4. Date(int year = 1, int month = 1, int day = 1)
  5. {
  6. _year = year;
  7. _month = month;
  8. _day = day;
  9. }
  10. private:
  11. int _year;
  12. int _month;
  13. int _day;
  14. };

以前我们在日期类中想要输出日期,都需要在类中自己创建一个用于输出的成员函数。

  1. void Print()
  2. {
  3. cout<< _year << "年" << _month << "月" << _day << "日" << endl;
  4. }

对于内置类型我们可以直接用cout<<输出其值。

我们也可以重载流提取<<实现输出内置类型的成员值,首先来了解一下cout的由来。

  • iostream 是 C++ 标准库中的一个头文件,它包含了用于输入和输出的流类的定义。iostream 头文件中定义了 istream 和 ostream 这两个基类,它们分别用于输入和输出操作。

  • ostream 是 iostream 头文件中定义的一个类,它是输出流的基类。ostream 类提供了输出操作的基本功能和接口,例如 << 操作符用于输出数据到流中。

  • cout 是 ostream 类的一个对象,它是标准输出流对象。cout 对象可以使用 << 操作符将数据输出到标准输出设备(通常是控制台)。

iostream 是一个头文件,ostream 是 iostream 中定义的输出流基类,而 cout 是 ostream 类的一个对象,用于将数据输出到标准输出设备。通过使用 cout 对象和 << 操作符,我们可以方便地将数据输出到控制台。

 现在在类中实现<<重载:

  1. void operator<<(ostream& out)
  2. {
  3. out << _year << "年" << _month << "月" << _day << "日" << endl;
  4. }

ostream& 是一个引用类型,表示对输出流对象的引用,通过使用 ostream& 引用类型,可以将输出流对象传递给操作符重载。

 当我们要使用运算符重载<<时,需要使用如下形式:

d1 << cout;//或者d1.operator<<(cout);

运算符重载<<的第一个参数为左操作数,第二个参数为右操作数。

虽然这种形式可以输出我们想要的结果,但这与我们使用的cout<<d1这种常规方式有所出入。

我们可以对其进行修改,将<<运算符重载作为全局函数,将输出流对象的引用作为第一个参数,日期类对象的引用作为第二个参数。

  1. void operator<<(ostream& out, const Date& d)
  2. {
  3. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  4. }

同时,为了使全局<<运算符重载能够访问到日期类对象d的私有成员变量,可以在日期类中创建友元函数声明,这样就可以访问对象的成员了。

friend void operator<<(ostream& out, const Date& d);

下面来测试一下: 

  1. class Date
  2. {
  3. friend void operator<<(ostream& out, const Date& d);
  4. public:
  5. Date(int year = 1, int month = 1, int day = 1)
  6. {
  7. _year = year;
  8. _month = month;
  9. _day = day;
  10. }
  11. void Print()
  12. {
  13. cout<< _year << "年" << _month << "月" << _day << "日" << endl;
  14. }
  15. private:
  16. int _year;
  17. int _month;
  18. int _day;
  19. };
  20. void operator<<(ostream& out, const Date& d)
  21. {
  22. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  23. }
  24. void Test()
  25. {
  26. Date a(2023, 11, 24);
  27. a.Print();
  28. cout << a;
  29. }
  30. int main()
  31. {
  32. Test();
  33. return 0;
  34. }

 成功实现运算符<<的重载。

 

2、实现连续输出

如果是下面这种连续输出呢?

  1. void Test2()
  2. {
  3. Date a(2023, 11, 24);
  4. Date b(2023, 11, 25);
  5. cout << a << b << endl;
  6. }

这时编译器会报错。 

 

 为了支持连续输出的形式,我们需要为<<重载增加返回值。

  1. ostream& operator<<(ostream& out, const Date& d)
  2. {
  3. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  4. return out;
  5. }

同时,友元函数声明也要修改一下:

friend ostream& operator<<(ostream& out, const Date& d);

 测试一下:

  1. class Date
  2. {
  3. friend ostream& operator<<(ostream& out, const Date& d);
  4. public:
  5. Date(int year = 1, int month = 1, int day = 1)
  6. {
  7. _year = year;
  8. _month = month;
  9. _day = day;
  10. }
  11. void Print()
  12. {
  13. cout<< _year << "年" << _month << "月" << _day << "日" << endl;
  14. }
  15. private:
  16. int _year;
  17. int _month;
  18. int _day;
  19. };
  20. ostream& operator<<(ostream& out, const Date& d)
  21. {
  22. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  23. return out;
  24. }
  25. void Test2()
  26. {
  27. Date a(2023, 11, 24);
  28. Date b(2023, 11, 25);
  29. cout << a << b << endl;
  30. }
  31. int main()
  32. {
  33. Test2();
  34. return 0;
  35. }

成功实现连续输出: 

二、流输入

iostreamistream 和 cin 是 C++ 中用于输入的相关类和对象。

  • iostream 是 C++ 标准库中的一个头文件,它包含了用于输入和输出的流类的定义。iostream 头文件中定义了 istream 和 ostream 这两个基类,分别用于输入和输出操作。

  • istream 是 iostream 头文件中定义的一个类,它是输入流的基类。istream 类提供了输入操作的基本功能和接口,例如 >> 操作符用于从流中读取数据。

  • cin 是 istream 类的一个对象,它是标准输入流对象。cin 对象可以使用 >> 操作符从标准输入设备(通常是键盘)读取数据。

总结:iostream 是一个头文件,istream 是 iostream 中定义的输入流基类,而 cin 是 istream 类的一个对象,用于从标准输入设备读取数据。通过使用 cin 对象和 >> 操作符,我们可以方便地从键盘输入数据。

 下面来实现流提取>>运算符重载,与流插入<<实现方式相同。

  1. istream& operator>>(istream& in, Date& d)
  2. {
  3. in >> d._year >> d._month >> d._day;
  4. return in;
  5. }

 测试一下

  1. class Date
  2. {
  3. friend ostream& operator<<(ostream& out, const Date& d);
  4. friend istream& operator>>(istream& in, Date& d);
  5. public:
  6. Date(int year = 1, int month = 1, int day = 1)
  7. {
  8. _year = year;
  9. _month = month;
  10. _day = day;
  11. }
  12. void Print()
  13. {
  14. cout<< _year << "年" << _month << "月" << _day << "日" << endl;
  15. }
  16. private:
  17. int _year;
  18. int _month;
  19. int _day;
  20. };
  21. istream& operator>>(istream& in, Date& d)
  22. {
  23. in >> d._year >> d._month >> d._day;
  24. return in;
  25. }
  26. void Test3()
  27. {
  28. Date d;
  29. cin >> d;
  30. cout << d;
  31. }
  32. int main()
  33. {
  34. Test3();
  35. return 0;
  36. }

成功实现流提取运算符重载。 

总结:

如果类的声明和定义是分文件的,我们一般把流提取和流插入运算符重载放到作为内联函数放到头文件中,这样省去了链接的过程,在编译过程就能call地址。

  1. inline ostream& operator<<(ostream& out, const Date& d)
  2. {
  3. out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
  4. return out;
  5. }
  6. inline istream& operator>>(istream& in, Date& d)
  7. {
  8. in >> d._year >> d._month >> d._day;
  9. return in;
  10. }
  • 在C++中,如果一个成员函数直接在类内部定义,它会被视为内联函数。内联函数的定义与声明都在类的定义中,这样编译器可以在调用处将函数的代码插入到调用位置,而不是通过函数调用的方式执行。
  • 通常情况下,短小的函数适合作为内联函数,因为内联函数的调用开销较小,可以避免函数调用的额外开销。将这样的函数定义在类内部可以方便地将其声明和定义放在一起,提高代码的可读性和维护性。
  • 然而,需要注意的是,编译器是否将一个在类内部定义的成员函数视为内联函数,最终还是由编译器决定。编译器可能会根据一些因素(如函数的复杂性、调用频率等)来决定是否将其内联展开。
  • 总之,将短小的函数定义在类内部可以被视为内联函数,这样可以提高代码的执行效率和可读性。但最终是否内联展开还是由编译器决定。

三、const修饰

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

我们来看下面代码:

  1. class A {
  2. public:
  3. void Print()
  4. {
  5. cout << _a << endl;
  6. }
  7. private:
  8. int _a = 1;
  9. };
  10. int main()
  11. {
  12. A aa;
  13. aa.Print();
  14. return 0;
  15. }

成功输出: 

如果用const修饰aa,这样可以吗? 

const A aa;

编译后程序报错: 

 

这是因为造成了权限放大的问题。

 Print函数的参数是A*this,aa的类型是const A*,所以aa调用Print函数会造成权限放大,而且如果权限平移,在this指针前用const修饰,这样也是禁止的,我们不能修改this指针。

这时有一个新的间接方法:

  • 语法规定叫const成员函数,const修饰*this,也就意味着this的类型变成const A*类型。
  • 内部不改变成员变量的成员函数时,最好加上const,const对象和普通对象都可以调用。

 这时我们就可以对C++类与对象(4)—日期类的实现这篇文章的Date.h文件中部分成员函数进行const修饰了。

  1. #include <iostream>
  2. #include <assert.h>
  3. using namespace std;
  4. class Date {
  5. public:
  6. Date(int year = 0, int month = 0, int day = 0);
  7. void Print();
  8. int GetMonthDay(int year, int month) const;
  9. bool operator==(const Date& d) const;
  10. bool operator!=(const Date& d) const;
  11. bool operator< (const Date& d) const;
  12. bool operator<=(const Date& d) const;
  13. bool operator> (const Date& d) const;
  14. bool operator>=(const Date& d) const;
  15. Date& operator+=(int day);
  16. Date operator+(int day) const;
  17. Date& operator-=(int day);
  18. // d1 - 100
  19. Date operator-(int day);
  20. // d1 - d2;
  21. int operator-(const Date& d) const;
  22. // ++d1
  23. Date& operator++();
  24. // d1++
  25. Date operator++(int);
  26. Date& operator--();
  27. Date operator--(int);
  28. private:
  29. int _year;
  30. int _month;
  31. int _day;
  32. };

四、取地址

.取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成

  1. class Date
  2. {
  3. public :
  4. Date* operator&()
  5. {
  6. return this ;
  7. }
  8. const Date* operator&()const
  9. {
  10. return this ;
  11. }
  12. private :
  13. int _year ; // 年
  14. int _month ; // 月
  15. int _day ; // 日
  16. };
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容。

五、[ ]运算符重载

  1. class Array
  2. {
  3. public:
  4. int& operator[](int i)
  5. {
  6. assert(i < 10);
  7. return _a[i];
  8. }
  9. const int& operator[](int i) const
  10. {
  11. assert(i < 10);
  12. return _a[i];
  13. }
  14. private:
  15. int _a[10];
  16. int _size;
  17. };
  18. void Func(const Array& aa)
  19. {
  20. for (int i = 0; i < 10; ++i)
  21. {
  22. //aa[i]++;
  23. cout << aa[i] << " ";
  24. }
  25. }

首先,我们来看Array类:

  • Array类定义了一个私有的整型数组_a,大小为10,以及一个私有的整型变量_size

  • Array类重载了[]运算符,这样我们就可以像使用普通数组一样使用Array类的对象。

  • operator[]函数有两个版本,一个是非常量版本,一个是常量版本。非常量版本返回一个可修改的引用,常量版本返回一个不可修改的常量引用。

  • operator[]函数中,使用了assert函数来确保索引i小于10,防止数组越界。

然后,我们来看Func函数:

  • Func函数接受一个Array类的常量引用作为参数。因为参数是常量引用,所以我们不能在函数中修改参数的值。

  • Func函数中,有一个循环,循环变量i从0遍历到9。在循环体中,首先注释掉了aa[i]++,这是因为aa是一个常量引用,我们不能修改它的值。然后,使用cout打印出aa[i]的值,然后打印一个空格。

举个例子,如果我们创建一个Array类的对象a,并初始化_a数组为0到9,然后调用Func(a),那么控制台上会打印出0 1 2 3 4 5 6 7 8 9

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号