C++中的特殊成员

1.   const成员

       1.1  const数据成员

          const类型变量是不可修改的只读模式,必须采用初始化参数列表方式进行初始化

       1.2  const成员函数  

          写法:const写在函数后面

          常成员函数不能修改数据成员,只可读取

          常成员函数可以和普通函数同时存在

          当常成员函数和普通函数同时存在时,普通对象优先调用普通函

       1.3  const对象

              const修饰的对象,常对象只能调用常成员函数

#include <iostream>
#include <string>
using namespace std;

class student
{
public:
	student(string name, int age)
	{
		student::name = name;
		//student::age = age;//报错,age必须用初始化参数列表方式初始化
	}
	void printdata()
	{
		cout << age << endl;
	}
	void printdata1()const//常成员函数
	{
		//name = "HSHB";//报错,常成员函数不可修改数据
	}
protected:
	string name;
	const int age = 18;//const数据成员,只可使用,不可修改
	//age = 20;//报错,age不可修改
};

int main()
{
	student A("普通对象", 18);
	A.printdata();//普通对象调用普通函数
	A.printdata1();//不报错,可调用常函数
	const student B("常对象", 18);
	B.printdata1();//常对象调用常函数
	//B.printdata();//报错,不可调用普通函数
	return 0;
}

2.  static成员(不属于对象,属于类,调用时可以不使用对象,也可以用对象)

static成员依然受权限限定

    2.1 static数据成员

          必须在类外初始化,不可在类中初始化,也不可在类中初始化参数列表

    2.2 static成员函数

           static写在成员函数之前

           调用非静态成员必须要指定对象

  2.3 static对象

#include <iostream>
#include <string>
using namespace std;

class student
{
public:
	//student(string name,int age):name(name),age(age) {}报错,static不可在类中初始化参数列表
	//age = 20;报错,不可再类中初始化static变量
	static int age;
	static void printdata()
	{
		//cout << name << endl;//报错非静态成员引用必须与特定对象相对
		cout << "静态成员函数" << endl;
	}//调用非静态成员必须要指定对象
	static void pring(student& A)
	{
		cout << A.age << endl << A.name;
	}
protected:
	string name;
	//static int age;
};
//在类外初始化static变量时,不需要再用static修饰,但要用类名限定
int student::age = 18;

int main()
{
	student A;
	//cout << studnet::age << endl;报错,受权限限定
	student::printdata();
	A.printdata();
	student::pring(A);
	return 0;
}

3.  友元      friend描述的关系,友元只是提供一个场所,赋予对象具有打破类的权限限定

3.1友元函数

    普通友元函数

    以另一个类的成员函数充当友元函数

#include <iostream>
#include <string>
using namespace std;

class student
{
public:
	student(string name, int age) :name(name), age(age) {}
	void print()
	{
		cout << name << endl << age;
	}
	friend void printdata()
	{
		student A("djbg", 20);
		//cout << .name << endl << .age;不属于类不能直接访问成员
		cout << A.name << endl << A.age;//友元函数提供一个场所,让对象无视权限
	}
protected:
	string name;
private:
	int age;
	friend void printdata1(student& A);
};
void printdata1(student& A)
{
	cout << A.name << endl << A.age;
}

int main()
{
	student A("lodjnf", 18);;
	A.print();
	printdata1(A);//友元函数,直接访问
	return 0;
}

3.2友元类

class student
{
	friend class teacher;
public:
	student(string name, int age) :name(name), age(age) {}
protected:
	string name;
	int age;
};

class teacher
{
public:
	void print()
	{
		student A("lknf", 18);
		cout << A.name << endl << A.age << endl;
	}
	void print1(student& A)
	{
		cout << A.name << endl << A.age << endl;
	}
protected:

};

int main()
{
	student A("ksnf", 18);
	teacher B;
	B.print();
	B.print1(A);
	return 0;
}

4. this和explicit

4.1explicit修饰构造函数使用,不让隐式转换构造

4.2this指针

     避免形式参数和数据成员同名,通指对象的地址

     充当函数返回值,返回函数本身

     静态成员函数中不可使用this指针

class student
{
public:
	student(int age) :age(age) {}
	void print()
	{
		cout << age << endl;
	}
protected:
	int age;
};

class teacher
{
public:
	teacher(string name, int age) :name(name), age(age) {}
	//普通函数不存在初始化参数列表
	void print()
	{
		//类名限定,帮助计算机识别
		teacher::name = name;
		this->age = age;
	}
	void print()
	{
		cout << this->name << endl << this->age << endl;
	}
	teacher returnT()
	{
		return *this;
	}
protected:
	string name;
	int age;
};


int main()
{
	student A(20);
	A.print();

	teacher B("ljnsa", 20);
	B.print();

	B.returnT();
	return 0;
}

先到这里啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值