类的入门<C++入门>(跑路人笔记)(3)

简介: 类的入门<C++入门>(跑路人笔记)

拷贝构造函数

概念

用于将一个相同类型的对象内容拷贝到另一个对象中,只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象 创建新对象时由编译器自动调用。

举例如下


class date
{
public:
    //构造函数
  date(int year = 2002, int month = 8, int day = 26)
  {
    _year = year;
    _month = month;
    _day = day;
  }
    //拷贝构造函数
  date(const date& c)//也是构造函数的重载
  {
    _year = c._year;
    _month = c._month;
    _day = c._day;
  }
    //打印函数
  void Print()
  {
    cout << _year << "_" << _month << "_" << _day << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};



这样我们对另一个对象进行初始化时就可以直接传来对象了.

image.png



在初始化时使用=赋值也可以调用到拷贝构造函数


image.png


其实我们不写拷贝构造函数我们的编译器也会给我们生成一个,自己生成的拷贝构造函数在浅拷贝的时候完全够用了,当我们需要深拷贝的时候就可以自己写.


深拷贝

既然我们不写拷贝构造函数编译器会自动生成一个用于浅拷贝的,为啥还要有呢?


主要是因为我们要写深拷贝.


深拷贝就是我们在实现的时候注意一下,不能简单通过赋值操作来拷贝的需要深拷贝一下.


比如指针等.


我们比如我们使用Stack类的时候需要从堆区拿空间,就需要指针来保存变量,如果我们使用浅拷贝就会造成free两次的警告,而且在使用的时候也十分诡异.


所以我们要专门写个拷贝构造函数来达到深拷贝的目的.


比如我们的Stack类的.


Stack(const Stack& s)
  {
  _data = (int*)malloc(sizeof(int) * (s._capacity));
  memcpy(s._data, _data, sizeof(int) * s._capacity);
  _capacity = s._capacity;
  _top = s._top;
  }


我们的这些默认生成的函数都可以自己实现,只要自己记住他们的格式就好.


其中取地址重载等函数都可以实现但是,没必要编译器的实现以及足够我们使用了.


操作符重载

我们用类的时候总要使用-,+,=,*,/,++,--,==,!=等操作符,还是以日期类为例.


当然日期类就一般不会使用*,/了=.=


早操作符重载的格式如下


//返回值类型+operator+要重载的符号+(形参)
     Date& operator=(Date& d1 ,const Date& d2);
//上方就是我对=号赋值符的一次实现.


而具体的我们要看实现.


比如我们可以选择在类里实现或是在类外实现.


先来一个日期类吧


class date
{
public:
  void Init(int year = 1, int  month = 1, int day = 1)
  {
  _year = year;
  _month = month;
  _day = day;
  }
  void Print()
  {
  cout << _year << "-" << _month << "-" << _day << endl;
  }
private:
  int _day;
  int _month;
  int _year;
};




我们先用类外的方式实现一个==看看吧


bool operator==(const Date& d1, const Date& d2)
{
  if (d1._year == d2._year && d1._month == d2._month && d1._day == d2._day)
  {
  return true;
  }
  else
  {
  return false;
  }
}



这个其实并不能使用因为我们_year是私有形势存储的,要想使用就必须将_day,_month,_year公有化或者使用内部共有函数来得到他们的值,因为我们只是演示,所以我暂时先把他公有化出来.


我们实现好了这个==运算符后


image.png


上图框起的两种方式都可以使用到我们的==重载功能,但是我们偏向使用下面的a==b.


在我们使用a == b的时候其实编译器会帮我们换成第一种的形式—operator==(a,b).


不过我们还是把这些运算符放在类里较好,及保证了类的封装性,又保证了我们元素不被外界访问.


我们将操作符重载放在类里需要对格式进行稍微改变.


// ==运算符重载
  bool operator==(const Date& d)
  {
  if (_year == d._year && _month == d._month && _day == d._day)
  {
    return true;
  }
  else
  {
    return false;
  }
  }


在类内定义的格式其实和外面定义的几乎没啥区别,除了少了一个变量,在使用_year的时候我们不用把它变成共有化的了.


其实少的那个变量使用了this指针来代替.而_year其实也是通过this指针得到的.


但是我们的this是两个变量的那个呢?


来看看我们如何使用类里定义的重构函数就知道了.


image.png


我们在使用的时候的形势其实注定了,上面的①中的this其实就是a的地址.


而下面的②其实this也是a的地址.


两者在编译看来没有区别.编译器也会2变成1后,然后编译.


如果有两个变量一般是左边的左this指针,三个变量就是最左的是this指针


类比思考一下,我们的前置后置++ --等其实在类内实现的时候就没必要专门设置形参只需要一个this指针就够了.但是这样就没办法分别了,所以我们规定后置类型要创建一个int类型的形参用于函数分辨.以便形成重构.


下面是对日期类所有操作符重载的实现,我们在实现的时候,有些部分可以操作符可以复用最好复用,复用带来的好处有很多,不仅方便,而且后期找bug也可以减少低级错误.

class Date
{
public:
  void Print()
  {
    cout << _year << "_" << _month << "_" << _day << endl;
  }
  // 获取某年某月的天数
  int GetMonthDay(int year, int month)
  {
    if (month > 12)
    {
      cout << "月输入错误" << endl;
      return -1;
    }
    int const arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//保存每个月的日期
    if (month == 2 && JudgeLeapYear(year))//闰年
    {
      return 29;
    }
    return arr[month];
  }
  // 全缺省的构造函数
  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  // 拷贝构造函数
  // d2(d1)
  Date(const Date& d)
  {
    _year = d._year;
    _month = d._month;
    _day = d._day;
  }
  // 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
  Date& operator=(const Date& d)
  {
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
  }
  // 析构函数
  ~Date()
  {
    ;//不写用默认的也可
  }
  // 日期+=天数
  Date& operator+=(int day)
  {
    *this = *this + day;//复用+
    return *this;
  }
  // 日期+天数
  Date operator+(int day)
  {
    Date tmp(*this);
    (tmp._day) += day;
    while (tmp._day >= GetMonthDay(tmp._year, tmp._month))//判断我们的day是不能超过当月的最大数值的.
    {
      tmp._day -= GetMonthDay(tmp._year, tmp._month);
      tmp._month += 1;
      if (tmp._month == 13)
      {
        tmp._month -= 12;
        tmp._year += 1;
      }
    }
    return tmp;
  }
  // 日期-天数
  Date operator-(int day)
  {
    Date tmp(*this);
    (tmp._day) -= day;
    while (tmp._day <= 0)
    {
      tmp._day += GetMonthDay(tmp._year, tmp._month);
      tmp._month -= 1;
      if (tmp._month == 0)
      {
        tmp._month += 12;
        tmp._year -= 1;
      }
    }
    return tmp;
  }
  // 日期-=天数
  Date& operator-=(int day)
  {
    _day -= day;
    while (_day <= 0)
    {
      _day += GetMonthDay(_year, _month);
      _month -= 1;
      if (_month == 0)
      {
        _month = 12;
        _year -= 1;
      }
    }
    return *this;
  }
  // 前置++
  Date& operator++()
  {
    *this += 1;
    return *this;
  }
  // 后置++
  Date operator++(int)//传的int是语法规定
  {
    Date tmp(*this);
    *this += 1;
    return tmp;
  }
  // 后置--
  Date operator--(int)
  {
    Date tmp(*this);
    *this -= 1;
    return tmp;
  }
  // 前置--
  Date& operator--()
  {
    *this -= 1;
    return *this;
  }
  // >运算符重载
  bool operator>(const Date& d)
  {
    if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  // ==运算符重载
  bool operator==(const Date& d)
  {
    if (_year == d._year && _month == d._month && _day == d._day)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  //>=运算符重载
  inline bool operator >= (const Date& d)
  {
    if (*this > d || *this == d)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  // <运算符重载
  bool operator < (const Date& d)
  {
    return !(*this >= d);
  }
  // <=运算符重载
  bool operator <= (const Date& d)
  {
    if (*this < d || *this == d)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  // !=运算符重载
  bool operator != (const Date& d)
  {
    return !(*this == d);
  }
  // 日期-日期 返回天数
  int operator-(const Date& d)
  {
    Date min = (*this > d ? d : *this);
    Date max = (*this > d ? *this : d);
    int ret = 0;
    while (min != max)
    {
      min++;
      ret++;
    }
    return ret;
  }
private:
  bool JudgeLeapYear(int year)
  {
    if ((year % 4 == 0 && year % 100 != 0) || year % 100 == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
private:
  int _year;
  int _month;
  int _day;
};

const成员

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


image.png


其实很简单,就是在类内函数()后加上const就可以把原来类型为Date*的this指针变成const Date*类型.


相关文章
|
6天前
|
程序员 C++
C++模板元编程入门
【7月更文挑战第9天】C++模板元编程是一项强大而复杂的技术,它允许程序员在编译时进行复杂的计算和操作,从而提高了程序的性能和灵活性。然而,模板元编程的复杂性和抽象性也使其难以掌握和应用。通过本文的介绍,希望能够帮助你初步了解C++模板元编程的基本概念和技术要点,为进一步深入学习和应用打下坚实的基础。在实际开发中,合理运用模板元编程技术,可以极大地提升程序的性能和可维护性。
|
3天前
|
设计模式 安全 编译器
【C++11】特殊类设计
【C++11】特殊类设计
22 10
|
9天前
|
C++
C++友元函数和友元类的使用
C++中的友元(friend)是一种机制,允许类或函数访问其他类的私有成员,以实现数据共享或特殊功能。友元分为两类:类友元和函数友元。类友元允许一个类访问另一个类的私有数据,而函数友元是非成员函数,可以直接访问类的私有成员。虽然提供了便利,但友元破坏了封装性,应谨慎使用。
40 9
|
4天前
|
存储 安全 编译器
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
|
4天前
|
存储 编译器 C语言
【C++基础 】类和对象(上)
【C++基础 】类和对象(上)
|
4天前
|
存储 自然语言处理 编译器
【C++入门 三】学习C++缺省参数 | 函数重载 | 引用
【C++入门 三】学习C++缺省参数 | 函数重载 | 引用
|
4天前
|
小程序 C++
【C++入门 二 】学习使用C++命名空间及其展开
【C++入门 二 】学习使用C++命名空间及其展开
|
4天前
|
人工智能 分布式计算 Java
【C++入门 一 】学习C++背景、开启C++奇妙之旅
【C++入门 一 】学习C++背景、开启C++奇妙之旅
|
12天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。
|
12天前
|
编译器 C++
【C++】string类的使用④(字符串操作String operations )
这篇博客探讨了C++ STL中`std::string`的几个关键操作,如`c_str()`和`data()`,它们分别返回指向字符串的const char*指针,前者保证以&#39;\0&#39;结尾,后者不保证。`get_allocator()`返回内存分配器,通常不直接使用。`copy()`函数用于将字符串部分复制到字符数组,不添加&#39;\0&#39;。`find()`和`rfind()`用于向前和向后搜索子串或字符。`npos`是string类中的一个常量,表示找不到匹配项时的返回值。博客通过实例展示了这些函数的用法。