栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++特殊成员

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++特殊成员

const成员

const数据成员

具有常属性,const类型变量不是不可以修改,而是只读模式

必须采用初始化参数列表进行初始化

const成员函数

写法上const是写在函数后面得

常成员函数是不能够修改数据成员的,只读数据成员

常成员函数可以与普通函数同时存在,也就是说函数名可以相同。

普通对象优先调用普通函数,当没用同名的时候普通对象可以调用常成员函数

const对象

用const修饰的对象

特点:常对象只能调用常成员函数,不能调用普通函数。

#include
#include
using namespace std;
class MM {
public:
	MM(string name, int age):age(age) {
		MM::name = name;	//name名字相同时可以用类名限定,可用初始化参数列表也可不用
		//MM::age = age;	常属性成员不能在里面直接初始化
	}
	void print() {
		//age = 18;只读
		cout << name << "t" << age << endl;
	}
	void print()const {
		//name = "xiaofang";	不能修改
		cout << "常成员函数" << endl;
	}
protected:
	string name;
	const int age;//const 数据成员
};

int main() {
	MM mm("对象",18);
	mm.print();
	const MM cmm("常对象", 21);
	cmm.print();
	return 0;
}

static成员

static成员依然受权限限定 

static成员不属于对象,是属于类的(意味着是所有对象共有的),调用可以不需要对象,当然也可以用对象调用

static数据成员

必须在类外初始化,不能在类内初始化,也不能用初始化参数列表,类外初始化,不需要static修饰但是需要类名限定

static成员函数

static写在函数前面即可

特点:调用非静态成员必须指定对象。

static对象

和静态数据相同,只需要注意释放是最后释放的

#include
#include
using namespace std;
class GG {
public:
	GG(string name="") :name(name) {
		num++;
	}
	static void printStatic();
	static void printData(const GG& jj) {
		cout << jj.name << endl;
	}
protected:
	string name;
public:
	static int num;
};
void GG::printStatic() {//传参对象参数,或者在里面定义一个对象都可以
	//调用非静态成员必须指定对象。 
	//cout << name << endl; 非静态成员引用必须与特定对象相对
	cout << num << endl;//
	GG boy2;
	cout << boy2.name << endl;
	cout << "静态成员函数" << endl;
}
//类外初始化,不需要static修饰但是需要类名限定
int GG::num = 1;
int main() {
	//静态数据成员可以不用对象
	cout << GG::num << endl;
	//什么叫共有的
	GG boy("gege");
	//静态数据成员可以用对象访问
	cout << boy.num << endl;
	GG arr[3];
	GG* pGg = new GG;
	cout << GG::num << endl;
	cout << pGg->num << endl;
	cout << boy.num << endl;
	delete pGg;
	pGg = nullptr;

	GG::printStatic();
	return 0;
}

友元

用freind修饰的叫友元,友元只是提供一个场所(范围)赋予对象具有打破类的权限限定(无视权限),并不是说可以忽视原来的规则 

友元函数

普通友元函数

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

顺序:

B类

A类

A类的友元函数,也就是B类的成员函数

友元类

#include
#include
using namespace std;
//前项声明
void printData();
class GG {
public:
	GG(string name, int age) :name(name), age(age){}
	void print() {
		cout << name <<"t" << age << endl;
	}
	friend void printData() {//友元函数不属于类 
		//访问不了cout << name << "t" << age << endl;
		GG gg("shuaige", 19);
		cout << gg.name << "t" << gg.age << endl;//在这个函数内可以无视权限
	}
protected:
	string name;
private:
	int age;
	friend void printData2(GG& gg);
};
//类外实现函数定义不需要类名限定,也不需要friend
void printData2(GG& gg){
	cout<< gg.name << "t" << gg.age << endl;
}

//以另一个类的成员函数充当友元函数
//前项声明
class B {
public:
	void printA();
protected:
};
class A {
public:
	friend void B::printA();
protected:
	string name="A";
};
void B::printA()
{
	A a;
	cout << a.name << endl;
}

//友元类
class Girl {
	friend class Boy;
public:
	Girl(string name,int age):name(name),age(age){}

protected:
	string name;
	int age;
};
class Boy {
public:
	void print() {
		Girl mm("女孩", 18);
		cout << mm.name << " " << mm.age << endl;
	}
	void printGirl(Girl& mm) {
		cout << mm.name << " " << mm.age << endl;
	}
	Girl& returnGirl(Girl& mm) {
		return mm;	
	}

	
protected:
};

int main() {
	GG gg("帅哥", 18);
	gg.print();
	printData();
	printData2(gg);

	B b;
	b.printA();

	Girl mm("花菇凉",20);
	Boy bb;
	bb.print();
	bb.printGirl(mm);
	//cout << bb.returnGirl(mm).name << endl;错误,出了友元类,没有权限
	
	return 0;
}

this 指针与explicit

explicit主要是修饰构造函数使用,可以不让隐式转换构造。  

this指针

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

2.充当函数返回值,返回对象本身,用*this表示对象本身

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

类外没有this指针

#include
using namespace std;

class GG {
public:
	explicit GG(int age):age(age){}
	void print() {
		cout << age << endl;
	}
protected:
	int age;
};
class Boy {
public:
	Boy(string name, int age) :name(name),age(age) {}
	//普通函数不存在初始化参数列表
	void initData(string name, int age) {
		//要解决参数和成员数据同名1.用类名限定,2.用this指针标识
		Boy::name = name;
		this->age = age;
	}
	void print() {
		cout << this->name << "   " << this->age << endl;
	}
	//返回对象本身
	Boy returnBoy() {
		return *this;
	}
	
protected:
	string name;
	int age;
};

int main() {
	//不让隐式转换
	
	GG gg(18);
	gg.print();


	Boy boy("shuangge",19);
	boy.print();
	boy.initData("dagege", 20);
	boy.print();

	boy.returnBoy().returnBoy().returnBoy().print();
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604360.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号