[c++]——类与对象(中篇)

类与对象——中篇

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

在这里插入图片描述

2.构造函数

特性:
1.函数名与类名相同
2.没有返回值
3. 构造函数可以重载
4. 实例化时自动调用对应的构造函数

样例1:(无参构造)

class Date
{
public:	
	Date()//无参构造   同时也可以不写,系统自动调用构造函数
	{}
	void Display()
	{		cout << this->_year << "-"<<this->_month <<"-"<< this->_day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;	
	a.Display();	
	system("pause");
	return 0;
	}

样例2:

class Date
{
public:	
	Date()
	{
		this->_year = 2018;//后两个是初始值
	}
	void Display()
	{
		cout << this->_year << "-"<<this->_month <<"-"<< this->_day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;	
	a.Display();
	system("pause");
	return 0;
}

无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参
构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。

样例3:

class Date//报错,无参的俩个构造函数不构成重载
{
public:	
	Date()
	{
		this->_year = 2018;
	}
	Date()
	{
		this->_year = 2018;
		this->_month = 1;
		this->_day = 1;
	}
	Date(int year = 2019,int month = 1,int day = 1)
	{
		this->_year = year;
		this->_month = month;
		this->_day = day;
	}
	void Display()
	{
		cout << this->_year << "-"<<this->_month <<"-"<< this->_day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	Date b(2017, 2, 3);
	a.Display();
	b.Display();

	system("pause");
	return 0;
}

错误调用

Date b();//显示未调用原型函数

C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如

int/char…,自定义类型就是我们使用class/struct/union自己定义的类型,看看下面的程序,就会发现
编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数(函数并不会自动初始化内置类型)

class time
{
public:
	void _print()
	{
		cout << "time()" << endl;
	}
};
class Date
{
public:	
	Date(int year = 2019,int month = 1,int day = 1)
	{
		this->_year = year;
		this->_month = month;
		this->_day = day;
	}
	void Display()
	{
		cout << this->_year << "-"<<this->_month <<"-"<< this->_day << endl;
	}

//private://暂时为了访问t改成public
	int _year;
	int _month;
	int _day;

	time t;
};



int main()
{
	Date a;
	a.t._print();//最后我们打印出了time()说明符合以上原则
	system("pause");
	return 0;
}

3.析构函数

概念:对象销毁时会自动调用析构函数,完成类的一些资源清理工作。
特征:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未***显式***定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
    样例1:
typedef int DataType;//所有的数据结构都需要我们自己清理资源,因为析构函数不懂得怎么释放int*,file*等指针
class SeqList
{
public :
 SeqList (int capacity = 10)
 {
 _pData = (DataType*)malloc(capacity * sizeof(DataType));
 assert(_pData);

 _size = 0;
 _capacity = capacity;
 }

 ~SeqList()
 {
 if (_pData)
 {
 free(_pData ); // 释放堆上的空间
 _pData = NULL; // 将指针置为空
 _capacity = 0;
 _size = 0;
 }
 }

private :
 int* _pData ;//数据结构中的数据和大小实际上都是内置类型,c++不会对内置类型进行处理
 size_t _size;
 size_t _capacity;
};

样例2:

class String//证明c++会对自定义类型进行析构函数的调用
{
public:
	String(const char* str = "jack")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~string()" << endl;
		free(_str);
	}
private:
	char* _str;
};

class Preson
{
private:
	String _name;
	int _age;
};
int main()
{
	Preson p;	
	system("pause");
	return 0;
}

4.拷贝构造函数

拷贝构造函数只能引用拷贝,如果传值则会形成无限递归,因为传值时需要拷贝参数,然后参数又无尽的进行拷贝构造。
在这里插入图片描述

class Date
{
public:	
	Date(const Date& a)//此处不写系统将自动生成此函数
	{
		_day = a._day;
		_month = a._month;
		_year = a._year;
	}
	Date(int year = 2019,int month = 1,int day = 1)
	{
		this->_year = year;
		this->_month = month;
		this->_day = day;
	}
	void Display()
	{
		cout << this->_year << "-"<<this->_month <<"-"<< this->_day << endl;
	}
private:
	int _year;
	int _month;
	int _day;	
};
int main()
{
	Date a(2016,2,3);
	Date copy(a);//拷贝构造函数	

	copy.Display();
	a.Display();

	system("pause");
	return 0;
}

字符串的类暂时不能进行浅拷贝,因为如果指针同时指向了同一块空间的话,会对这块空间进行俩次以上的释放,所以程序会崩溃。

5.赋值运算符重载

5.1运算符重载

实现日期类:https://mp.csdn.net/mdeditor/86508064#

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

注意:
1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型或者联合类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
4.作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
5.操作符有一个默认的形参this,限定为第一个形参
6. 注意以下5个运算符不能重载。这个经常在笔试选择题中出现

.* :: sizeof ?: .

6.const成员

某个成员函数中的数据不能狗被改变所以会加上const

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

练习:

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

ps:权限缩小的都可以

猜你喜欢

转载自blog.csdn.net/lucky52529/article/details/86494544