C++ 类之间的关系 学习笔记

c++类之间的关系大体分为两类  横向关系和纵相关系

纵向关系:继承

横向关系:关联  组合  聚合 依赖

在这里我们介绍横向关系

从强到弱为组合  聚合  关联  依赖

1.组合

具体定义

例如  我们定义了两个类 手和头

他们分别有自己的功能

又定义了人类

class Person
{
private:
    Head head;
    Hand hand;
public:
    void Meeting();

};

包含了手 和头

可以看出  人类和手和头的生命周期相同  我们称此种方式叫组合

 

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


class Head
{
public:
	void Say();

};

void Head:: Say()
{
	cout << "说" << endl;
}

class Hand
{
public:
	void Move();

};

void Hand::Move()
{
	cout << "比划" << endl;
}


class Person
{
private:
	Head head;
	Hand hand;
public:
	void Meeting();

};

void Person::Meeting()
{
	hand.Move();
	head.Say();
}
int main()
{
	Person ps;
	ps.Meeting();
	return 0;
}

2.关联

关联体现的是两个类之间语义级别的一种强依赖关系,比如我和我的朋友,这种关系比依赖更强、不存在依赖关系的偶然性、关系也不是临时性的,一般是长期性的,而且双方的关系一般是平等的。关联可以是单向、双向的。表现在代码层面,为被关联类CFriend以类的属性形式出现在关联类CPerson中,也可能是关联类CPerson引用了一个类型为被关联类CFriend的全局变量。在UML类图设计中,关联关系用由关联类CPerson指向被关联类CFriend的带箭头实线表示,在关联的两端可以标注关联双方的角色和多重性标记。

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


class Friend
{
private:
	int nRp;
public:
	Friend(int n)
	{
		nRp = n;
	}
	Friend()
	{
		nRp = 0;
	}
	~Friend()
	{
		nRp = 0;
	}
	
	void Play();
	int GetRp()
	{
		return nRp;
	}
};

void Friend::Play()
{
	
		cout << "有朋友" << endl;
	
	
}




class Person
{
private:
	Friend* pFriend;
public:
	
	Person()
	{
		pFriend = nullptr;
	}
	~Person()
	{
		pFriend = nullptr;
	}
	void Play();
	void SetFriend(Friend* pFriend)
	{
		if (pFriend->GetRp() >= 60)
		{
			this->pFriend = pFriend;

		}
		
			

		
	}
};

void Person::Play()
{
	if (pFriend != NULL)
	{

		pFriend->Play();
		cout << "和朋友玩" << endl;
	}
	
	
	else
	{
		cout << "没朋友" << endl;
		cout << "自己玩" << endl;
	}
}
int main()
{
	Friend Fr(53);
	Person ps;
	ps.SetFriend(&Fr);

	ps.Play();
	return 0;
}

3.聚合

聚合是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系。此时整体与部分之间是可分离的,它们可以具有各自的生命周期,部分可以属于多个整体对象,也可以为多个整体对象共享。比如计算机与CPU、公司与员工的关系等,比如一个航母编队包括海空母舰、驱护舰艇、舰载飞机及核动力攻击潜艇等。表现在代码层面,和关联关系是一致的,只能从语义级别来区分。在UML类图设计中,聚合关系以空心菱形加实线箭头表示。

#include<iostream>
using namespace std;


class Person
{
public:
	
	void Play();
};
void Person::Play()
{
	cout << "玩" << endl;
}

class Family
{
private:
	Person* arr[10];
public:
	Family()
	{
		int a = 0;
			for (a; a <= 9; a++)
			{

				arr[a] = NULL;
			}
		
		
	}
	void Play();
	void PushBack(Person*per);
};
void Family::PushBack(Person* per)
{
	
	for(int i=0;i<=9;i++)
	{
		if (arr[i] == NULL)
		{
			arr[i] = per;
			break;
		}
		
	
		
	}
	
}

void Family::Play()
{
	for (int i = 0; i <= 9; i++)
	{
		if (arr[i] != NULL)
		{



			arr[i]->Play();
			



		}
		else
		{



			cout << "---" << endl;

		}
	}
}

int main()
{
	Family Fa;
	Person p1;
	Person p2;
	
	Fa.PushBack(&p1);
	Fa.PushBack(&p2);
	Fa.Play();


	return 0;

}

4.依赖

简单的理解,依赖就是一个类A使用到了另一个类B,而这种使用关系是具有偶然性的、临时性的、非常弱的,但是类B的变化会影响到类A。比如某人要打代码,需要用一台电脑,此时人与电脑之间的关系就是依赖。表现在代码层面,为类CComputer作为参数被类CPerson在某个method方法中使用。在UML类图设计中,依赖关系用由类CPerson指向类CComputer的带箭头虚线表示。

#include<iostream>
using namespace std;


class Computer
{
public:
	void Code();
};
void Computer::Code()
{
	cout << "#include..." << endl;
} 

class Person
{
public:
	void Coding(Computer &com);
};

void Person::Coding(Computer& com)
{
	cout << "打代码" << endl;
	com.Code();
}
int main()
{
	Computer com;
	Person Ps;
	Ps.Coding(com);

	return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值