C++练级之路——类和对象(中二)

简介: C++练级之路——类和对象(中二)

1、运算符重载


       C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也是具有其返回值类型,函数名字以及参数列表,其返回值类型和参数列表与普通的函数类似。


函数名字为:关键字operator后面接需要重载的运算符符号;

函数原型:返回值类型 operator操作符(参数列表)。

注意:


1.不能通过链接其他符号来创建新的操作符:比如 operator@;

2.重载操作符必须有一个类类型参数;

3.用于内置类型的运算符,其含义不能改变,例如:内置类型的+,不能改变其含义;

4.作为类成员函数重载时,其形参看起来比操作书数数目少1,因为成员函数的第一个参数为隐藏的this;

5.  .*   ::     sizeof     ?:    .    注意以上五个运算符不能重载,这个经常在笔试选择题中出现。

//运算符重载
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);
 
//函数实现
bool Date::operator<(const Date& d)
{
  if (_year < d._year)
    return true;
 
  else if (_year == d._year)
  {
    if (_month < d._month)
      return true;
 
    else if (_month == d._month)
      return _day < d._day;
  }
  return false;
}
 
bool Date::operator==(const Date& d)
{
  return _year == d._year
    && _month == d._month
    && _day == d._day;
}
 
bool Date::operator<=(const Date& d)
{
  return *this < d && *this == d;
}
bool Date::operator>(const Date& d)
{
  return !(*this <= d);
}
bool Date::operator>=(const Date& d)
{
  return !(*this <d);
}
bool Date::operator!=(const Date& d)
{
  return !(*this == d);
}


当我们重载了  <  和 ==  时,就可以复用这两个运算符,重载<=  >   >=   != ,更加方便了。

2、赋值运算符重载

1.参数类型:const  参数名&,传递引用可以提高传参效率,(不用再调用拷贝构造了);

2.返回值类型:参数名&  返回引用可以提高返回的效率,有返回值的目的是为了支持连续赋值;

3.检测是否自己给自己赋值

4.返回this,要符合连续赋值的含义;


//声明
Date& operator=(const Date& d);
 
//定义
Date& Date:: operator=(const Date& d)
{
  _year = d._year;
  _month = d._month;
  _day = d._day;
  return *this;
}

2、赋值运算符只能重载成成员函数,不能重载成全局函数

因为赋值运算符第一个参数是this指针,重载成全局函数,就需要传this指针,而当类里面没有显式定

义赋值运算符时,编译器会自动生成一个默认的。此时就会和类外的赋值运算符重载形成冲突,所以赋值运算符只能是成员函数。



3、用户没有显式实现时,编译器会自动生成一个默认的赋值操作符重载,以值的方式逐字节拷贝


注意:内置类型成员变量是直接赋值的,而自定义成员变量需要调用对应类的赋值运算符重载完成赋值,如果自定义类中没有显式实现赋值运算符重载,编译器也会默认生成赋值重载;

class Time
{
public:
  Time()
  {
    _hour = 1;
    _minute = 1;
    _second = 1;
  }
    //这个赋值运算符重载写不写都可以,不写的话编译器也会自动生成
  /*Time& operator=(const Time& t)
  {
    if (this != &t)
    {
      _hour = t._hour;
      _minute = t._minute;
      _second = t._second;
    }
    return *this;
  }*/
  
//private:
  int _hour;
  int _minute;
  int _second;
};
 
class Date1
{
public:
  void print()
  {
    cout << _year << "-" << _month << "-" << _day << endl;
  }
private:
  // 基本类型(内置类型)
  int _year = 1970;
  int _month = 1;
  int _day = 1;
  // 自定义类型
  Time _t;
};
int main()
{
  Date1 d1;
  Date1 d2;
  d1 = d2;
 
  d1.print();
  d2.print();
  return 0;
}


但是,我们真的不需要自己写了吗?

不是的,如果类中涉及到资源管理,开辟空间的,就要自己实现赋值重载了,因为编译器自己实现的是浅拷贝,也就是值拷贝,不会额外开辟空间,所以我们自己写深拷贝,和拷贝构造,析构函数差不多。



3、前置++和后置++重载


前置++,返回+1之后的结果,

注意:this指向的对象函数结束后不会销毁,故用引用的方式返回提高效率;

后置++,返回+1之前的结果

为了能够区分


C++规定:后置++重载时多增加一个Int 参数,但调用函数时,用户不用传递,编译器会自动传递,(问就是C++规定的)

后置++,要用值的方式返回,因为要在函数内创建一个临时对象tem来保存*this,然*this++


然后返回tem,

//前置++
Date& Date::operator++()
{
  *this += 1;
  return *this;
}
 
//后置++
//int 只是一个标志,代表他是后置的--,没有实际意义
Date Date::operator++(int)
{
  Date tem = *this;
  *this += 1;
  return tem;
}
 
//前置--
Date& Date::operator--()
{
  *this -= 1;
  return *this;
}
 
//后置--
Date Date::operator--(int)
{
  Date tem = *this;
  *this -= 1;
  return tem;
}


4、const成员变量

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

class Date1
{
public:
  Date1(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;
  }
  void Print() const
  {
    cout << "Print()const" << endl;
    cout << "year:" << _year << endl;
    cout << "month:" << _month << endl;
    cout << "day:" << _day << endl << endl;
  }
private:
  int _year; // 年
  int _month; // 月
  int _day; // 日
};
void Test()
{
  Date1 d1(2022, 1, 13);
  d1.Print();
  const Date1 d2(2022, 1, 13);
  d2.Print();
}
  int main()
{
    Test();
    return 0;
}


注意:权限可以缩小,平移,但是不可以放大;

请思考下面的几个问题:


1. const对象可以调用非const成员函数吗?

2. 非const对象可以调用const成员函数吗?

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

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


5、日期类的实现


       我们还可以重载流插入和流提取

流插入我们要在类外实现,因为在类中实现,第一个参数是隐形的this指针,而我们希望第一个参数

是ostream& out,那我们就定义在类外可以解决这个问题,但是定义在类外我们就无法访问类中的私有的成员变量,就用到了另一个办法,友元,友元的概念就是我是你的朋友,我可以访问你的元素,不管是公有还是私有,这里暂且了解一下,下节会讲;


下面来看日期类的实现,上面的运算符重载都会用到;

//Date.h
#pragma once
#include<iostream>
using namespace std;
 
int is_year(int y);
 
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);
  Date(const Date& d);
  //在类里面定义的函数默认就内联函数
  int GetMonthDay(int y, int m)
  {               
    static int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (m == 2)
      return months[m] + is_year(y);
    return months[m];
  }
 
  //日期+=天数
  Date& operator+=(int day);
  Date operator+(int day);
  Date& operator-=(int day);
  Date operator-(int day);
 
  //++和--
  Date& operator++();
  Date operator++(int);
  Date& operator--();
  Date operator--(int);
 
  //日期-日期
  int operator-(const Date& d);
 
 
  //运算符重载
  Date& 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);
  bool operator!=(const Date& d);
 
 
  //流插入和流输出
  ~Date();
  void print();
 
private:
  int _year;
  int _month;
  int _day;
};
 
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);
2.


//Date.cpp
 
#include"Date.h"
 
 
int is_year(int y)
{
  if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
    return 1;
  return 0;
}
  Date::Date(int year , int month , int day )
  {
    _year = year;
    _month = month;
    _day = day;
  }
  Date::Date(const Date& d)
  {
    cout << "Date::Date(const Date& d)" << endl;
    _year = d._year;
    _month = d._month;
    _day = d._day;
  }
    
  Date& Date::operator+=(int day)
  {
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
      _day -= GetMonthDay(_year, _month);
      _month++;
      if (_month == 13)
      {
        _year++;
        _month = 1;
      }
    }
    return *this;
  }
 
  Date Date::operator+(int day)
  {
    Date tem = *this;
    tem += day;
    return tem;
  }
 
  Date& Date::operator-=(int day)
  {
    _day -=day;
    while (_day < 0)
    {
      _month--;
      if (_month == 0)
      {
        _year--;
        _month = 12;
      }
      _day += GetMonthDay(_year, _month);
    }
    return *this;
  }
 
  Date Date::operator-(int day)
  {
    Date tem = *this;
    tem -= day;
    return tem;
  }
 
 
  //前置++
  Date& Date::operator++()
  {
    *this += 1;
    return *this;
  }
 
  //后置++
  //int 只是一个标志,代表他是后置的--,没有实际意义
  Date Date::operator++(int)
  {
    Date tem = *this;
    *this += 1;
    return tem;
  }
 
  //前置--
  Date& Date::operator--()
  {
    *this -= 1;
    return *this;
  }
 
  //后置--
  Date Date::operator--(int)
  {
    Date tem = *this;
    *this -= 1;
    return tem;
  }
 
  //日期-日期
  int Date:: operator-(const Date& d)
  {
    Date max = *this;
    Date min = d;
    int n = 0;
    int flag = 1;
    if (max < min)
    {
      max = d;
      min = *this;
      flag = -1;
    }
 
    while (min!=max)
    {
      ++min;
      ++ n;
    }
    return n*flag;                                                 
  }
 
 
 
  //赋值运算符重载
  Date& Date:: operator=(const Date& d)
  {
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
  }
 
  bool Date::operator<(const Date& d)
  {
    if (_year < d._year)
      return true;
 
    else if (_year == d._year)
    {
      if (_month < d._month)
        return true;
 
      else if (_month == d._month)
        return _day < d._day;
    }
    return false;
  }
 
  bool Date::operator==(const Date& d)
  {
    return _year == d._year
      && _month == d._month
      && _day == d._day;
  }
 
  bool Date::operator<=(const Date& d)
  {
    return *this < d && *this == d;
  }
  bool Date::operator>(const Date& d)
  {
    return !(*this <= d);
  }
  bool Date::operator>=(const Date& d)
  {
    return !(*this <d);
  }
  bool Date::operator!=(const Date& d)
  {
    return !(*this == d);
  }
 
  void Date::print()
  {
    cout << _year << "-" << _month << "-" << _day << endl;
  }
  Date::~Date()
  {
    //cout << "Date::~Date()" << endl;
 
  }
 
  ostream& operator<<(ostream& out, const Date& d)
  {
    out << d._year << "年" << d._month << "月" << d._day << endl;
    return out;
  }
 
  istream& operator>>(istream& in, Date& d)
  {
    cout << "请输入日期:" << endl;
 
    in >> d._year >> d._month >> d._day;
    return in;
  }
 
 
//Test.cpp
#include"Date.h"
 
//Date func()
//{
//  Date d3(2024, 4, 14);
//  return d3;
//}
int fx()
{
  int a = 10;
  int b = 20;
  int c = 30;
  return a + b + c;
}
//int main()
//{
//  
//    Date ret = func();
//    ret.print();
//
//
//  /*Date d1(2024, 4, 14);
//  Date d2(2024, 5, 14);
//  d1.print();
//  d2.print();
//  cout << (d2 < d1) << endl;
//  cout << (d2 <= d1) << endl;
//  cout << (d2 > d1) << endl;
//  cout << (d2 >= d1) << endl;
//  cout << (d2 == d1) << endl;
//  cout << (d2 != d1) << endl;*/
//
//  return 0;
//}
 
//Date func()
//{
//  Date d3(2024, 4, 14);
//  return d3;
//}
//Date& func()
//{
//  Date d3(2024, 4, 14);
//  return d3;
//}
//int main()
//{
//  //const Date& ret = func();
//  //ret.print();
//
//  return 0;
//}
 
//Date& func()
//{
//  static Date d3(2024, 4, 14);
//  return d3;
//}
//int main()
//{
//   Date& ret = func();
//   ret.print();
//
//  return 0;
//}
//class Time
//{
//public:
//  Time()
//  {
//    _hour = 1;
//    _minute = 1;
//    _second = 1;
//  }
//  /*Time& operator=(const Time& t)
//  {
//    if (this != &t)
//    {
//      _hour = t._hour;
//      _minute = t._minute;
//      _second = t._second;
//    }
//    return *this;
//  }*/
//  
private:
//  int _hour;
//  int _minute;
//  int _second;
//};
//
//class Date1
//{
//public:
//  void print()
//  {
//    cout << _year << "-" << _month << "-" << _day << endl;
//  }
//private:
//  // 基本类型(内置类型)
//  int _year = 1970;
//  int _month = 1;
//  int _day = 1;
//  // 自定义类型
//  Time _t;
//};
//int main()
//{
//  Date1 d1;
//  Date1 d2;
//  d1 = d2;
//
//  d1.print();
//  d2.print();
//  return 0;
//}
//int main()
//{
//  Date d1(2024, 4, 15);
//  Date d2(2024, 2, 15);
//  d2 = d1;
//
//  d1.print();
//  d2.print();
//
//  
//  return 0;
//}
 
 
 
 
//class Date1
//{
//public:
//  Date1(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;
//  }
//  void Print() const
//  {
//    cout << "Print()const" << endl;
//    cout << "year:" << _year << endl;
//    cout << "month:" << _month << endl;
//    cout << "day:" << _day << endl << endl;
//  }
//private:
//  int _year; // 年
//  int _month; // 月
//  int _day; // 日
//};
//void Test()
//{
//  Date1 d1(2022, 1, 13);
//  d1.Print();
//  const Date1 d2(2022, 1, 13);
//  d2.Print();
//}
//  int main()
//{
//    Test();
//    return 0;
//}
int main()
{
  Date d1(2024, 4, 17);
  Date d2(2024, 9, 14);
 
  cin >> d1 >> d2;
  cout << d1 << d2;
  /*cout << (d2 - d1) << endl;
 
  d1.print();
  d2.print();*/
  return 0;
}

6、取地址及const取地址操作符重载


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


class Date1
{
public:
  Date1* operator&()
  {
    return this;
  }
  const Date1* operator&()const
  {
    return this;
  }
private:
  int _year;
  int _month;
  int _day;
};


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


撒花!!!

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