类和对象(中)

目录

类的6个默认成员函数:

构造函数:

概念:

特性:

默认构造函数:

介绍:

用途:

析构函数:

概念:

特性:

拷贝构造函数:

概念:

特征:

浅拷贝:

深拷贝:

赋值运算符重载:

运算符重载:

全局:

局部:

赋值运算符重载:

赋值运算符重载格式

赋值运算符只能重载成类的成员函数不能重载成全局函数 :

前置++和后置++重载:

日期类的实现:

const成员函数:

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


类的6个默认成员函数:


构造函数:

概念:

类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。

构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。

构造函数可用于为某些成员变量设置初始值。

构造函数的主要任务并不是开空间创建对象,而是初始化对象。

特性:

  • 函数名与类名相同
  • 无返回值
  • 对象实例化时编译器自动调用对应的构造函数
  • 构造函数可以重载
class Test
{
public:
	/*常规初始化
	void Init(int year = 1,int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/

	//使用构造函数
	//无参构造函数
	Test()
	{

	}

	//有参构造函数
	Test(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

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

int main()
{
    //常规初始化
    Test T1;
    T1.Init(2024,4,8);
    
    //无参构造函数初始化
    Test T1;

    //有参构造函数初始化
	Test T1(2024,4,8);
	T1.Print();
	return 0;
}

默认构造函数:

介绍:

如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦

用户显式定义编译器将不再生成
 class Date
 {
  public:
 // 如果用户显式定义了构造函数,编译器将不再生成
     Date(int year, int month, int day)
     {
         _year = year;
         _month = month;
         _day = day;
     }
 
 void Print()
 {
     cout << _year << "-" << _month << "-" << _day << endl;
 }
  
  private:
     int _year;
     int _month;
     int _day;
 };
  
  int main()
 {
     Date d1;
     return 0;
 }

用途:

C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类
型,如:int/char...,自定义类型就是我们使用class/struct/union等自己定义的类型,看看
下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员
函数

对于内置类型不作处理

对于自定义成员变量调用它的默认构造函数

(无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数)
class Time
{
public:
	Time(int hour = 1)//不传参数就可以调用的就是构造函数
	{
		cout << "Time()" << endl;
		_hour = hour;
		_minute = 0;
	}
private:
	int _hour;
	int _minute;
};

class A
{
public:
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
    //:内置类型成员变量在类中声明时可以给默认值
	int _year = 1;
	int _month = 1;
	int _day = 1;
	Time T;
};

int main()
{
	A a;
	a.Print();
	return 0;
}

析构函数:

概念:

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

特性:

  • 析构函数名是在类名前加上字符 ~
  • 无参数无返回值类型
  • 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数
  • 函数不能重载
  • 对象生命周期结束时,C++编译系统系统自动调用析构函数
typedef int DataType;
class Stack
{
public:
	Stack(int n = 4)
	{
		data = (DataType*)malloc(sizeof(DataType) * n);
		if (data == NULL)
		{
			perror("malloc fail");
			return;
		}
		size = n;
		top = 0;
	}
	void Push(int x)
	{
		assert(data);
		data[top++] = x;
	}
	int Top()
	{
		assert(data);
		return data[top - 1];
	}
	void Print()
	{
		while (top != 0)
		{
			cout << data[top-1] << "-";
			top--;
		}
	}
	~Stack()
	{
		if (data)
		{
			free(data);
			data = NULL;
			size = 0;
			top = 0;
		}

	}
private:
	DataType* data;
	int size;
	int top;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	s1.Print();
	return 0;
}

拷贝构造函数:

概念:

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

特征:

  •  拷贝构造函数是构造函数的一个重载形式
  • 拷贝构造函数的参数只有一个且必须是类类型对象的引用 
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)
    //Date(const Date d)  :错误写法,会引起编译器无穷递归
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date D1(2024, 4, 10);
	Date D2 = D1;
	D1.Print();
	D2.Print();
	return 0;
}

浅拷贝:

若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按
字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝
class Time
{
public:
    Time()
    {
        _hour = 1;
        _minute = 1;
        _second = 1;
    }
    Time(const Time& t)
    {
        _hour = t._hour;
        _minute = t._minute;
        _second = t._second;
        cout << "Time::Time(const Time&)" << endl;
    }
private:
    int _hour;
    int _minute;
    int _second;
};
class Date
{
private:
    // 基本类型(内置类型)
    int _year = 1970;
    int _month = 1;
    int _day = 1;
// 自定义类型
    Time _t;
//在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的
//自定义类型是调用其拷贝构造函数完成拷贝的
};
int main()
{
    Date d1;
    Date d2(d1);
    return 0;
}

深拷贝:

当类中涉及资源申请时,类中不包含拷贝构造函数的浅拷贝构造会使程序崩溃掉,这时要在类中定义拷贝构造函数去解决程序崩溃


赋值运算符重载:

运算符重载:

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
函数原型:返回值类型 + operator + 操作符(参数列表)
以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(2024,4,12);
    Date d2(2024,4,15);
    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;
};

赋值运算符重载:

赋值运算符重载格式

参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
检测是否自己给自己赋值
返回*this :要复合连续赋值的含义
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;
    }
        
    return *this;
 }
private:
    int _year ;
    int _month ;
    int _day ;
};

赋值运算符只能重载成类的成员函数不能重载成全局函数

赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。

前置++和后置++重载:

前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载,C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器 自动传递
注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
一份,然后给this+1而temp是临时对象,因此只能以值的方式返回,不能返回引用
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
         _year = year;
         _month = month;
         _day = day;
    }
 // 前置++:返回+1之后的结果
 // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
    Date& operator++()
    {
        _day += 1;
        return *this;
    }

    Date operator++(int)
    {
        Date temp(*this);
        _day += 1;
        return temp;
    }
private:
    int _year;
    int _month;
    int _day;
};

日期类的实现:

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	int day[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 || (year / 100 != 0 && year / 4 == 0) && (year / 400 == 0))
	{
		return 29;
	}
	else
	{
		return day[month];
	}
}
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
// 拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
// 赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
// 析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
//函数打印
void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while(_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// 日期-天数
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		_day += GetMonthDay(_year, _month);
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}

//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	tmp -= 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

// >运算符重载
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
	}
	if (_year == d._year)
	{
		if (_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{
	return *this > d || *this == d;
}
// <运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this <= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	int n = 0;
	Date max = d;
	Date min = *this;
	if (*this > d)
	{
		max = *this;
		min = d;
	}
	while (max > min)
	{
		n++;
		min +=1;
	}
	return n;
}

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 ; // 日
};
  • 72
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 46
    评论
评论 46
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值