c++初阶------类和对象(六大默认构造函数的揭破)-2

简介: c++初阶------类和对象(六大默认构造函数的揭破)

c++初阶------类和对象(六大默认构造函数的揭破)-1

https://developer.aliyun.com/article/1499142


  1. 深拷贝
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Stack
{
public:
  Stack(int capacity = 3)
  {
    _a = (int*)malloc(sizeof(int) * capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = capacity;
    _top = 0;

  }
  //深拷贝
  Stack(const Stack& st)
  {
    _a = (int*)malloc(sizeof(int) * st._capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = st._capacity;
    _top = st._top;

  }
private:
  int *_a;
  int _top;
  int _capacity;
};
int main()
{
  Stack d1(5);
  Stack d2(d1);
  return 0;
}

创建两个一模一样的空间,深拷贝就是要进行一比一的复制,浅拷贝在这里会公用一块地址,

浅拷贝的示意图:

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请

时,则拷贝构造函数是一定要写的,否则就是浅拷贝。

简单的来说,如果涉及到地址就要考虑一下是否需要深拷贝,如是简单的拷贝值的化,拷贝函数可以写也可以不写,不写的话,编译器会默认构造一个拷贝函数,(浅拷贝)


#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Stack
{
public:
  //构造函数
  Stack(int capacity = 3)
  {
    _a = (int*)malloc(sizeof(int) * capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = capacity;
    _top = 0;

  }
  //深拷贝
  Stack(const Stack& st)
  {
    _a = (int*)malloc(sizeof(int) * st._capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = st._capacity;
    _top = st._top;

  }
  //析构函数
  ~Stack()
  {
    free(_a);
    cout << "释放了空间" << endl;
    _a = nullptr;
    _top = 0;
    _capacity = 0;
  }
private:
  int *_a;
  int _top;
  int _capacity;
};
class MyQueue
{
private:
  Stack _d1;
  Stack _d2;
  int _size = 0;

};
int main()
{
  MyQueue q1;
  MyQueue q2(q1);//调用了拷贝构造
  return 0;
}

这里的情况就是q1会开辟成员变量的空间,调用了MyQueue的默认构造函数,然后_d1和_d2调用Stack的自定义的默认构造.

q1和q2之间的联系,之间调用的是拷贝函数,调用MyQueue的默认拷贝构造函数,而Stack调用的就是自定义的拷贝构造函数

一些场合

自定义当返回值

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Stack
{
public:
  //构造函数
  Stack(int capacity = 3)
  {
    _a = (int*)malloc(sizeof(int) * capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = capacity;
    _top = 0;

  }
  //深拷贝
  Stack(const Stack& st)
  {
    cout << "拷贝" << endl;
    _a = (int*)malloc(sizeof(int) * st._capacity);
    if (_a == NULL)
    {
      perror("malloc");
      exit(-1);
    }
    _capacity = st._capacity;
    _top = st._top;

  }
  //析构函数
  ~Stack()
  {
    free(_a);
    cout << "释放了空间" << endl;
    _a = nullptr;
    _top = 0;
    _capacity = 0;
  }
private:
  int *_a;
  int _top;
  int _capacity;
};
class MyQueue
{
private:
  Stack _d1;
  Stack _d2;
  int _size = 0;

};
Stack fun()
{
  Stack d1;
  return  d1;
}
int main()
{
  fun();
  return 0;
}

可以看出返回值,不会直接返回d1,而是d1的拷贝,也就是d1的临时对象

如果换成

Stack& fun()
{
  static Stack d1;
  return  d1;
}

可以把图中的Test函数换成我们的fun函数,看懂了就可以了。

为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。

赋值运算符重载

运算符重载

前面我们学习C语言,知道内置类型(int 、char…)可以直接使用各种运算符,但是自定义类型是不可以的

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
  //构造函数
  Date(int year, int month, int day)
  {
    cout << "构造函数" << endl;
    _year = year;
    _month = month;
    _day = day;
  }
  //浅拷贝
  Date(const Date& d1)
  {
    cout << "拷贝" << endl;
    _year = d1._year;
    _month = d1._month;
    _day = d1._day;
  }
  //析构函数
  ~Date()
  {
    cout << "析构函数" << endl;
  }
  int _year;
  int _month;
  int _day;
};
//相等
bool Equal(Date x, Date y)
{
  return x._year == y._year
    && x._month == y._month
    && x._day == y._day;
}
//大于
bool Greater(Date x, Date y)
{
  if (x._year > y._year)
  {
    return true;
  }
  else if (x._year == y._year && x._month > y._month)
  {
    return true;
  }
  else if (x._year == y._year && x._month == y._month && x._day > y._day)
  {
    return true;
  }
  else
    return false;
}
int main()
{
  Date a(2024,1,3);
  Date b(2024, 1, 5);
  cout << Equal(a, b) << endl;
  return 0;
}

我们如果要比较这些自定义类型就有可能要写成函数,但是不太方便,所以c++改变了一些

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

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

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

//相等
bool operator==(Date x, Date y)
{
  return x._year == y._year
    && x._month == y._month
    && x._day == y._day;
}
//大于
bool operator>(Date x, Date y)
{
  if (x._year > y._year)
  {
    return true;
  }
  else if (x._year == y._year && x._month > y._month)
  {
    return true;
  }
  else if (x._year == y._year && x._month == y._month && x._day > y._day)
  {
    return true;
  }
  else
    return false;
}

函数名就更改过成这样的,使用这个函数方法有两种,一种是直接写函数名直接调用

另一种

这里使用a ==b 就是 operator==(x, y)的简写,需要注意的是 << 的优先级比 == 、>…高。

函数重载和运算符重载两者毫不相关,函数重载允许参数不同的同名函数,而运算符重载允许自定义类型直接使用运算符

我们还可以在类里面写

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
  //构造函数
  Date(int year, int month, int day)
  {
    cout << "构造函数" << endl;
    _year = year;
    _month = month;
    _day = day;
  }
  //浅拷贝
  Date(const Date& d1)
  {
    cout << "拷贝" << endl;
    _year = d1._year;
    _month = d1._month;
    _day = d1._day;
  }
  //析构函数
  ~Date()
  {
    cout << "析构函数" << endl;
    cout << _year << '-' << _month << '-' << _day << endl;
  }

  //相等
  bool operator==(Date& y)
  {
    return _year == y._year
      && _month == y._month
      &&_day == y._day;
  }
private:
  int _year;
  int _month;
  int _day;
  //大于
  bool operator>(Date& y)
  {
    if (_year > y._year)
    {
      return true;
    }
    else if (_year == y._year && _month > y._month)
    {
      return true;
    }
    else if (_year == y._year && _month == y._month && _day > y._day)
    {
      return true;
    }
    else
      return false;
  }
  void exchengtime(int* a)
  {
    if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
      a[2] = 29;
    else
      a[2] = 28;
  }
  Date& operator+=(int day)//这里返回引用是防止对象拷贝返回
  {
    int a[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    _day += day;
    exchengtime(a);
    while (_day > a[_month])
    {
      _day -= a[_month];
      _month++;
      if (_month > 12)
      {
        _year++;
        exchengtime(a);
        _month %= 12;
      }

    }
    return *this;
  }
};
int main()
{
  Date a(2024,1,3);
  Date b(2024, 1, 5);
  cout << (a == b) << endl; // a.operator==(b)  -> a.operator==(&a, b)
  a += 363;
  return 0;
}

赋值运算符重载

有时候就是我们会看见到i = 5; j = 20;

我们有时需要对对象进行赋值

#include<iostream>
//using namespace std;
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
  //构造函数
  Date(int y, int m, int d)
  {
    _year = y;
    _month = m;
    _day = d;

  }
  //拷贝
  Date(const Date& d1)
  {
    _year = d1._year;
    _month = d1._month;
    _day = d1._day;
  }
  //析构函数
  ~Date()
  {
    cout << _year << endl;
    cout << _month << endl;
    cout << _day << endl;
  }
  Date& operator=(const Date& d1)
  {
    //防止自己给自己赋值(特别是深拷贝的时候)
    if (this != &d1)
    {
      _year = d1._year;
      _month = d1._month;
      _day = d1._day;
    }
    return *this;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  int a = 10;
  cout << a << endl;
  Date d1(2024, 1, 15);
  Date d2(2024,1,16);
  Date d3(2024,1,17);
  d3 = d1 = d2;


  return 0;
}

这样就可以不使用到拷贝构造,

还需要注意的是operator=是默认成员函数,不写编译器会默认生成,跟拷贝构造的行为类似,对内置类型进行值拷贝,自定义类型调用他的赋值

赋值运算符重载格式:

1. 参数类型:const T&,传递引用可以提高传参效率

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

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

4. 返回*this :要复合连续赋值的含义

全部代码总和:

test.h

#pragma once
#include<iostream>
#include<assert.h>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
  //构造函数
  Date(int y = 2024, int m = 1, int d = 15);
  //拷贝
  Date(const Date& d1);
  //析构函数
  ~Date();

  int Getmonthday(int year, int month);

  bool operator<(const Date& d1);
  bool operator>(const Date& d1);
  bool operator==(const Date& d1);
  bool operator<=(const Date& d1);
  bool operator>=(const Date& d1);

  Date& operator=(const Date& d1);
  Date& operator+=(int day);
  Date operator+(int day);
  
private:
  int _year;
  int _month;
  int _day;
};

test.cpp

#include"Date.h"
Date::Date(int y, int m, int d)
{
  _year = y;
  _month = m;
  _day = d;
  if (_year < 0 || _month < 1 || _month >12 || _day > Getmonthday(_year, _month) || _day < 0)
  {
    cout << _year << "-" << _month << "-" << _day << endl;
    cout << "非法日期" << endl;
    exit(-1);
  }

}
int Date::Getmonthday(int year, int month)
{
  int monthday[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  {
    monthday[2] = 29;
  }
  return monthday[month];

}
//拷贝
Date::Date(const Date& d1)
{
  _year = d1._year;
  _month = d1._month;
  _day = d1._day;
}
//析构函数
Date::~Date()
{
  cout << _year << endl;
  cout << _month << endl;
  cout << _day << endl;
}
Date& Date::operator=(const Date& d1)
{
  //防止自己给自己赋值(特别是深拷贝的时候)
  if (this != &d1)
  {
    _year = d1._year;
    _month = d1._month;
    _day = d1._day;
  }
  return *this;
}
Date& Date::operator+=(int day)
{
  *this = *this + day;
  return *this;
}
Date Date::operator+(int day)
{
  Date tmp(*this);
  int monthday[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  tmp._day += day;
  if ((tmp._year % 4 == 0 && tmp._year % 100 != 0) || (tmp._year % 400 == 0))
  {
    monthday[2] = 29;
  }
  else
  {
    monthday[2] = 28;
  }
  while (tmp._day - monthday[tmp._month] > 0)
  {
    
    tmp._day -= monthday[tmp._month];
    tmp._month++;
    if (tmp._month > 12)
    {
      tmp._month %= 12;
      tmp._year++;
      if ((tmp._year % 4 == 0 && tmp._year % 100 != 0) || (tmp._year % 400 == 0))
      {
        monthday[2] = 29;
      }
      else
      {
        monthday[2] = 28;
      }
    }
  }
  return tmp;


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

}
bool Date::operator==(const Date& d1)
{
  return _year == d1._year && _month == d1._month && _day == d1._day;
}
bool Date::operator<=(const Date& d1)
{
  return (*this) < d1 || (*this) == d1;
}
bool Date::operator>(const Date& d1)
{
  return !((*this) <= d1);
}
bool Date::operator>=(const Date& d1)
{
  return !((*this)< d1);
}

test1.cpp

#include"Date.h"
int main()
{
  Date d1(2024, 1, 3);
  Date d2(2024,1,16);
  Date d3(2020,1,17);
  int a = d1 == d2;
  cout << a << endl;
  a = d1 >= d2;
  cout << a << endl;
  a = d1 <= d2;
  cout << a << endl;
  d3 += 366;
  return 0;
}

需要注意的是代码中的**operator+(int day)**函数 和 **operator+=(int day)**函数的效率是很低效的

这张图里面可以说明,上图的代码的对象拷贝是次数多,造成了效率的低效,

对象的赋值进行了一次拷贝对象

*this + day 创建了一个临时的 Date 对象,表示当前对象的值加上 day 天的结果。*this = … 将上面提到的临时对象的值赋给了当前对象 *this。这个赋值操作涉及到对象的拷贝。赋值操作符 = 的行为是根据类的定义而定的,,它会执行对象的成员逐个拷贝。


实现 - 和-=

这里的实现方法和上面的方法是不一样的,

Date& Date::operator-=(int day)
{
  _day -= day;
  int monthday[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
  {
    monthday[2] = 29;
  }
  else
  {
    monthday[2] = 28;
  }
  while (_day <= 0)
  {
    _month--;
    
    if (_month <= 0)
    {
      _year--;
      if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
      {
        monthday[2] = 29;
      }
      else
      {
        monthday[2] = 28;
      }
      _month = 12;
    }
    _day += monthday[_month];

  }
  return *this;


}
Date Date::operator-(int day)
{
  Date tmp(*this);
  tmp -= day;
  return tmp;


}


可以看看这里,这里的情况就是,在operator-函数里面套用operator-=,这样的效率就变高了

实现++

前置++

Date& Date::operator+=(int day)
{
  _day += day;
  int monthday[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
  {
    monthday[2] = 29;
  }
  else
  {
    monthday[2] = 28;
  }
  while (_day - monthday[_month] > 0)
  {
    _day -= monthday[_month];
    _month++;
    if (_month > 12)
    {
      _month %= 12;
      _year++;
      if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))
      {
        monthday[2] = 29;
      }
      else
      {
        monthday[2] = 28;
      }
    }

  }
  return *this;
}
Date& Date::operator++()
{
  *this += 1;
  return *this;
}

后置++

后置++不能和上面写的一样,因为要构成重载,我们只能从参数那里下手,不然就会无法构成重载,

Date Date::operator++(int)
{
  Date tmp(*this);
  *this += 1;
  return tmp;

}

编译器链接的时候,函数的名字只和函数的名字和函数的参数类型有关,跟函数的形参名字没有关系。

就是说c++的函数在链接的时候有自己的函数名修饰规则,


c++初阶------类和对象(六大默认构造函数的揭破)-3

https://developer.aliyun.com/article/1499148

目录
打赏
0
4
4
1
12
分享
相关文章
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
【C++篇】深度解析类与对象(中)
在上一篇博客中,我们学习了C++类与对象的基础内容。这一次,我们将深入探讨C++类的关键特性,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载、以及取地址运算符的重载。这些内容是理解面向对象编程的关键,也帮助我们更好地掌握C++内存管理的细节和编码的高级技巧。
【C++篇】深度解析类与对象(上)
在C++中,类和对象是面向对象编程的基础组成部分。通过类,程序员可以对现实世界的实体进行模拟和抽象。类的基本概念包括成员变量、成员函数、访问控制等。本篇博客将介绍C++类与对象的基础知识,为后续学习打下良好的基础。
|
1月前
|
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
72 19
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
59 13
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
57 5
【C++面向对象——群体类和群体数据的组织】实现含排序功能的数组类(头歌实践教学平台习题)【合集】
1. **相关排序和查找算法的原理**:介绍直接插入排序、直接选择排序、冒泡排序和顺序查找的基本原理及其实现代码。 2. **C++ 类与成员函数的定义**:讲解如何定义`Array`类,包括类的声明和实现,以及成员函数的定义与调用。 3. **数组作为类的成员变量的处理**:探讨内存管理和正确访问数组元素的方法,确保在类中正确使用动态分配的数组。 4. **函数参数传递与返回值处理**:解释排序和查找函数的参数传递方式及返回值处理,确保函数功能正确实现。 通过掌握这些知识,可以顺利地将排序和查找算法封装到`Array`类中,并进行测试验证。编程要求是在右侧编辑器补充代码以实现三种排序算法
47 5
【C++面向对象——类的多态性与虚函数】计算图像面积(头歌实践教学平台习题)【合集】
本任务要求设计一个矩形类、圆形类和图形基类,计算并输出相应图形面积。相关知识点包括纯虚函数和抽象类的使用。 **目录:** - 任务描述 - 相关知识 - 纯虚函数 - 特点 - 使用场景 - 作用 - 注意事项 - 相关概念对比 - 抽象类的使用 - 定义与概念 - 使用场景 - 编程要求 - 测试说明 - 通关代码 - 测试结果 **任务概述:** 1. **图形基类(Shape)**:包含纯虚函数 `void PrintArea()`。 2. **矩形类(Rectangle)**:继承 Shape 类,重写 `Print
51 4
【C++面向对象——类的多态性与虚函数】编写教学游戏:认识动物(头歌实践教学平台习题)【合集】
本项目旨在通过C++编程实现一个教学游戏,帮助小朋友认识动物。程序设计了一个动物园场景,包含Dog、Bird和Frog三种动物。每个动物都有move和shout行为,用于展示其特征。游戏随机挑选10个动物,前5个供学习,后5个用于测试。使用虚函数和多态实现不同动物的行为,确保代码灵活扩展。此外,通过typeid获取对象类型,并利用strstr辅助判断类型。相关头文件如&lt;string&gt;、&lt;cstdlib&gt;等确保程序正常运行。最终,根据小朋友的回答计算得分,提供互动学习体验。 - **任务描述**:编写教学游戏,随机挑选10个动物进行展示与测试。 - **类设计**:基类
41 3
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等