C++基础 04

1.const下载函数上,修饰的是形参中的this指针

class Teacher
{
public:
	
    void const seta(int a)  //这里的const修饰的this指针:void seta(const Teacher* const t,int a)
	{
		 this->a=a ;//这里会报错,原则上回报错,但是在windows平台,vs2015上没有报错,照样可以修改this->a的值;
		 cout << this->a << endl;
	}
private:
	int a;

};


void main(){
	Teacher t;
	t.seta(200);
	system("pause");
}

2.友元函数

class Teacher
{
public:
	//在类的内部声明友元函数,这个函数在类的外部实现的,可以修改类的私有属性;
	friend void xiugaiA(Teacher * t, int a);
	void setA(int a)
	{
		this->a = a;
	}
	int getA()
	{
		return a;
	}
private:
	int a;
};

void xiugaiA(Teacher * t,int a)
{
	t->a = a;
}

void main(){
	Teacher t;
	t.setA(100);
	xiugaiA(&t,200);
	cout << t.getA() << endl;
	system("pause");
}

3.友元类

class Student {
public:
	friend class Teacher;//声明友元类,在声明的友元类中可以访问本类的私有属性和函数;
	void setA(int a)
	{
		this->a = a;
	}
	int getA()
	{
		return a;
	}
private:
	int getB()
	{
		return b;
	}
	int a;
	int b=1000000;
};

class Teacher
{
public:
	void setA(int a)
	{
		this->a = a;
	}
	void setStuA(int a)
	{
		stu.setA(a);
	}
	int getStuA()
	{
		return stu.getA();
	}
	int getStuB()
	{
		return stu.getB();
	}
private:
	int a;
	Student stu;
	
};

void main(){
	Teacher t;
	t.setStuA(11111);
	cout << t.getStuA() << endl;
	system("pause");
}

友元类和函数,和java中的反射机制是类似的,都是打开的类的封装;访问私有属性和方法;



4.运算符重载基础

class Student {
public:
	int a;
	Student(int a)
	{
		this->a = a;
	}
};

Student addobject(Student & t1, Student & t2)
{
	Student temp(t1.a + t2.a);
	return temp;
}

//+号运算符重载了;
Student operator+(Student & t1, Student & t2)
{
	Student temp(t1.a + t2.a);
	return temp;
}

void main(){
	Student t1(10), t2(20);
	//Student t3 = addobject(t1,t2);
	Student t3 = t1 + t2;//运算重载的本质也是函数的调用,只不过换了一种写法;
	cout << t3.a << endl;
	system("pause");
}


5.运算符重载在 成员函数 和 全局函数 的时候

class Student {
	friend Student operator+(Student & t1, Student & t2);
	friend Student addobject(Student & t1, Student & t2);
private:
	int a;
public:
	Student(int a)
	{
		this->a = a;
	}
	int geta()
	{
		return a;
	}
	//在类的内部做运算符重载
	Student operator-(Student & t)
	{
		Student stu(this->a-t.a);
		return stu;
	}
};

Student addobject(Student & t1, Student & t2)
{
	Student temp(t1.a + t2.a);
	return temp;
}

//+号运算符重载了;全局函数做运算符重载;a是私有属性,必须要和友元函数结合使用;
Student operator+(Student & t1, Student & t2)
{
	Student temp(t1.a + t2.a);
	return temp;
}

void main(){
	Student t1(10), t2(20);
	//Student t3 = addobject(t1,t2);
	Student t3 = t1 + t2;//运算重载的本质也是函数的调用,只不过换了一种写法;
	cout << t3.geta()<< endl;

	Student t4 = t1 - t2;
	cout << t4.geta() << endl;
	system("pause");
}


6.等号运算符重载

class Student {
public:
	int a;
	char * name="jjj";
	Student(int a,char *na)
	{
		this->a = a;
		int len=strlen(na);
		cout << "构造函数" << len << endl;
		this->name = new char[len+1];
		strcpy(name,na);
	}
	Student(const Student& stu)
	{
		cout << "拷贝构造函数" << endl;
		int len = strlen(stu.name);
		this->name = new char[len + 1];
		strcpy(name, stu.name);
	}

	//对象的等号运算符重载,只适用于 已经初始化的对象之间的赋值 t1=t2;
	Student& operator=(Student& stu)
	{
		a = stu.a;
		if (name!=NULL)
		{
			delete(name);
			name = NULL;
		}
		int len = strlen(stu.name);
		name = new char[len+1];
		strcpy(name,stu.name);
		return *this;
	}
	~Student()
	{
		if (this->name != NULL)
		{
			delete this->name;
			this->name = NULL;
		}
	}
};


void main(){
	Student t1(1, "hahah");
	Student t2(8, "heheheh");
	//t1 = t2;//这里是把t2对象的数据复制一份到t1中,所以两个对象的指针指向的是同一块内存空间,析构会出问题的;
	Student t3 = t1;//这种调用的是拷贝构造函数,需要在拷贝构造函数中去申请内存拷贝数据;
	cout << t3.name << endl;
	system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值