实验目的: 1. 练习多文件结构、预编译指令 2. 练习指针、动态内存 |
实验软件和硬件环境: CPU:Intel core i5 7300-HQ 操作系统:Windows 10 专业版 IDE:Dev C++ 5.11 VS2017 编译器:TDM-GCC 4.9.2 MSVC
|
实验步骤与内容: (代码请见文末附录) 1.多文件组成的项目练习(二): 利用实验6素材.zip的文件,练习类声明、成员函数、主调程序分离。 (1)建立一个空应用(Empty Project)项目文件(名称自定)。在项目中添加实验6素材中的三个文件。 DEV-CPP环境下操作:在左侧项目管理窗口,右击项目名称选“添加”,添加如下三个文件: student.h student.cpp main.cpp
编译该项目。
(2)练习删除源码文件、只连接目标文件。 DEV-CPP环境下操作: 删除项目文件夹下的源码文件student.cpp,只留目标文件student.o。 在项目文件中删除student.cpp名,只包含如下两个文件: student.h main.cpp 在dev-cpp中菜单“项目”/“项目属性”/"参数"-"链接"下"加入库或者对象",将student.o文件加入,再编译运行该项目。 成功运行,由此看来,编译器先将每个cpp文件都编译成o文件,然后进行链接cpp文件丢失,只要o文件还在,就可以继续链接执行。
为什么一个项目多个文件中都有如下两句,而不担心重复? #include <iostream> using namespace std;
首先系统的头文件都会只用预处理命令来避免重复引用;使用using namespace std时,std命名空间中的各种元素都暴露在全局中,再次使用using namespace std也不会产生什么后继的作用。
但是不要在庞大工程头文件中使用using namespace std,这会造成空间混乱
2. 练习第六章PPT,P48-57例 GradeBook类,用面向对象思想使用数组。 将数组封装在对象中,加强了数组的安全性,也简化了代码
new出来的内存空间不会被自动释放,析构函数也不会自动释放其空间,所以凡是new的,无论如何都要进行delete释放,该例程并未释放所申请的空间。
|
结论分析与体会:
|
就实验过程中遇到的问题及解决处理方法,自拟1-3道问答题:
未遇到明显问题。 |
附录 源码:
// TestMain.cpp
// Creates GradeBook object using a two-dimensional array of grades.
#include "GradeBook.h" // GradeBook class definition
// function main begins program execution
int main()
{
// two-dimensional array of student grades
int gradesArray[GradeBook::students][GradeBook::tests] =
{ { 87, 96, 70 },{ 68, 87, 90 },
{ 94, 100, 90 },{ 100, 81, 82 },
{ 83, 65, 85 },{ 78, 87, 65 },
{ 85, 75, 83 },{ 91, 94, 100 },
{ 76, 72, 84 },{ 87, 93, 73 } };
GradeBook myGradeBook(
"C++ Programming Practice", gradesArray);
myGradeBook.displayMessage();
myGradeBook.processGrades();
return 0; // indicates successful termination
} // end main
//Definiton of class Gradebook that uses a two-dimesion array to store test grades
#include <string> //program uses C++ Standard Library string class
using namespace std;
#ifndef GRADEBOOK_H
#define GRADEBOOK_H
//GradeBook class definition
class GradeBook {
public:
//constants
static const int students = 10; //number of students
static const int tests = 3; // number of tests
//constructor inititalizes cousre name and array of grades
GradeBook(string, const int[][tests]);
void setCourseName(string); // function to set the course name
string getCourseName(); // function to retrieve the course name
void displayMessage(); // display a welcome message
void processGrades(); // perform various operations on the grade data
int getMinimum(); //find the minimum grade in the grade book
int getMaximum(); //find the maximum grade in the grade book
double getAverage(const int[], const int); // get student's average
void outputBarChart(); // output bar char of grade distriudbution
void outputGrades(); // output the constanets of the grades array
private:
string courseName; // course name for this grade book
int grades[students][tests]; // two-dimensional array of grades
}; // end class
#endif //
// Member-function definitions for class GradeBook that
// uses a two-dimensional array to store grades.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip> // parameterized stream manipulators
using std::setprecision; // sets numeric output precision
using std::setw; // sets field width
// include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"
// two-argument constructor initializes courseName and grades array
GradeBook::GradeBook(string name, const int gradesArray[][tests])
{
setCourseName(name); // initialize courseName
// copy grades from gradeArray to grades
for (int student = 0; student < students; student++)
for (int test = 0; test < tests; test++)
grades[student][test] = gradesArray[student][test];
} // end two-argument GradeBook constructor
// function to set the course name
void GradeBook::setCourseName(string name)
{
courseName = name; // store the course name
} // end function setCourseName
// function to retrieve the course name
string GradeBook::getCourseName()
{
return courseName;
} // end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// this statement calls getCourseName to get the name of the course this GradeBook represents
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
} // end function displayMessage
// perform various operations on the data
void GradeBook::processGrades()
{
// output grades array
outputGrades();
// call functions getMinimum and getMaximum
cout << "\nLowest grade in the grade book is " << getMinimum()
<< "\nHighest grade in the grade book is " << getMaximum() << endl;
outputBarChart();// output grade distribution chart of all grades on all tests outputBarChart();
} // end function processGrades
// find minimum grade
int GradeBook::getMinimum()
{
int lowGrade = 100; // assume lowest grade is 100
for (int student = 0; student < students; student++) // loop through rows of grades array
{
for (int test = 0; test < tests; test++) // loop through columns of current row
{
if (grades[student][test] < lowGrade) // if current grade less than lowGrade, assign it to lowGrade
lowGrade = grades[student][test]; // new lowest grade
} // end inner for
} // end outer for
return lowGrade; // return lowest grade
} // end function getMinimum
// find maximum grade
int GradeBook::getMaximum()
{
int highGrade = 0; // assume highest grade is 0
// loop through rows of grades array
for (int student = 0; student < students; student++)
{
// loop through columns of current row
for (int test = 0; test < tests; test++)
{
// if current grade greater than lowGrade, assign it to highGrade
if (grades[student][test] > highGrade)
highGrade = grades[student][test]; // new highest grade
} // end inner for
} // end outer for
return highGrade; // return highest grade
} // end function getMaximum
// determine average grade for particular set of grades
double GradeBook::getAverage(const int setOfGrades[], const int grades)
{
int total = 0; // initialize total
// sum grades in array
for (int grade = 0; grade < grades; grade++)
total += setOfGrades[grade];
return static_cast<double>(total) / grades; // return average of grades
} // end function getAverage
// output bar chart displaying grade distribution
void GradeBook::outputBarChart()
{
cout << "\nOverall grade distribution:" << endl;
// stores frequency of grades in each range of 10 grades
const int frequencySize = 11;
int frequency[frequencySize] = { 0 };
// for each grade, increment the appropriate frequency
for (int student = 0; student < students; student++)
for (int test = 0; test < tests; test++)
++frequency[grades[student][test] / 10];
// for each grade frequency, print bar in chart
for (int count = 0; count < frequencySize; count++)
{
// output bar label ("0-9:", ..., "90-99:", "100:" )
if (count == 0)
cout << " 0-9: ";
else if (count == 10)
cout << " 100: ";
else
cout << count * 10 << "-" << (count * 10) + 9 << ": ";
// print bar of asterisks
for (int stars = 0; stars < frequency[count]; stars++)
cout << '*';
cout << endl; // start a new line of output
} // end outer for
} // end function outputBarChart
// output the contents of the grades array
void GradeBook::outputGrades()
{
cout << "\nThe grades are:\n\n";
cout << " "; // align column heads
// create a column heading for each of the tests
for (int test = 0; test < tests; test++)
cout << "Test " << test + 1 << " ";
cout << "Average" << endl; // student average column heading
// create rows/columns of text representing array grades
for (int student = 0; student < students; student++)
{
cout << "Student " << setw(2) << student + 1;
// output student's grades
for (int test = 0; test < tests; test++)
cout << setw(8) << grades[student][test];
// call member function getAverage to calculate student's average;
// pass row of grades and the value of tests as the arguments
double average = getAverage(grades[student], tests);
cout << setw(9) << setprecision(2) << fixed << average << endl;
} // end outer for
} // end function outputGrades