📄 gradebook.cpp
字号:
// Tutorial 21: GradeBook.cpp
// GradeBook member function templates definitions.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required for parameterized stream manipulation
#include "GradeBook.h" // include GradeBook class definition
using namespace std; // for accessing C++ Standard Library members
// one-argument constructor
template< class T >
GradeBook< T >::GradeBook( int size )
{
// set the number of students in the class
setClassSize( size );
// dynamically allocate memory for the grades array
grades = new T[ classSize ];
// initialize each value in grades to 0
for ( int i = 0; i < classSize; i++ )
{
grades[ i ] = 0; // works for int or double
} // end for
} // end GradeBook one-argument template constructor
// return the class size
template< class T >
int GradeBook< T >::getClassSize()
{
return classSize;
} // end function template getClassSize
// set the class size
template< class T >
void GradeBook< T >::setClassSize( int value )
{
classSize = value;
} // end function template setClassSize
// return the grade at a specific index
template< class T >
T GradeBook< T >::getGrade( int index )
{
return grades[ index ];
} // end function template getGrade
// set specified element of grades array to specified value
template< class T >
void GradeBook< T >::setGrade( T value, int index )
{
grades[ index ] = value;
} // end function template setGrade
// display each element in the grades array
template< class T >
void GradeBook< T >::displayGrades()
{
cout << "Student # Grade" << endl;
// output each student's grade
for ( int student = 0; student < classSize; student++ )
{
cout << setw( 5 ) << student + 1 << setw( 13 )
<< grades[ student ] << endl;
} // end for
} // end function template displayGrades
// return the minimum grade in the grades array
template< class T >
T GradeBook< T >::getMinimum()
{
T lowGrade = grades[ 0 ]; // initialize lowGrade
// examine remaining elements of the grades array
for ( int i = 1; i < classSize; i++ )
{
// if grades[ i ] less than lowGrade, assign it to lowGrade
if ( grades[ i ] < lowGrade)
{
lowGrade = grades[ i ]; // update the value of lowGrade
} // end if
} // end for
return lowGrade; // return the minimum grade
} // end function template getMinimum
// return the maximum grade in the grades array
template< class T >
T GradeBook< T >::getMaximum()
{
T highGrade = grades[ 0 ]; // initialize highGrade
// examine remaining elements of the grades array
for ( int i = 1; i < classSize; i++ )
{
// if grades[ i ] greater than highGrade,
// assign it to highGrade
if ( grades[ i ] > highGrade )
{
highGrade = grades[ i ]; // update the value of highGrade
} // end if
} // end for
return highGrade; // return the maximum grade
} // end function template getMaximum
// return the average grade
template< class T >
double GradeBook< T >::getAverage()
{
double total = 0.0; // initialize sum of the grades
// sum all of the grades
for ( int i = 0; i < classSize; i++ )
{
total += grades[ i ]; // add element i to total
} // end for
return total / classSize; // return the average grade
} // end function template getAverage
// display the class average, minimum and maximum grade
template< class T >
void GradeBook< T >::displayStatistics()
{
// display the average grade
cout << "\nClass average grade: " << getAverage() << endl;
// display the minimum and maximum grades
cout << "Class minimum grade: " << getMinimum()
<< "\nClass maximum grade: " << getMaximum() << endl;
} // end function template displayStatistics
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -