c++类与对象2

隐含的this指针

性质:每个成员函数都有一个指针形参,它的名字是固定的,称为this指针,this指针是隐式的(为何构造函数除外?)。成员函数隐含指针形参,是编译器自己处理的,不能在成员函数的形参中添加this指针的参数定义,也不能在调用时显示传递对象的地址给this指针。

传递方式:对象地址作为实参传递给成员函数的第一个形参this指针

存放位置:this指针在对象调用成员函数前创建,在成员函数调用结束后销毁,所以this指针存放在上。

类的默认成员函数

1、构造函数
有参和无参的构造函数

class Date
{

private:
	int _year;
	int _month;
	int _day;

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

public:
	void Display()
	{
		cout << _year << _month << _day << endl;
	}
};

void Test()
{
Date d1;//调用无参构造函数
Date d2(2015, 1, 1);//调用带参构造函数
//Date d3()意义?
}

缺省参数的构造函数

class Date
{

public:
	//缺省参数的构造函数
	Date(int year = 2000, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	//半缺省参数的构造函数
	Date(int year, int month = 1)
	{
		_year = year;
		_month = month;
		_day = 1;
	}
public:
	void Display()
	{
		cout << _year << _month << _day << endl;
	}
	
private:
	int _year;
	int _month;
	int _day;

};

void Test()
{
Date d1;//调用缺省构造函数
Date d2(2015, 1, 9);
}

2.拷贝构造函数

未显示定义,系统会默认缺省的拷贝构造函数。缺省的拷贝构造函数会依次拷贝类成员进行初始化

class Date
{
public:
	//无参的构造函数
	Date()
	{}
	
	//拷贝构造函数
	Date(const Date& d)//引用传参
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

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

void Test()
{
Date d1;
Date d2(d1);
}

3.析构函数

class Date
{
public:
	~Date()//析构函数
	{}
private:
	int _year;
	int _month;
	int _day;
};

class Array
{
public:
  Array(int size)
   {
      _ptr = (int *)malloc(size*sizeof(int));
   }  

~Array()
 {
 if (_ptr)
  {
   free(_ptr);
   _ptr = 0;
   }
 }

private:
   int *_ptr;
};

4、运算符重载

#include<iostream>
#include<stdlib.h>

using namespace std;

class Date
{

public:
       //构造函数
	Date(int year, int day, int month)
	{
		_year = year;
		_month = month;
		_day = day;
	}

       //拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

         //“==”操作符重载
	bool operator ==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
        void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;


};

void Test()
{
        Date date1(2018, 8, 8);
	date1.Print();

        Date date2 = date1;
	date2.Print();
	
	date2 == date1;

}

5.赋值运算符的重载

拷贝构造函数是创建新的的对象,使用一个已有对象来初始化这个准备创建的对象。赋值运算符的重载是对一个已存在的对象进行拷贝赋值。

重载运算符的调用及编译器的处理如图:

在这里插入图片描述

#include<iostream>
#include<stdlib.h>

using namespace std;
class Date
{
public:
	Date()
	{}
	//拷贝构造函数
	Date(const Date& d)
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}
	Date& operator = (const Date& d)
	{
	if (this != &d)
		{
			this->_year = d._year;
			this->_month = d._month;
			this->_day = d._day;
		}
	return *this;
	}


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

void Test()
{
	Date d1;
	Date d2 = d1;//调用拷贝构造函数

	Date d3;
	d3 = d1;//调用赋值运算符的重载
}

注意:如果返回void类型不能进行连续赋值

6、类的const成员函数
在成员函数后面加const,修饰this指针指向的对象,保证调用这个const成员函数的对象不会被改变。如下
在这里插入图片描述

示例代码:

#include<iostream>
#include<stdlib.h>

using namespace std

class Date
{
public:
        //构造函数
	Date(int year = 2019, int month = 8, int day = 18)
	{
		_year = year;
		_month = month;
		_day = day;
	}
        //普通的成员函数
	void display()
	{
		cout << "display()" << endl;
		cout << "year:" << _year << endl << endl;
	}
        //const成员函数
	void display() const
	{
		cout << "display() const" << endl;
		cout << "month:" << _month << endl;
	}

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

void Test()
{
        Date date1;
	date1.display();//调用普通的成员函数

	const Date date2;
	date2.display();
}

注意事项:
(1)const对象可以调用其它的const函数(举例理解?);
(2)非const对象可以调用非const成员函数和const成员函数;
(3)const成员函数内可以调用其它的const成员函数;
(4)非const成员函数内可以调用其它的const成员函数和非const成员函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值