C++-类与对象(2)

类与对象(第二部分)

类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。空类并不是什么都没有,任何类在我们不写的情况下,都会自动生成6个默认成员函数

类1

构造函数

构造函数的概念

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有一个合适的初始值,并且在对象的声明周期内只调用一次。

构造函数的特性

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是它的主要任务并不是开空间创建对象,而是初始化对象。

特征如下:

​ 1、函数名与类名相同

​ 2、无返回值

​ 3、对象实例化时编译器自动调用对应的构造函数

​ 4、构造函数可以重载

class Date{
public:
    //无参构造函数
    Date(){
        
    }
    //带参构造函数
    Date(int year, int month, int day){
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};
void Test(){
    Date d;
    Date d1(1900,1,1);
    //注意:通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    //以下代码的函数,声明了d3函数,该函数无参,返回一个日期类型的对象
    Date d3();
}

5、如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显示定义,那么编译器将不在生成。

class Date
{
public:
 /*
 // 如果用户显式定义了构造函数,编译器将不再生成
 Date (int year, int month, int day)
 {
 _year = year;
 _month = month;
 _day = day;
 }
 */
private:
 int _year;
 int _month;
 int _day;
};
void Test()
{
 // 没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数
    Date d;
}

6、默认构造函数只能有一个,不管是无参的,还是全缺省的。

// 默认构造函数
class Date
{ 
public:
 Date()
 {
 _year = 1900 ;
 _month = 1 ;
 _day = 1;
 }
 
 Date (int year = 1900, int month = 1, int day = 1)
 {
 _year = year;
 _month = month;
 _day = day;
 }
private :
 int _year ;
 int _month ;
 int _day ;
};
// 以下测试函数能通过编译吗?
void Test()
{
 Date d1;
}

类2

在VS2013下可以编译通过,说明在VS内编译器对这种重载错误并不是特别在意。

但是在g++中,出现了编译器不知道执行哪个构造函数。

7、关于编译器生成的默认成员函数,我们可能会想到这个默认的构造函数有什么用?我们直接声明一个对象,返回的_year值都是随机数,那么还有什么意义呢?

C++把类型分成内置类型和自定义类型。内置类型就是语法已经定义好的类型,如int/char…;自定义类型就是我们通过class/struct/union等定义的类型

#include <iostream>                       
class Time    
{    
  public:    
    Time()    
    {    
      std::cout << "Time()" << std::endl;    
      // 比特科技    
      //   8. 成员变量的命名风格    
      _hour = 0;    
      _minute = 0;    
      _second = 0;    
    }    
  private:    
    int _hour;    
    int _minute;    
    int _second;    
};    
class Date    
{    
  private:    
    // 基本类型(内置类型)    
    int _year;    
    int _month;    
    int _day;    
    // 自定义类型    
    Time _t;    
};
int main()
{
  Date d;
  return 0;
}     

8、成员变量风格

通过以上代码能看出,在C++中,我们一般定义成员变量时,会在成员变量之前加上一个下划线

int _year;
int _month;
int _day;

析构函数

析构函数的概念

与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作由编译器来完成。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。

析构函数的特性

1、析构函数名就是构造函数名之前加上字符~

2、无参数无返回值

3、一个类只有一个析构函数。若未显示定义,系统会自动生成默认的析构函数

4、对象生命周期结束时,C++编译系统自动调用析构函数

#include <iostream>    
#include <assert.h>    
typedef int DateType;    
class SeqList    
{                     
public:    
  SeqList(int capacity = 10){    
    _pData = (DateType*)malloc(capacity * sizeof(DateType));    
    assert(_pData);    
    _size = 0;    
    _capacity = capacity;    
  }    
  ~SeqList()    
  {    
    if(_pData)    
    {    
      free(_pData);  //释放栈上资源  
      _pData = NULL;    
      _size = 0;    
      _capacity = 0;    
    }    
  }    
private:    
  int* _pData;    
  size_t _size;    
  size_t _capacity;    
}; 

5、编译器自动生成的默认析构函数,对会自定类型成员调用它的析构函数

class String
{
public:
 String(const char* str = "jack")
 {
 	_str = (char*)malloc(strlen(str) + 1);
 	strcpy(_str, str);
 }
 ~String()
 {
 	cout << "~String()" << endl;
 	free(_str);
 }
private:
 	char* _str;
};
class Person
{
private:
 	String _name;
 	int _age;
};
int main()
{
 	Person p;
 	return 0;
}

拷贝构造函数

拷贝构造函数概念

只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时,新对象由编译器自动调用

拷贝构造函数的特征

1、拷贝构造函数是构造函数的一个重载

2、拷贝构造函数的参数只有一个且必须使用引用传参,使用传值的方式会引发无穷递归调用

类3

3、若未显示定义,系统生成默认的构造拷贝函数。默认的拷贝构造函数对象按内存存储字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝

#include <iostream>    
class Date    
{    
  public:    
    Date(int year = 1900, int month = 1, int day = 1)    
    {    
      _year = year;    
      _month = month;    
      _day = day;    
    }    
    void Print(){    
      std::cout<<_year<<"-"<<_month<<"-"<<_day<<std::endl;    
    }    
  private:    
    int _year;    
    int _month;    
    int _day;    
};                    
int main()    
{             
  Date d1;    
  // 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。    
  Date d2(d1);                                                   
  d2.Print();                                                    
  return 0;                                                      
}

赋值运算符重载

运算符重载

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

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

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

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型或者枚举类型的操作数
  • 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
  • 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
  • 操作符有一个默认的形参this,限定为第一个形参
  • .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
// 全局的operator==
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;
};
// 这里会发现运算符重载成全局的就需要成员变量是共有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(const Date& d1, const Date& d2)
{
 return d1._year == d2._year;
 && d1._month == d2._month
 && d1._day == d2._day;
}
void Test ()
{
 Date d1(2018, 9, 26);
 Date d2(2018, 9, 27);
 cout<<(d1 == d2)<<endl;
}
class Date
{ 
public:
 Date(int year = 1900, int month = 1, int day = 1)
 {
 _year = year;
 _month = month;
 _day = day;
 }
 // bool operator==(Date* this, const Date& d2)
 // 这里需要注意的是,左操作数是this指向的调用函数的对象
 bool operator==(const Date& d2)
 {
 return _year == d2._year;
     && _month == d2._month
 	&& _day == d2._day;
 }
private:
 int _year;
 int _month;
 int _day;
};
void Test ()
{
 Date d1(2018, 9, 26);
 Date d2(2018, 9, 27);
 cout<<(d1 == d2)<<endl;
}
赋值运算符重载
class Date
{ 
public :
 Date(int year = 1900, int month = 1, int day = 1)
 {
 	_year = year;
 	_month = month;
 	_day = day;
 }
 Date (const Date& d)
 {
 	_year = d._year;
 	_month = d._month;
 	_day = d._day;
 }
 
 Date& operator=(const Date& d)
 {
 	if(this != &d)
 	{
 		_year = d._year;
 		_month = d._month;
 		_day = d._day;
 	}
 }
private:
 int _year ;
 int _month ;
 int _day ;
};

赋值运算符主要有四点:

1、参数类型

2、返回值

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

4、返回*this

5、一个类如果没有显示定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝

const成员

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

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

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

class Date
{
public :
 Date* operator&()
 {
 	return this ;
 }
 const Date* operator&()const
 {
 	return this ;
 }
private :
 int _year ; // 年
 int _month ; // 月
 int _day ; // 日
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章