【C++】类与对象(中)

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

如果一个类中什么成员都没有,简称为空类。

空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。

默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

2、构造函数

2.1 概念

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

2.2 特性

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

其特征如下:

  • 函数名与类名相同。

  • 无返回值。

  • 对象实例化时编译器自动调用对应的构造函数。

  • 构造函数可以重载。

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(2024, 10, 1); // 调用带参构造函数

	// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
	// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
	//Date d3(); // 警告
}
  • 如果类中没有显式定义构造函数,则 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;
};

void TestDate()
{
	// 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
	// 函数放开后,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成无参构造函数
	Date d1;
	d1.Print();
}

d1 对象调用了编译器生成的默认构造函数,但是 d1 对象的_ year/_ month/_ day,依旧是随机值。 

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

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

class Date
{
private:
	// 内置类型(基本类型)
	int _year;
	int _month;
	int _day;

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

void Test()
{
	Date d;
}

构造函数,也是默认成员函数,不写,编译器会自动生成
编译生成的默认构造的特点:
1、不写才会生成,写了任意一个构造函数就不会生成了
2、内置类型的成员不会处理(C++11,声明支持给缺省值)
3、自定义类型的成员才会处理,会去调用这个成员的默认构造函数

总结:一般情况都需要我们自己写构造函数,决定初始化方式。成员变量全是自定义类型,可以考虑不写构造函数

注意:

C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值

class Date
{
private:
	// 内置类型(基本类型)
	int _year = 1970; // 声明时给缺省值
	int _month = 1;
	int _day = 1;

	// 自定义类型
	Time _t;
};
  • 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。

注意:

无参构造函数全缺省构造函数我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。(特点:不传参就可以调用的构造就是默认构造)

class Date
{
public:
	Date()
	{
		_year = 1970;
		_month = 1;
		_day = 1;
	}
	Date(int year = 1970, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};

// 下面的测试函数不能通过编译
// 多个并存会存在调用的二义性
void Test()
{
	//Date d1; // 这里d1不知道要调用哪个构造函数
}

3、析构函数

3.1 概念

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

3.2 特性

析构函数是特殊的成员函数

其特征如下:

  • 析构函数名是在类名前加上字符  ~。

  • 无参数无返回值类型。

  • 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载。

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

  • 关于编译器自动生成的析构函数,是否会完成一些事情呢?看下面的程序,编译器生成的默认析构函数,对自定类型成员调用它的析构函数。
class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	int _year = 1970;
	int _month = 1;
	int _day = 1;

	Time _t;
};

void Test()
{
	Date d;
}

在 main 方法中没有直接创建 Time 类的对象,为什么最后会调用 Time 类的析构函数?
因为:main 方法中创建了 Date 对象 d,而 d 中包含4个成员变量,其中 _year, _month, _day 三个是内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而 _t 是 Time 类对象,所以在 d 销毁时,要将其内部包含的 Time 类的 _t 对象销毁,所以要调用Time 类的析构函数。
但是:main 函数中不能直接调用 Time 类的析构函数,实际要释放的是 Date 类对象,所以编译器会调用 Date 类的析构函数,而 Date 没有显式提供,则编译器会给 Date 类生成一个默认的析构函数,目的是在其内部调用 Time 类的析构函数,即当 Date 对象销毁时,要保证其内部每个自定义对象都可以正确销毁。main 函数中并没有直接调用 Time 类析构函数,而是显式调用编译器为 Date 类生成的默认析构函数
注意:创建哪个类的对象则调用该类的构造函数,销毁哪个类的对象则调用该类的析构函数

  • 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date 类;有资源申请时,一定要写,否则会造成资源泄漏,比如 Stack 类。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (nullptr == _array)
		{
			perror("malloc");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};

void TestStack()
{
	Stack s;
}

4、拷贝构造函数

4.1 概念

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

4.2 特性

拷贝构造函数也是特殊的成员函数

其特征如下:

  • 拷贝构造函数是构造函数的一个重载形式。

  • 拷贝构造函数的参数只有一个且必须是同类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。

class Date
{
public:
	Date(int year = 1970, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Date(const Date d) // 错误写法,编译报错,会引发无穷递归
	Date(const Date& d)
	{
		_year = d._year; // 加const防止这两个写反了
		_month = d._month;
		_day = d._day;

	}
private:
	int _year = 1970;
	int _month = 1;
	int _day = 1;
};

void TestDate()
{
	Date d1;
	// 等价的两种写法
	Date d2(d1);
	Date d3 = d1;
}

注意:

Date d2(d1) 要去调用 Date 拷贝构造函数,调用这个函数之前要先传值传参(传入d1),传参又会形成一个新的拷贝构造...无穷递归。

  • 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数通过逐个复制对象的每个成员来初始化新对象,这种拷贝叫做浅拷贝,或者值拷贝。
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(const Time&)" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	int _year = 1970;
	int _month = 1;
	int _day = 1;

	Time _t;
};

void Test()
{
	Date d1;

	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
	Date d2(d1);
}

注意:

在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。

  • Stack需要我们自己实现深拷贝的拷贝构造,默认生成的会出现问题。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	//this指向s2,参数s是对s1的引用
	Stack(Stack& s)
	{
		// 深拷贝
		_array = (DataType*)malloc(sizeof(DataType) * s._capacity);
		if (nullptr == _array)
		{
			perror("malloc");
			return;
		}

		memcpy(_array, s._array, sizeof(DataType) * s._size);
		_size = s._size;
		_capacity = s._capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};

void TestStack()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);
}

如果没有写拷贝构造函数:

注意:

类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。

  • 拷贝构造函数典型调用场景:
  1. 使用已存在对象创建新对象
  2. 函数参数类型为类类型对象
  3. 函数返回值类型为类类型对象
class Date
{
public:
	Date(int year, int minute, int day)
	{
		cout << "Date(int,int,int):" << this << endl;
	}
	Date(const Date& d)
	{
		cout << "Date(const Date& d):" << this << endl;
	}
	~Date()
	{
		cout << "~Date():" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

// 函数参数类型为类类型对象
// 函数返回值类型为类类型对象
Date Test(Date d)
{
	Date temp(d);
	return temp;
}

int main()
{
	Date d1(2024, 10, 1);
    // 使用已存在对象创建新对象
	Test(d1);
	return 0;
}

Date(int,int,int):00DEF940  this指向d1对象的地址
Date(const Date& d):00DEF830  this指向临时d的地址
Date(const Date& d):00DEF804  this指向临时tmep对象的地址
Date(const Date& d):00DEF860  this指向返回值的临时对象地址
~Date():00DEF804  销毁Test函数中的temp
~Date():00DEF830  销毁Test函数参数d
~Date():00DEF860  销毁Test函数返回时创建的临时对象
~Date():00DEF940  销毁main函数中的d1

为什么销毁的顺序是这样的?

局部对象优先销毁:在一个作用域(如Test函数)内,最后创建的局部对象会最先被销毁。这意味着 temp 对象在 d 对象之后创建,因此 temp 先被销毁。

函数参数的生命周期:函数参数 d 的生命周期与函数内部的局部变量相同。当函数结束时,所有局部变量和参数按相反的构造顺序被销毁。

临时对象的销毁:返回值 temp 会被拷贝到一个临时对象中。这个临时对象的生命周期通常延续到表达式结束时,因此在函数结束后立即被销毁。

全局对象的最后销毁:d1 是 main 函数中的对象,生命周期持续到 main 函数结束。因此,它是最后被销毁的对象。

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

5、赋值运算符重载

5.1 运算符重载

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

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

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

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@ 
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能改变,例如:内置的整型 +,不能改变其含义
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的 this
  • .*  作用域访问符::  运算符sizeof  三目运算符? :  .  注意以上 5 个运算符不能重载。
  • 不能改变操作符的操作数个数,一个操作符是几个操作数,那么重载的时候就有几个参数。 
class Date
{
public:
	Date(int year = 1970, 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, 28);
	cout << (d1 == d2) << endl;
}
class Date
{
public:
	Date(int year = 1970, 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, 28);
	cout << (d1 == d2) << endl;
}

5.2 赋值运算符重载

  • 赋值运算符重载的格式:
  1. 参数类型:const T&,传递引用可以提高传参效率

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

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

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

class Date
{
public:
	Date(int year = 1970, 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;
};
  • 赋值运算符只能重载成类的成员函数不能重载成全局函数

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

  • 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

注意:

内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。

class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time& operator=(const Time& t)
	{
		// 不可以*this != t,因为this是指针,是内置类型直接用!=
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}

		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	int _year = 1970;
	int _month = 1;
	int _day = 1;

	Time _t;
};

既然编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了,还需要自己实现吗?

像日期类这样的类是没必要的。那么下面的类呢?

// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};

void TestStack()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2;
	s2 = s1;
}

注意:

如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现。

6、日期类的实现

Date.h

#include <iostream>
using namespace std;

class Date
{
	// 友元声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month) const;
	// 构造函数
	Date(int year, int month, int day);
	void Print() const;
	// 赋值运算符重载
	Date& operator=(const Date& d);
	// 比较运算符重载
	bool operator<(const Date& d) const;
	bool operator==(const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;
	bool operator!=(const Date& d) const;
	// 加减运算符重载
	Date& operator+=(int day);
	Date operator+(int day) const;
	Date& operator-=(int day);
	Date operator-(int day) const;
	// 自增自减运算符重载
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	// 日期相减
	int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include "Date.h"

int Date::GetMonthDay(int year, int month) const
{
	const static int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month == 2
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return monthArray[month];
}

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

	// 检查日期是否合法
	if (month < 1 || month>12
		|| day<1 || day>GetMonthDay(year, month))
	{
		cout << "非法日期" << endl;
	}
}

void Date::Print() const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

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

bool Date::operator==(const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator<=(const Date& d) const
{
	return *this < d || *this == d;
}

bool Date::operator>(const Date& d) const
{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

Date& Date::operator+=(int day)
{
	if (day < 0)
		return *this -= (-day);

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}

	return *this;
}

Date Date::operator+(int day) const
{
	Date tmp(*this);
	tmp += day;

	// 出作用域tmp就被销毁了,不能用引用返回
	return tmp;
}

Date& Date::operator-=(int day)
{
	if (_day < 0)
		return *this += (-day);

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

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

	return tmp;
}

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

// 加一个int参数进行占位,和前置++构成函数重载
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;

	return tmp;
}

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

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

	return tmp;
}

int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

ostream& operator<<(ostream& out, const Date& d)
{
	cout << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

test.cpp

#include "Date.h"

void TestDate1()
{
	Date d1(2024, 10, 1);
	// 拷贝构造是一个已经存在的对象去初始化另一个要创建的对象
	Date d2(d1);
	d2.Print();

	Date d3(2024, 10, 2);
	// 赋值是两个已经存在的对象拷贝
	d1 = d3;
	// 或者
	//d1.operator=(d3);
	d1.Print();
}

void TestDate2()
{
	Date d1(2024, 10, 1);
	d1 += 4000;
	d1.Print();

	Date d2(2024, 10, 1);
	d2 += -4000;
	d2.Print();
}

void TestDate3()
{
	Date d1(2024, 10, 1);
	// --d1 -> d1.operator--()
	Date ret1 = --d1;
	d1.Print();
	ret1.Print();

	// d1-- -> d1.operator--(0)
	Date ret2 = d1--;
	d1.Print();
	ret2.Print();
}

void TestDate4()
{
	Date d1(2024, 10, 1);
	Date d2(2000, 8, 1);

	cout << d1 - d2 << endl;
}

void TestDate5()
{
	Date d1(2024, 10, 1);
	Date d2(2024, 10, 2);

	cout << d1 << d2;

	cin >> d1;
	cout << d1;
}

int main()
{
	TestDate5();
	return 0;
}

7、const成员

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

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << "Print()" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
	void Print() const
	{
		cout << "Print()const" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2024, 10, 1);
	d1.Print();
	const Date d2(2024, 11, 1);
	d2.Print();
	return 0;
}

几个问题:

const 对象可以调用非 const 成员函数吗?不可

比如 d2 不可以调用非 const 成员函数

非 const 对象可以调用const成员函数吗?可

比如 d1 对象可以调用 const 成员函数

const 成员函数内可以调用其它的非 const 成员函数吗?不可

// 假设有这样的代码
void ModifyDate(int newYear, int newMonth, int newDay)
{
    _year = newYear;
    _month = newMonth;
    _day = newDay;
}

void Print() const
{
    cout << "Print() const" << endl;
    cout << "year:" << _year << endl;
    cout << "month:" << _month << endl;
    cout << "day:" << _day << endl << endl;

    // 尝试调用非const成员函数
    //ModifyDate(2023, 2, 14); // 编译错误
}

非 const 成员函数内可以调用其它的 const 成员函数吗? 可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值