项目十的男生类和女生类中有很多共同的数据成员和成员方法,可以优化为继承的方式
代码实现:
1. single父类
抽出了成员方法:getAge()和getName()
抽出了数据成员:age和name
头文件
#pragma once
#include <string>
using namespace std;
class Single
{
public:
Single();
Single(int age, const string &name);
int getAge() const;
string getName() const;
private:
int age;
string name;
};
实现方法
#include "Single.h"
Single::Single()
{
age = 0;
name = "无名";
}
Single::Single(int age, const string & name)
{
this->age = age;
this->name = name;
}
int Single::getAge() const
{
return age;
}
string Single::getName() const
{
return name;
}
2. Boy类
头文件
#pragma once
#include <vector>
#include "Single.h"
class Girl;
class Boy : public Single
{
public:
Boy();
Boy(int age, string name, int salary);
// int getAge() const;
// string getName() const;
int getSalry() const;
bool satisfied(const Girl& girl) const;
string Description() const;
static void inputBoys(vector <Boy> &boys); //输入多个男孩的信息
private:
// int age;
// string name;
int salary;
};
实现方法
#include "Boy.h"
#include "Girl.h"
#include <sstream>
#include <iostream>
#define SALARY_RATIO 0.003 //薪资系数
Boy::Boy() {
// age = 0;
// name = "";
salary = 0;
}
Boy::Boy(int age, string name, int salary):Single(age,name) {
// this->age = age;
// this->name = name;
this->salary = salary;
}
/*
int Boy::getAge() const {
return age;
}
string Boy::getName() const {
return name;
}
*/
int Boy::getSalry() const {
return salary;
}
bool Boy::satisfied(const Girl& girl) const {
int yanzhi = salary * SALARY_RATIO;
if (yanzhi > 100) {
yanzhi = 100;
}
//如果颜值高于薪资乘颜值系数,表示满意
if (girl.getYanZhi() >= yanzhi) {
return true;
}
else {
return false;
}
}
string Boy::Description() const {
stringstream ret;
ret << getName() << "的年龄:" << getAge() << " 薪资:" << salary;
return ret.str();
}
void Boy::inputBoys(vector <Boy> &boys) {
int age;
string name;
int salary;
int n = 1; //男生的个数
while (1) {
cout << "请输入第" << n << "个男生的年龄【输入0结束输入】:";
cin >> age;
if (age == 0) {
break;
}
cout << "请输入第" << n << "个男生的姓名:";
cin >> name;
cout << "请输入第" << n << "个男生的薪资:";
cin >> salary;
boys.push_back(Boy(age, name, salary));
n++;
}
}
3. Girl类
头文件
#pragma once
#include <vector>
#include "Single.h"
class Boy;
class Girl : public Single
{
public:
Girl();
Girl(int age, string name, int yanZhi);
// int getAge() const;
// string getName() const;
int getYanZhi() const;
string Description() const;
bool satisfied(const Boy &boy) const;
static void iputGirls(vector<Girl> &girls);
private:
// int age;
// string name;
int yanZhi; //颜值
};
实现方法
#include "Girl.h"
#include "Boy.h"
#include <sstream>
#include <iostream>
#define YANZHI_RATIO 300 //颜值系数
Girl::Girl() {
// age = 0;
// name = "";
yanZhi = 0;
}
Girl::Girl(int age, string name, int yanZhi):Single(age,name) {
// this->age = age;
// this->name = name;
this->yanZhi = yanZhi;
}
/*
int Girl::getAge() const {
return age;
}
string Girl::getName() const {
return name;
}
*/
int Girl::getYanZhi() const {
return yanZhi;
}
bool Girl::satisfied(const Boy &boy) const {
//如果男生的薪资大于自己的颜值*颜值系数,表示满意
if (boy.getSalry() >= yanZhi * YANZHI_RATIO) {
return true;
}
else
{
return false;
}
}
string Girl::Description() const {
stringstream ret;
ret << getName() << "的年龄:" << getAge() << " 颜值:" << yanZhi;
return ret.str();
}
void Girl::iputGirls(vector<Girl> &girls) {
int age;
string name;
int yanZhi;
int n = 1;
while (1)
{
cout << "请输入第" << n << "个女生的年龄【输入0结束输入】:";
cin >> age;
if (age == 0) {
break;
}
cout << "请输入第" << n << "个女生的姓名:";
cin >> name;
cout << "请输入第" << n << "个女生的颜值:";
cin >> yanZhi;
girls.push_back(Girl(age, name, yanZhi));
n++;
}
}
4. 调用
#include "Girl.h"
#include "Boy.h"
#include <vector>
#include <iostream>
//自动匹配
void autoMatch(const vector<Boy> &boys, const vector<Girl> &girls) {
for (int i = 0; i < boys.size(); i++) {
for (int j = 0; j < girls.size(); j++) {
if (boys[i].satisfied(girls[j]) && girls[j].satisfied(boys[i])) {
cout << "(" << boys[i].getName() << ")匹配的女生是(" << girls[j].getName() << ")" << endl;
}
}
}
}
int main() {
vector<Boy> boys;
vector<Girl> girls;
Boy::inputBoys(boys);
Girl::iputGirls(girls);
cout << "\n。。。。匹配后。。。。\n";
autoMatch(boys, girls);
return 0;
}