C++类和对象(中)

17 篇文章 0 订阅
15 篇文章 0 订阅
#define _CRT_SECURE_NO_WARNINGS 1;

#include<iostream>
using namespace std;
#include<stdlib.h>


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


class Date
{
public:
	//1.无参构造函数
	Date()
	{
		cout << "无参构造函数" << endl;
	}
	//2.带参构造函数
	Date(int year, int mouth, int day)
	{
		_year = year;
		_mouth = mouth;
		_day = day;
		cout << "带参构造函数" << endl;
	}

private:
	int _year;
	int _mouth;
	int _day;
};


int main()
{
	Date d1;    //如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明  

	Date d2(2015, 1, 1);
 
	// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
	Date d3();

	system("pause");
	return 0;
}
#endif

#if 0
//默认构造函数
//无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
//注意:无参 构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数
class Date
{
public:
	Date()
	{
		_year = 1990;
		_mouth = 1;
		_day = 1;
	}

	Date(int year = 1900, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;
	}

private:
	int _year;
	int _mouth;
	int _day;
};

int main()
{
	//Date d1;  //编译不通过,Date包含多个默认构造函数

	system("pause");
	return 0;
}
#endif

//编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数
#if 0
class Time
{
public :
	Time()
	{
		cout << "Time()" << endl;
	}
private:
	int _hour;
	int  _minute;
	int _second;
};

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

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

int main()
{
	Date d;

	system("pause");
	return 0;
}
#endif

//析构函数
//析构函数是特殊的成员函数。
//其特征如下:
//1. 析构函数名是在类名前加上字符 ~。 
//2. 无参数无返回值, 析构函数不能重载。 
//3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
//4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
//5. 注意析构函数体内并不是删除对象,而是做一些清理工作。

#if 0
#include<assert.h>
typedef int DataType;
class SeqList
{
public:
	SeqList(int capacity = 10)
	{
		_pData = (DataType *)malloc(capacity * sizeof(DataType));
		assert(_pData);
		_capacity = capacity;
	}

	~SeqList()
	{
		cout << "~SeqList()" << endl;

		if (_pData)
		{
			free(_pData);    //释放堆空间
			_pData = NULL;  //指针置为空
			_capacity = 0;
			_size = 0;
		}
	}
private:
	int* _pData;
	size_t _size;
	size_t _capacity;
};

void Test()
{
	SeqList sq;
}

int main()
{
	Test();

	system("pause");
	return 0;
}
#endif

#if 0
class A
{
public:
	//explicit A(int a)
	A(int a)
		//:(_a = a)
	{
		_a = a;
	}
	void Display()
	{
		cout << _a << endl;
	}
	void SetA(int a)
	{
		_a = a;
	}

public:
	int _a = 10;
	const int _b = 100;
};

int main()
{
	A a1 = 20;    
	a1.SetA(1);
	//a1 = 30;

	cout << a1._b << endl;
	printf("hello world\n");

	system("pause");
	return 0;
}
#endif

//拷贝构造函数
//1. 拷贝构造函数是构造函数的一个重载形式。 
//2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
//(传参的时候会拷贝一个临时变量,这个临时变量会调用拷贝构造函数,这样就会无限递归)

#if 0
class Date
{
public:
	Date(int year = 1900, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;

		cout << "构造函数" << endl;
	}

	Date(const Date& d)
	{
		/*
		this->_year = d._year;
		this->_mouth = d._mouth;
		this->_day = d._day;
		*/

		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;

		cout << "拷贝构造函数" << endl;
	}

private:
	int _year;
	int _mouth;
	int _day;
};

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

	system("pause");
	return 0;
}
#endif

#if 0
//拷贝构造函数若未显示定义,系统会生成默认的拷贝构造函数
class Date
{
public:
	Date(int year = 1900, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;

		cout << "构造函数" << endl;
	}

private:
	int _year;
	int _mouth;
	int _day;
};

int main()
{
	Date d1;
	Date d2(d1);  //调用默认拷贝构造函数, 浅拷贝

	system("pause");
	return 0;
}
#endif 

#if 0
//需要深拷贝
class String
{
public:
	String(const char* str = "hello world")
	{
		_str = (char*)malloc(strlen(str) + 1); //先申请空间,(后面再加一个'\0'的空间)
		strcpy(_str, str);

		cout << "构造函数" << endl;

		/*
		this->_str = (char*)malloc(strlen(str) + 1);
		strcpy(this->_str, str);
		*/
	}

	~String()
	{
		cout << "析构函数" << endl;
		free(_str);

		//free(this->_str);
	}

private:
	char* _str;
};

int main()
{
	String s1("lcihao lichao");
	//String s2(s1); //利用默认的拷贝构造函数;程序会崩掉。默认生成的是浅拷贝。

	system("pause");
	return 0;
}
#endif

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

//1.不能通过连接其他符号来创建新的操作符:比如operator@ 
//2.重载操作符必须有一个类类型或者枚举类型的操作数
//3.用于内置类型的操作符,其含义不能改变,例如:内置的整型 + ,不 能改变其含义
//4.作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的 
//操作符有一个默认的形参this,限定为第一个形参
//5.  .* (类成员指针访问运算符)、
	//:: (域运算符)
	//sizeof(长度运算符)
	//?: (三目操作符/条件操作运算法) 
	//. (成员访问操作符)
	//注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

class Date
{
public:
	Date(int year = 1900, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;

		cout << "构造函数" << endl;
	}

	//bool operator==(const Date& d1, const Date& d2) 
	//{} 此运算符函数的参数太多

	//bool operator==(Date* this, const Date& d)
	//左操作数是this指向的调用函数的对象
	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _mouth == d._mouth
			&& _day == d._day;
	}


private:
	int _year;
	int _mouth;
	int _day;
};

int main()
{
	Date d1(2019, 4, 23);
	Date d2(2019, 4, 23);
	cout << (d1 == d2) << endl;
	cout << (d1.operator==) << endl;

	system("pause");
	return 0;
}
#endif

#if 0
//赋值运算符重载
class Date
{
public:
	Date(int year = 1990, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;

		/*
		this->_year = year;
		this->_mouth = mouth;
		this->_day = day;
		*/

		cout << "构造函数" << endl;
	}

	Date(const Date& d)
	{
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;

		/*
		this->_year = d._year;
		this->_mouth = d._mouth;
		this->_day = d._day;
		*/

		cout << "拷贝构造函数" << endl;
	}

	//void operator=(const Date& d)
	//{
	//	cout << "赋值运算符的重载" << endl;
	//	if (this != &d)  //防止自己给自己赋值
	//	{
	//		_year = d._year;
	//		_mouth = d._mouth;
	//		_day = d._day;

	//		/*
	//		this->_year = d._year;
	//		this->_mouth = d._mouth;
	//		this->_day = d._day;
	//		*/
	//	}
	//}

	Date& operator=(const Date& d)
	{
		cout << "赋值运算符的重载" << endl;

		if (this != &d)  //防止自己给自己赋值
		{
			_year = d._year;
			_mouth = d._mouth;
			_day = d._day;

			/*
			this->_year = d._year;
			this->_mouth = d._mouth;
			this->_day = d._day;
			*/
		}

		return *this;
	}

private:
	int _year;
	int _mouth;
	int _day;
};

int main()
{
	Date d1;
	Date d2 = d1; //调用拷贝构造函数(因为此处是在定义d2这个对象)

	Date d3;
	d3 = d1; //调用赋值运算符的重载 (因为d3对象已经存在,这里只是在赋值)

	d2 = d3 = d1;  //当定义的赋值运算符的重载还是没有返回值时不能连续赋值,
					//所以赋值运算符的重载函数必须有返回值 (返回值为该类对象的引用)
	
	system("pause");
	return 0;
}
#endif

//赋值运算符主要有五点:
//1. 参数类型 
//2. 返回值 
//3. 检测是否自己给自己赋值 
//4. 返回* this 
//5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,
//	完成对象按字节序的值拷贝。(浅拷贝)

#if 0
class String
{
public:
	String(const char* str = "lichao")
	{
		cout << "构造函数" << endl;

		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}

	~String()
	{
		cout << "析构函数" << endl;
		free(_str);
	}
	
	//String& operator=(const String& s)
	//{
	//	cout << "赋值运算符重载函数" << endl;

	//	if (this != &s)
	//	{
	//		//strcpy(this->_str, s._str);
	//		strcpy(_str, s._str);
	//	}

	//	return *this;
	//}

private:
	char* _str;
};

int main()
{
	String s1("lichao");
	String s2("hellow world");
	
	s2 = s1;  //如果没有显示定义赋值运算符的重载函数,
			  //编译器会生成默认的赋值运算符重载函数(可以完成按字节序的值拷贝)

	system("pause");
	return 0;
}
#endif

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

class Date
{
public:
	Date(int year = 1990, int mouth = 1, int day = 1)
	{
		_year = year;
		_mouth = mouth;
		_day = day;
	}

	//void Display(const Date* this) //编译器对const 成员函数的处理

	void Display()const  //const 修饰的成员函数
	{
		cout << "Display()const" << endl;
		cout << this->_year << "-" << _mouth << "-" << _day << endl;

		//SetDate();  //const 成员函数不能调用其他的非const 成员函数

		SetDate(); //const成员函数可以调用其他的const 成员函数
	}

	void Display()
	{
		cout << "Display()" << endl;
		cout << _year << "-" << _mouth << "-" << _day << endl;

		SetDate();  //非const 成员函数可以调用其他的const成员函数
	}

	//void SetDate()
	//{
	//	cout << "SetDate()" << endl;
	//}

	void SetDate()const
	{
		cout << "SetDate()const" << endl;
	}

private:
	int _year;
	int _mouth;
	int _day;
};

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

	const Date d2;  //const 对象可以调用其他的从const 成员函数
	d2.Display(); 
	d2.SetDate();

	//const 对象不可以调用非const 成员函数
	//非const对象可以调用const成员函数
	
	system("pause");
	return 0;
}

//const 对象不可以调用非const 成员函数
//const 成员函数不能调用其他的非const 成员函数
//非const对象可以调用const成员函数
//非const 成员函数可以调用其他的const成员函数

//const 对象可以调用其他的const成员函数
//const 成员函数可以调用其他的const 成员函数

#endif

# if 0
//取地址及const 去地址操作符重载

//这两个默认成员函数一般不用重新定义 ,编译器默认会生成
//如果想让别人获取到指定的内容则需要重载

class Date
{
public:
	Date* operator&()
	{
		return this;
	}

	const Date* operator&()const
	{
		return this;
	}

private:
	int _year;
	int _mouth;
	int _day;
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值