赞
踩
目录
创建一个日期类。
- 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;
- }

成功实现运算符<<的重载。
如果是下面这种连续输出呢?
- 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;
- }
将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指针。
这时有一个新的间接方法:
这时我们就可以对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;
- };

这两个默认成员函数一般不用重新定义 ,编译器默认会生成
- 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
。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。