主页 > 人工智能  > 

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

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

目录

一、流输出

1、实现单个输出

2、实现连续输出

二、流输入

  总结:

三、const修饰

四、取地址

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

五、[ ]运算符重载


一、流输出 1、实现单个输出

创建一个日期类。

class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } private: int _year; int _month; int _day; };

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

void Print() { cout<< _year << "年" << _month << "月" << _day << "日" << endl; }

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

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

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

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

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

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

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

void operator<<(ostream& out) { out << _year << "年" << _month << "月" << _day << "日" << endl; }

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

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

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

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

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

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

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

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

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

下面来测试一下: 

class Date { friend void operator<<(ostream& out, const Date& d); public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout<< _year << "年" << _month << "月" << _day << "日" << endl; } private: int _year; int _month; int _day; }; void operator<<(ostream& out, const Date& d) { out << d._year << "年" << d._month << "月" << d._day << "日" << endl; } void Test() { Date a(2023, 11, 24); a.Print(); cout << a; } int main() { Test(); return 0; }

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

 

2、实现连续输出

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

void Test2() { Date a(2023, 11, 24); Date b(2023, 11, 25); cout << a << b << endl; }

这时编译器会报错。 

 

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

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

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

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

 测试一下:

class Date { friend ostream& operator<<(ostream& out, const Date& d); public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout<< _year << "年" << _month << "月" << _day << "日" << endl; } private: int _year; int _month; int _day; }; ostream& operator<<(ostream& out, const Date& d) { out << d._year << "年" << d._month << "月" << d._day << "日" << endl; return out; } void Test2() { Date a(2023, 11, 24); Date b(2023, 11, 25); cout << a << b << endl; } int main() { Test2(); return 0; }

成功实现连续输出: 

二、流输入

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

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

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

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

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

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

istream& operator>>(istream& in, Date& d) { in >> d._year >> d._month >> d._day; return in; }

 测试一下

class Date { friend ostream& operator<<(ostream& out, const Date& d); friend istream& operator>>(istream& in, Date& d); public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout<< _year << "年" << _month << "月" << _day << "日" << endl; } private: int _year; int _month; int _day; }; istream& operator>>(istream& in, Date& d) { in >> d._year >> d._month >> d._day; return in; } void Test3() { Date d; cin >> d; cout << d; } int main() { Test3(); return 0; }

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

总结:

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

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

我们来看下面代码:

class A { public: void Print() { cout << _a << endl; } private: int _a = 1; }; int main() { A aa; aa.Print(); return 0; }

成功输出: 

如果用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修饰了。

#include <iostream> #include <assert.h> using namespace std; class Date { public: Date(int year = 0, int month = 0, int day = 0); void Print(); int GetMonthDay(int year, int month) const; bool operator==(const Date& d) const; bool operator!=(const Date& d) const; bool operator< (const Date& d) const; bool operator<=(const Date& d) const; bool operator> (const Date& d) const; bool operator>=(const Date& d) const; Date& operator+=(int day); Date operator+(int day) const; Date& operator-=(int day); // d1 - 100 Date operator-(int day); // d1 - d2; int operator-(const Date& d) const; // ++d1 Date& operator++(); // d1++ Date operator++(int); Date& operator--(); Date operator--(int); private: int _year; int _month; int _day; }; 四、取地址 .取地址及const取地址操作符重载

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

class Date { public : Date* operator&() { return this ; } const Date* operator&()const { return this ; } private : int _year ; // 年 int _month ; // 月 int _day ; // 日 }; 这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需 要重载,比如想让别人获取到指定的内容。 五、[ ]运算符重载 class Array { public: int& operator[](int i) { assert(i < 10); return _a[i]; } const int& operator[](int i) const { assert(i < 10); return _a[i]; } private: int _a[10]; int _size; }; void Func(const Array& aa) { for (int i = 0; i < 10; ++i) { //aa[i]++; cout << aa[i] << " "; } }

首先,我们来看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 。

标签:

C++类与对象(5)—流运算符重载、const、取地址由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C++类与对象(5)—流运算符重载、const、取地址