【C++类和对象中:解锁面向对象编程的奇妙世界】(四)

简介: 【C++类和对象中:解锁面向对象编程的奇妙世界】

【C++类和对象中:解锁面向对象编程的奇妙世界】(三):https://developer.aliyun.com/article/1425465


我们来实现一下-=操作符重载和-操作符重载,这里仍然是让-操作符重载复用-=操作符重载

Date& operator-= (int day)
{
    //如果传入的day是负数
    if(day < 0)
    {
        return *this += (-day);
    }    
    _day -= day;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _year--;
            _month = 12;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}
Date operator-(int day)
{
    Date tmp = *this;
    tmp -= day;
    return tmp;
}


结果验证:


5.3 前置++和后置++重载


class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  // 前置++:返回+1之后的结果
  // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
  Date& operator++()
  {
    *this += 1;
    return *this;
  }
  // 后置++:
  // 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
  // C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器\
  自动传递
  // 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存\
    一份,然后给this + 1
  //       而temp是临时对象,因此只能以值的方式返回,不能返回引用
    Date operator++(int)
  {
    Date temp(*this);
    *this += 1;
    return temp;
  }
      Date& operator--()
  {
    *this -= 1;
    return *this;
  }
  Date operator--(int)
  {
    Date temp(*this);
    *this -= 1;
    return temp;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d;
  Date d1(2023, 10, 27);
  d = d1++;    // d: 2023,10,27   d1:2023,10,28
  d = ++d1;    // d: 2023,10,29   d1:2023,10,29
  return 0;
}


5.4 日期相减重载


// d1 - d2
Date operator-(const Date& d)
{
  //假设左大右小
  int flag = 1;
  Date max = *this;
  Date min = d;
  //假设错了,左小右大,此时相减就是负数
  if (*this < d)
  {
    max = d;
    min = *this;
    flag = -1;
  }
  int n = 0;
  while (min != max)
  {
    ++min;
    ++n;
  }
  return n * flag;
}


我们再来回顾一下我们上面的Print函数,每次该成员函数都要我们自己去实现,比较麻烦,我们可以通过运算符重载去实现打印,由于此时是自定义类型,所以可以通过重载流插入运算符实现。


cout是ostream类型的对象,cin是istream类型的对象


内置类型可以支持流插入操作,是因为库里面已经实现过了。


#include <iostream>
using namespace std;
class Date
{
public:
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void operator << (ostream& out)//cout是ostream的对象
    {
        out << _year << "年" << _month << "月" << _day << "日";
    }
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    Date d1(2023, 11, 5);
    cout << d1;//报错,why??
    d1 << cout;//运行成功!!
    return 0;
}


运行结果:


双操作数的运算符,第一个参数是左操作数,第二个操作数是右操作数,作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this,所以对于第二种写法,第一个参数是d1,第二个参数是cout,可以转化为d1.operator<<(&d1,cout),对于第一种写法参数就不匹配,所以报错。虽然第二种写法是正确的,但是看着不习惯,要想第一种此恶法正确,我们必须第一个参数设置为cout。因此对与该函数我们可以写成全局函数。

#include <iostream>
using namespace std;
class Date
{
public:
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
//private://此时必须注释,否则全局函数访问不到该变量
    int _year;
    int _month;
    int _day;
};
void operator << (ostream& out,Date &d)//cout是ostream的对象
{
    out << d._year << "年" << d._month << "月" << d._day << "日";
}
int main()
{
    Date d1(2023, 11, 5);
    cout << d1;
    //d1 << cout;
    return 0;
}

此时就转化成了operator<<(cout,d1),虽然上面我们确实通过将该函数放到全局变量中实现了我们的流插入操作符函数,但是使用该函数的时候我们必须要将类的成员变量设置公开,那么这样不就与封装性相悖吗?我们可以通过友元来解决。

#include <iostream>
using namespace std;
class Date
{
public:
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  //友元函数
    friend void operator << (ostream& out,Date &d);//cout是ostream的对象
private:
    int _year;
    int _month;
    int _day;
};
void operator << (ostream& out,Date &d)//cout是ostream的对象
{
    out << d._year << "年" << d._month << "月" << d._day << "日";
}
int main()
{
    Date d1(2023, 11, 5);
    cout << d1;
    //d1 << cout;
    return 0;
}


上面使用了友元函数,这里介绍一下,友元函数就是相当你有一个私人游泳池(相当于类的私有成员变量),然后你的非常要好朋友可以直接去你的私人游泳池游泳(访问私有变量),此时你不介意。我们的流插入操作符还可以支持连续使用,想要连续使用就必须设置一个返回值,所以我们要修改一下上面的代码

#include <iostream>
using namespace std;
class Date
{
public:
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  //友元函数
    friend ostream& operator << (ostream& out,const Date &d);//cout是ostream的对象
private:
    int _year;
    int _month;
    int _day;
};
//流插入的内容d不会改变,可以加上const修饰
ostream& operator << (ostream& out,const Date &d)//cout是ostream的对象
{
    out << d._year << "年" << d._month << "月" << d._day << "日";
  return out;
}
int main()
{
    Date d1(2023, 11, 5);
  Date d2;
  //赋值运算符顺序:从右往左
  //流插入运算符顺序:从左往右
  //由运算符的结合性决定
  //这里的返回值必须是cout
    cout << d1 << d2;
    return 0;
}

我们再来实现一下流提取运算符重载

#include <iostream>
using namespace std;
class Date
{
public:
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  //友元函数
    friend ostream& operator << (ostream& out,const Date &d);//cout是ostream的对象
  friend istream& operator >> (istream& in,Date &d);//cin是istream的对象
private:
    int _year;
    int _month;
    int _day;
};
//流插入的内容d不会改变,可以加上const修饰
ostream& operator << (ostream& out,const Date &d)//cout是ostream的对象
{
    out << d._year << "年" << d._month << "月" << d._day << "日";
  return out;
}
//流插入的内容d会改变,不可以加上const修饰
istream& operator >> (istream& in,Date &d)//cin是istream的对象
{
  in >> d._year >> d._month >> d._day;
  return in;
}
int main()
{
    Date d1(2023, 11, 5);
  Date d2;
  //赋值运算符顺序:从右往左
  //流插入运算符顺序:从左往右
  //由运算符的结合性决定
  //这里的返回值必须是cout
    cin >> d1 >> d2;
  cout << d1 << d2;
    return 0;
}


运行结果:

总结:其他的运算符一般实现成成员函数,流插入和流提取操作符必须实现到全局中,这样才能让流对象作第一个参数。流本质是为了解决自定义类型输入和输出问题,C语言的printf函数只能指定内置类型,同时打印输出的时候还要指定输出格式,C语言的printf函数和scanf函数无法解决自定义类型的输入输出问题。C++通过面向对象和运算符重载来解决。


6.日期类的实现


class Date
{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month)
    {
        static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
       31 };
        int day = days[month];
        if (month == 2
            && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
        {
            day += 1;
        }
        return day;
    }
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1);
    // 拷贝构造函数
 // d2(d1)
    Date(const Date& d);
    // 赋值运算符重载
 // d2 = d3 -> d2.operator=(&d2, d3)
    Date& operator=(const Date& d);
    // 析构函数
    ~Date();
    // 日期+=天数
    Date& operator+=(int day);
    // 日期+天数
    Date operator+(int day);
    // 日期-天数
    Date operator-(int day);
    // 日期-=天数
    Date& operator-=(int day);
    // 前置++
    Date& operator++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    // >运算符重载
    bool operator>(const Date& d);
    // ==运算符重载
    bool operator==(const Date& d);
    // >=运算符重载
    bool operator >= (const Date& d);
    // <运算符重载
    bool operator < (const Date& d);
    // <=运算符重载
    bool operator <= (const Date& d);
    // !=运算符重载
    bool operator != (const Date& d);
    // 日期-日期 返回天数
    int operator-(const Date& d);
private:
    int _year;
    int _month;
    int _day;
};


7.const成员


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


我们来看看下面的代码

class Date
{
public:
  Date(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  void Print()
  {
    cout << "Print()" << endl;
    cout << "year:" << _year << endl;
    cout << "month:" << _month << endl;
    cout << "day:" << _day << endl << endl;
  }
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
void Test()
{
  Date d1(2022, 1, 13);
  d1.Print();
  const Date d2(2022, 1, 13);
  d2.Print();
}
int main()
{
  Test();
  return 0;
}


当我们给对象加上const限制时,这里报错了为什么呢?


作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this,这个隐藏的this类型的Date* cosnt this,而我们传入的类型时const Date* this。这里就会存在权限放大的问题,我们需要在隐藏的this类型的前面加上cosnt,但是this类型是隐藏的,我们应该怎么加呢?C++规定在函数之后写上cosnt就可以对隐藏的this类型的前面加上cosnt。

class Date
{
public:
  Date(int year, int month, int day)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  void Print() const
  {
    cout << "Print()" << endl;
    cout << "year:" << _year << endl;
    cout << "month:" << _month << endl;
    cout << "day:" << _day << endl << endl;
  }
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
void Test()
{
  Date d1(2022, 1, 13);
  d1.Print();
  const Date d2(2022, 1, 13);
  d2.Print();
}
int main()
{
  Test();
  return 0;
}

加上之后非const类型对象也能运行,因为只是进行了权限的缩小。运行结果:


总结:类似于比较运算符,打印函数和+-运算符,不会修改任何成员,所以在写该类函数的时候我们可以加上const。成员函数定义的原则:1、能定义成cosnt的成员函数都应该定义成cosnt,这样const对象(权限平移)和非const对象(权限缩小)都可以调用。2、要修改成员变量的成员函数,不能定义成cosnt。3、流插入和流提取不能加上cosnt,它们不是成员函数,没有this指针,是全局函数。


请思考下面的几个问题:


1. const对象可以调用非const成员函数吗?不可以,权限不能放大

2. 非const对象可以调用const成员函数吗?可以,权限可以缩小

3. const成员函数内可以调用其它的非const成员函数吗?

4. 非const成员函数内可以调用其它的const成员函数吗?


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


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

class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  Date* operator&()
  {
    cout << "Date * operator&()" << endl;
    return this;
  }
  const Date* operator&()const
  {
    cout << "const Date * operator&()const" << endl;
    return this;
  }
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
int main()
{
  Date d1;
  const Date d2;
  cout << &d1 << endl;
  cout << &d2 << endl;
  return 0;
}


运行结果:


为什么这里要写两个取地址操作符重载呢?因为普通对象返回Date*,而const对象返回cosnt Date*,这两个是不同的,参数也是不同的,所以能构成重载。如果我们注释第一个非const成员函数。

class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  //Date* operator&()
  //{
  //  cout << "Date * operator&()" << endl;
  //  return this;
  //}
  const Date* operator&()const
  {
    cout << "const Date * operator&()const" << endl;
    return this;
  }
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
int main()
{
  Date d1;
  const Date d2;
  cout << &d1 << endl;
  cout << &d2 << endl;
  return 0;
}


运行结果:


此时我们发现就会调用非cosnt成员函数,此时发生了参数和返回值权限的缩小。这个就和我们吃饭一样,有好吃的有自己相吃的就去吃,没有的就将就一下。

class Date
{
public:
  Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
  //Date* operator&()
  //{
  //  cout << "Date * operator&()" << endl;
  //  return this;
  //}
  /*const Date* operator&()const
  {
    cout << "const Date * operator&()const" << endl;
    return this;
  }*/
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
int main()
{
  Date d1;
  const Date d2;
  cout << &d1 << endl;
  cout << &d2 << endl;
  return 0;
}


运行结果:


这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需 要重载,比如想让别人获取到指定的内容!

相关文章
|
3月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
84 0
|
3月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
164 0
|
5月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
161 12
|
6月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
6月前
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
6月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
7月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
6月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
124 16
|
7月前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
6月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
324 6