类和对象(中)

1.类的6个默认成员函数

任何一个类在我们什么都不写的情况下,都会自动生成下面6个默认成员函数。

class Date {};

在这里插入图片描述

2.构造函数

2.1概念

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

2.2特性

构造函数并不是开空间创建对象,而是初始化对象。
其特征如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。
class Date
{
public:
    //1.无参数构造函数
    Date()
    {}

    //2.带参数构造函数
    Date(int year,int month,int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }
private:
    int _year;
    int _month;
    int _day;
};

void TestDate()
{
    Date d1; //调用无参数构造函数
    Date d2(2015,1,1); //调用带参数构造函数
    
    //注意:如果通过无参数构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    //以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
    Date d3();
}
  1. 如果类中没有显示定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显示定义,编译器将不再生成。
  2. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。(不用传参就能执行的构造函数为默认构造函数)
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;
}
  1. C++把类型分为内置类型(基本类型)和自定义类型。看看下面的程序,就会发现编译器生成默认的构造函数就会对自定义类型成员_t调用它的默认成员函数。
#include<iostream>
using namespace std;

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	Date()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	//基本类型
	int _year;
	int _month;
	int _day;

	//自定义类型
	Time _t;
};

int main()
{
	Date d;
	return 0;
}

在这里插入图片描述

总结:
构造函数的细节很多,但是在实际中我们用构造函数是这样的,大多数情况都要自己写构造函数并且建议一般情况都是写一个全缺省的构造函数,这种方式能适应各种场景。

3.析构函数

3.1概念

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

3.2特性

析构函数是特殊的成员函数。
特征如下:

  1. 析构函数名是在类名前加上字符~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数(因为析构函数不构成重载)。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统会自动调用析构函数。
#include<iostream>
using namespace std;

class Stack
{
public:
	Stack(int capacity = 4)
	{
		_capacity = capacity;
		_size = 0;
		_a = (int*)malloc(sizeof(int)*_capacity);
	}

	~Stack()
	{
		free(_a); //释放堆上的空间
		_a = nullptr;
		_size = _capacity = 0;
	}

private:
	int* _a;
	int _capacity;
	int _size;
};

【问题】:下面的变量st1和st2谁先析构?

#include<iostream>
using namespace std;

class Stack
{
public:
	Stack(int capacity = 4)
	{
		_capacity = capacity;
		_size = 0;
		_a = (int*)malloc(sizeof(int)*_capacity);
	}

	void Push()
	{}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_size = _capacity = 0;
	}

private:
	int* _a;
	int _capacity;
	int _size;
};

int main()
{
	Stack st1;
	st1.Push();

	Stack st2;
	st2.Push();

	return 0;
}

在这里插入图片描述

析构函数跟构造函数一样,会调用自定义类型成员的析构函数。

4.拷贝构造函数

4.1概念

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

拷贝构造就是拿一个已经存在的对象去构造初始化另一个要创建的对象

Date d1;
Date d2 = d1; //此为拷贝构造

4.2特征

拷贝构造函数也是特殊的成员函数,其特征如下:

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
#include<iostream>
using namespace std;

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;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
	Date d2(d1);

	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  1. 若未显式定义,系统生成默认的拷贝构造函数。默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
#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;
};

int main()
{
    Date d1;
	//这里d2调用的默认拷贝构造函数完成拷贝,d2和d1的值也是一样的
    Date d2(d1);

	return 0;
}

拷贝构造会对内置类型完成浅拷贝,或者值拷贝。
由浅拷贝会引出浅拷贝问题:
有一个栈类:

class Stack
{
public:
	Stack(int capacity = 4)
	{
		_capacity = capacity;
		_size = 0;
		_a = (int*)malloc(sizeof(int)*_capacity);
	}

	void Push()
	{}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_size = _capacity = 0;
	}

private:
	int* _a;
	int _capacity;
	int _size;
};

当我们使用拷贝构造的时候会引出一些问题:

Stack st;
Stack copy(st);

在这里插入图片描述

像Stack这样的类,编译器默认生成的拷贝构造完成的是浅拷贝,不满足我们的需求,需要自己实现深拷贝。

5.赋值运算符重载

5.1运算符重载

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

函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型operator操作符(参数列表)
注意:
● 不能通过连接其他符号来创建新的操作符:比如operator@
● 重载操作符必须有一个类类型或者枚举类型的操作数
● 用于内置类型的操作符,其含义不能改变,例如:内置的类型+,不能改变其含义
● 作为类成员的操作函数时,其参数看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
● .* 、 ::、sizeof、?: 、. 注意以上5个运算符不能重载。这个经常出现在笔试选择题中。

//全局的operator==
class Date
{
public:
    friend bool operator==(const Date& d1, const Date& d2);

    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;
}

int main()
{
    Date d1(2018, 9, 1);
    Date d2(2018, 9, 2);

    cout << (d1 == d2) << endl;


	system("pause");
	return 0;
}
//局部的operator==
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;
};

int main()
{
    Date d1(2018, 9, 1);
    Date d2(2018, 9, 2);

    cout << (d1 == d2) << endl;


	system("pause");
	return 0;
}

5.2赋值运算符重载

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._day;
        _month = d._month;
        _day = d._day;
    }

    //这里返回类型为Date&是要保证,类似d1 = d2 = d3这种赋值可行
    //返回类型使用&,是为了在返回的时候不用调拷贝构造
    //返回引用有一点要注意:出了函数作用域返回的对象还要存在
    Date& operator=(const Date& d)
    {
        //排除自己给自己赋值
        if (this != &d)
        {
            _year = d._day;
            _month = d._month;
            _day = d._day;
        }
        
        return *this;
    }
private:
    int _year;
    int _month;
    int _day;
};

//d1 = d2; d1.operator=(&d1,d2)

赋值运算符主要有四点:

  1. 参数类型
  2. 返回值
  3. 检测是否自己给自己赋值
  4. 返回*this
  5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
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;
};

int main()
{
    Date d1;
    Date d2(2018,9,1);
    
    //这里d1调用的编译器生成operator=完成拷贝,d2和d1的值也是一样的
    d1 = d2;
    return 0;
}

总结:

  1. 针对内置类型,会完成浅拷贝,也就是说像Date这样的类不需要我们自己写赋值运算符重载,Stack就得自己写。
  2. 针对自定义类型,也一样,它会调用它的赋值运算符重载,完成拷贝。

1-5.总结

在这里插入图片描述

6.const成员

6.1const修饰类的成员函数

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

为了防止this指针被修改,C++的类中可以在函数的后面加上const,表示this指向的对象不可修改。
好处:函数中不小心改变的成员变量,编译时会被检查出来。
建议:成员函数中,不需要改变成员变量,都加上const。

#include<iostream>
using namespace std;

class Date
{
public:

	Date(int year=1,int month=1,int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}

	void Display()
	{
		cout << "Display()" << endl;
		cout << _year << endl;
	}

	void Display() const
	{
		cout << "Display() const " << endl;
		cout << _year << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Display();

	const Date d2;
	d2.Display();


	system("pause");
	return 0;
}

在这里插入图片描述

问题:

  1. const对象可以调用非const成员函数吗?
  2. 非const对象可以调用const成员函数吗?
  3. const成员函数内可以调用其它的非const成员函数吗?
  4. 非const成员函数内可以调用其它的const成员函数吗?

答:非const对象可以调用const成员函数;
const对象不能调用非const成员函数(权限不能放大)

7.取地址及const取地址操作符重载(了解即可)

对类取地址时,其实&符号是被重载过的:

class Date
{
public :
    Date* operator&()
    {
        return this ;
    }
 
    const Date* operator&()const
    {
        return this ;
    }
 
private :
    int _year ; 
    int _month ; 
    int _day ; 
};
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值