C++第二部分核心编程2(类和对象(友元,运算符重载))

类和对象

1 友元

1.1全局函数做友元


class building
{
	//好朋友,可以访问
	friend void goodgay(building *b);
public:
	building()
	{
		m_sittingroom = "客厅";
		m_bedroom = "卧室";

	}
	string m_sittingroom;//客厅
private:
	string m_bedroom;//

};
//全局函数
void goodgay(building *b)
{
	cout << " 好基友全局函数正在访问:" << b->m_sittingroom << endl;

}
void test()
{
	building b;
	goodgay(&b);
}



int main()
{
	
	test();

	system("pause");
	return 0;
}

1.2 类做友元

class building;
class goodgay
{

public:
	goodgay();
	void visit();
	

	building *b;

};


class building
{
	friend class goodgay; //类做了友元

public:

	building();

	string m_sittingroom;//客厅
private:
	string m_bedroom;//

};
类外写成员函数
building::building()
{
	m_sittingroom = "客厅";
	m_bedroom = "卧室";
}
goodgay::goodgay()
{
	//创建建筑物的对象
	b = new building;
}

void goodgay::visit()
{
	cout << "好基友在访问:" << b->m_sittingroom << endl;
	cout << "好基友在访问:" << b->m_bedroom << endl;//类做了友元,可以访问了
}

void test()
{
	goodgay gg;
	gg.visit();
}

int main()
{

	test();

	system("pause");
	return 0;
}

1.3成员函数做友元

class building;
class goodgay
{

public:
	goodgay();
	void visit();//让visit访问building中访问
	void visit2();//让visit不访问building中访问

	building *b;

};


class building
{
	friend void goodgay::visit();

public:

	building();

	string m_sittingroom;//客厅
private:
	string m_bedroom;//

};
//类外写成员函数
building::building()
{
	m_sittingroom = "客厅";
	m_bedroom = "卧室";
}
goodgay::goodgay()
{
	//创建建筑物的对象
	b = new building;
}

void goodgay::visit()
{
	cout << "好基友在访问:" << b->m_sittingroom << endl;
	cout << "好基友在访问:" << b->m_bedroom << endl;//
}

void goodgay::visit2()
{
	cout << "好基友在访问:" << b->m_sittingroom << endl;
	//cout << "好基友在访问:" << b->m_bedroom << endl;//
}

void test()
{
	goodgay gg;
	gg.visit();
	gg.visit2();
}
int main()
{

	test();

	system("pause");
	return 0;
}

2.运算符重载

2.1运算符重载+号


class person
{
public:
	//1.成员函数重载+号
	/*person operator + (person &p)
	{
		person temp;
		temp.m_a = this->m_a + p.m_a;
		temp.m_b = this->m_b + p.m_b;
		return temp;
	}*/

	int m_a;
	int m_b;
};
2.全局函数做重载
//person operator+(person &p1, person &p2)
//{
//	person temp;
//	temp.m_a = p1.m_a + p2.m_a;
//	temp.m_b = p1.m_b + p2.m_b;
//	return temp;
//}

//3.函数重载的版本
person operator+(person &p1, int p2)
{
	person temp;
	temp.m_a = p1.m_a + p2;
	temp.m_b = p1.m_b + p2;
	return temp;
}
void test01()
{
	person p1;
	p1.m_a = 10;
	p1.m_b = 20;
	person p2;
	p2.m_a = 10;
	p2.m_b = 20;
	//person p3 = p1 + p2;//重载

	person p3 = p1 + 10;//函数重载的版本

	cout << "p3.m_a=" << p3.m_a << endl;
	cout << "p3.m_b=" << p3.m_b << endl;
}
//
int main()
{

	test01();

	system("pause");
	return 0;
}

2.2运算符重载左移运算符

class Person {
	friend ostream& operator<<(ostream& out, Person& p);

public:

	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}

	//成员函数 实现不了  p << cout 不是我们想要的效果
	//void operator<<(Person& p){
	//}

private:
	int m_A;
	int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}

void test() {

	Person p1(10, 20);

	cout << p1 << "hello world" << endl; //链式编程
}

int main() {

	test();

	system("pause");

	return 0;
}

2.3 运算符重载递增运算符


class MyInteger {

	friend ostream& operator<<(ostream& out, MyInteger myint);

public:
	MyInteger() {
		m_Num = 0;
	}
	//前置++
	MyInteger& operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}

	//后置++
	MyInteger operator++(int) {
		//先返回
		MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
		m_Num++;
		return temp;
	}

private:
	int m_Num;
};


ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_Num;
	return out;
}


//前置++ 先++ 再返回
void test01() {
	MyInteger myInt;
	cout << ++myInt << endl;
	cout << myInt << endl;
}

//后置++ 先返回 再++
void test02() {

	MyInteger myInt;
	cout << myInt++ << endl;
	cout << myInt << endl;
}

int main() {

	test01();
	//test02();

	system("pause");

	return 0;
}

2.4 运算符重载赋值运算符

class Person
{
public:

	Person(int age)
	{
		//将年龄数据开辟到堆区
		m_Age = new int(age);
	}

	//重载赋值运算符 
	Person& operator=(Person &p)
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//编译器提供的代码是浅拷贝
		//m_Age = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);

		//返回自身
		return *this;
	}


	~Person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}

	//年龄的指针
	int *m_Age;

};


void test01()
{
	Person p1(18);

	Person p2(20);

	Person p3(30);

	p3 = p2 = p1; //赋值操作

	cout << "p1的年龄为:" << *p1.m_Age << endl;

	cout << "p2的年龄为:" << *p2.m_Age << endl;

	cout << "p3的年龄为:" << *p3.m_Age << endl;
}

int main() {

	test01();

	//int a = 10;
	//int b = 20;
	//int c = 30;

	//c = b = a;
	//cout << "a = " << a << endl;
	//cout << "b = " << b << endl;
	//cout << "c = " << c << endl;

	system("pause");

	return 0;
}

2.5 运算符重载关系运算符

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	};

	bool operator==(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator!=(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	string m_Name;
	int m_Age;
};

void test01()
{
	//int a = 0;
	//int b = 0;

	Person a("孙悟空", 18);
	Person b("孙悟空", 18);

	if (a == b)
	{
		cout << "a和b相等" << endl;
	}
	else
	{
		cout << "a和b不相等" << endl;
	}

	if (a != b)
	{
		cout << "a和b不相等" << endl;
	}
	else
	{
		cout << "a和b相等" << endl;
	}
}


int main() {

	test01();

	system("pause");

	return 0;
}

2.6 函数调用运算符重载

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}

};
void test01()
{
	//重载的()操作符 也称为仿函数
	MyPrint myFunc;
	myFunc("hello world");
}


class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;

	//匿名对象调用  
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

	test01();
	test02();

	system("pause");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值