组合
来源:互联网 发布:淘宝转化率低怎么办 编辑:程序博客网 时间:2024/04/29 19:44
复合类(组合类):一个类的的对象是另外一个类成员变量(属性),那么前者被称为复合类。
组合类中的构造函数调用(先调用属性对象的‘构造函数’,再初始化另外部分)
组合类中如果出现交叉调用,需要进行前向声明
例如:Candidate类中成员中包含Voter,Voter类中又需要调用Candidate对象,则这两个类中的声明都需要加头文件包含和前向声明
Voter
*#ifndef Voter_hpp**#define Voter_hpp**#include"Person.hpp"**#include"PersonSet.hpp"**#include "Candidate.hpp"**#include <stdio.h>**#include<iostream>*using namespace std;class Candidate;class Voter:public Person{private: int _polingStation; static int _totalNumVoters;public: Voter(char *name,int age,int salary,int polingStation); void SetPolingStation(int PolingStation); void print(); friend bool operator==(const Voter& v1,const Voter& v2); static int Voters() { return _totalNumVoters; } ***//使用Candidate类 Person& SelectCandidate(PersonSet& candidate); void Vote(Candidate& aCandidate);***};*#endif /* Voter_hpp */*
Candidate
#ifndef Candidate_hpp#define Candidate_hpp#include"Person.hpp"#include"PersonSet.hpp"#include "Voter.hpp"#include <stdio.h>#include<iostream>using namespace std;class Voter;class Candidate:public Person{private://Voter类作为Candidate类的成员变量 Voter ** voter; //存放每个候选人的投票者 static int _numCandidates; int voter_size; //实际投票者人数 int _capacity; //容量public: Candidate(char *name,int age,int salary); int GetVotesNum(); void AddVoter(Voter& avoter); friend ostream& operator<<(ostream& out,const Candidate& c); int GetAverageVotersAge(); int GetAverageVotersSalary(); friend bool operator<(const Candidate& c1,const Candidate& c2); ~Candidate(); static int Candidates() { return _numCandidates; } void Reset() { voter_size = 0; }};#endif /* Candidate_hpp */
0 0
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- 组合
- Android MTK 设置开机启动自定义Launcher Home键换出Launcher切换
- iOS 生成.a静态库
- form表单那点事儿(下) 进阶篇
- Ireport导包位置
- 利用Geocoder来进行为位置转换经纬度
- 组合
- redis C语言API简单函数使用介绍
- Oci8Pdo Yii 1.1 expansion and bandage of the CLOB/BLOB parameters
- SQL模糊查询
- jquery相关语法实例介绍
- Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类
- 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3)
- Swift中使用NSLog的问题
- android studio 使用git添加远程库(这里用Github)