4135人加入学习
(13人评价)
C++编程系列 第二季函数和类

制作于2018.4.2

价格 免费

分离式编译

随着程序越来越复杂,我们希望把程序的各个部分分别存储在不同的文件中。

我们可以将原来的程序分成三个部分:

1.头文件:包含结构声明和使用这些结构的函数的原型。

2.源代码文件:包含于结构有关的函数的代码。

3.源代码文件:包含调用与结构相关的函数的代码。

 

2 :函数定义

3:使用

 

分离式编译

头文件中常包含的内容:

1.函数原型

2.使用#define或const定义的符号常量

3.结构声明

4.类声明

5.模板声明

6.内联函数

 

//game.h

#pragma once

#ifndef GAME_H_
#define GAME_H_

#include <string>
using namespace std;

struct Game
{
	string gameName;
	float gameScore;
};

void inputGame(Game games[],const int size);
void sort(Game games[],const  int size);
void display(const Game games[],const int size);

const int Size = 5;

#endif // !GAME_H_

//game.cpp

#include "game.h"
#include <iostream>

void inputGame(Game games[], const int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "请输入喜爱的游戏的名称:";
		cin >> games[i].gameName;
		cout << "请输入游戏评分(10以内的小数):";
		cin >> games[i].gameScore;
	}
}

void sort(Game games[], const  int size)
{
	Game temp;
	for (int i = 0; i < size - 1; i++)
	{
		for (int j = i + 1; j < size; j++)
		{
			if (games[i].gameScore < games[j].gameScore)
			{
				temp = games[i];
				games[i] = games[j];
				games[j] = temp;
			}
		}
	}
}

void display(const Game games[], const int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << i + 1 << ":" << games[i].gameName << "(" << games[i].gameScore << ")" << endl;
	}
}

//17分离式编译.cpp

#include "game.h"
#include <iostream>

int main()
{
	cout << "请输入5个你喜爱的游戏的名称,并给它们评分:" << endl;
	Game games[Size] = {};
	inputGame(games, Size);
	sort(games, Size);
	display(games, Size);
	return 0;
}

 

习题4

完成程序:药品物资管理

要求:

1.利用结构体来存储目前拥有的药物的名称、种类、数量、买入价格、卖出价格。

2.利用枚举来设置药物的种类(回复mp和回复hp)。

3.编写函数来控制药物的买入和卖出,卖出价为买入价格的3/4。

4.编写函数来显示拥有的药物的剩余钱数。

5.通过输入数字来控制函数调用。

6.实现分离式编译。

 

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(43)