因为男孩类和女孩类在代码中有一部分数据成员和成员函数是重复的,为了避免重复使用代码,可以使用类的继承将重复代码写入基类中。这里定义了一个基类Human类,然后定义了Boy类和Girl类继承Human类,然后将Boy类和Girl类重复的数据成员和成员函数写入Human类中。
实现代码如下:
// Human.h
#pragma once
#include <string>
using namespace std;
class Human
{
public:
Human(string, int);
string getName() const;
int getAge() const;
~Human();
private:
string name;
int age;
};
// Human.cpp
#include "Human.h"
Human::Human(string name, int age)
{
this->name = name;
this->age = age;
}
string Human::getName() const
{
return this->name;
}
int Human::getAge() const
{
return this->age;
}
Human::~Human(void)
{
}
// Boy.h
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "Human.h"
using namespace std;
class Girl;
class Boy: public Human
{
public:
Boy(string, int, int);
int getSalary() const;
bool satisfied(const Girl &) const;
string description() const;
static void inputBoys(vector <Boy> &boys);
~Boy();
private:
int salary;
};
// Boy.cpp
#include "Boy.h"
#include "Girl.h"
#include <sstream>
#define SALARY_FACTOR 0.006
Boy::Boy(string name, int age, int salary): Human(name, age)
{
this->salary = salary;
}
int Boy::getSalary() const
{
return this->salary;
}
bool Boy::satisfied(const Girl &girl) const
{
// cout << this->salary * SALARY_FACTOR << endl;
double salaryRate = this->salary * SALARY_FACTOR;
if(this->salary * SALARY_FACTOR >= 100){
salaryRate = 100;
}
if(girl.getYanZhi() >= salaryRate){
return true;
}else{
return false;
}
}
string Boy::description() const
{
stringstream ret;
ret << "姓名:" << this->getName() << " 性别:男 年龄:" << this->getAge() << " 薪资:" << this->salary;
return ret.str();
}
void Boy::inputBoys(vector <Boy> &boys)
{
int n = 1;
while(1){
int age, salary;
string name;
cout << "请输入第" << n << "个小哥哥的年龄【输入0结束】:";
cin >> age;
if(age == 0) break;
cout << "请输入第" << n << "个小哥哥的姓名:";
cin >> name;
cout << "请输入第" << n << "个小哥哥的薪资:";
cin >> salary;
boys.push_back(Boy(name, age, salary));
n++;
}
}
Boy::~Boy(void)
{
}
// Girl.h
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "Human.h"
using namespace std;
class Boy;
class Girl: public Human
{
public:
Girl(string, int, int);
int getYanZhi() const;
bool satisfied(const Boy &) const;
string description() const;
static void inputGirls(vector <Girl> &girls);
~Girl();
private:
int yanZhi;
};
// Girl.cpp
#include "Girl.h"
#include "Boy.h"
#include <sstream>
#define YANZHI_FACTOR 100
Girl::Girl(string name, int age, int yanZhi):Human(name, age)
{
this->yanZhi = yanZhi;
}
int Girl::getYanZhi() const
{
return this->yanZhi;
}
bool Girl::satisfied(const Boy &boy)const
{
if(boy.getSalary() >= this->yanZhi * YANZHI_FACTOR){
return true;
}else{
return false;
}
}
string Girl::description() const
{
stringstream ret;
ret << "姓名:" << this->getName() << " 性别:女 年龄:" << this->getAge() << " 颜值:" << this->yanZhi;
return ret.str();
}
void Girl::inputGirls(vector <Girl> &girls)
{
int n = 1;
while(1){
int age, yanZhi;
string name;
cout << "请输入第" << n << "个小姐姐的年龄【输入0结束】:";
cin >> age;
if(age == 0) break;
cout << "请输入第" << n << "个小姐姐的姓名:";
cin >> name;
cout << "请输入第" << n << "个小姐姐的颜值:";
cin >> yanZhi;
girls.push_back(Girl(name, age, yanZhi));
n++;
}
}
Girl::~Girl(void)
{
}
main.cpp
#include "Boy.h"
#include "Girl.h"
void test() // 自由匹配
{
vector <Girl> girls;
vector <Boy> boys;
Boy::inputBoys(boys);
Girl::inputGirls(girls);
for(int i = 0; i < girls.size(); i++){
for(int j = 0; j < boys.size(); j++){
if(girls[i].satisfied(boys[j]) && boys[j].satisfied(girls[i])){
cout << boys[j].description() << "<==>" << girls[i].description() << endl;
}
}
}
}
int main()
{
test();
system("pause");
return 0;
}
运行结果: