C++面向对象的程序设计11——项目练习:相亲项目(使用继承优化)

因为男孩类和女孩类在代码中有一部分数据成员和成员函数是重复的,为了避免重复使用代码,可以使用类的继承将重复代码写入基类中。这里定义了一个基类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;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值