{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04_classes_II - CSCI-1200 Data Structures Fall 2010 Lecture...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 4 — Classes II: Sort, Non-member Operators Review from Lecture 3 C++ classes, member variables and member functions, class scope, public and private Nuances to remember Within class scope (within the code of a member function) member variables and member functions of that class may be accessed without providing the name of the class object. Within a member function, when an object of the same class type has been passed as an argument, direct access to the private member variables of that object is allowed (using the ’.’ notation). Classes vs. structs Designing classes Today’s Lecture Extended example of student grading program Passing comparison functions to sort Non-member operators 4.1 Example: Student Grades Our goal is to write a program that calculates the grades for students in a class and outputs the students and their averages in alphabetical order. The program source code is broken into three parts: Re-use of statistics code that we wrote in Lecture 2. Class Student to record information about each student, including name and grades, and to calculate averages. The main function controls the overall flow, including input, calculation of grades, and output. // File: main_student.cpp // Purpose: Compute student averages and output them alphabetically. #include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <vector> #include "student.h" unsigned int max(unsigned int x, unsigned int y) { return x>y ? x : y; } int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage:\n " << argv[0] << " infile-students outfile-grades\n"; return 1; } std::ifstream in_str(argv[1]); if (!in_str) { std::cerr << "Could not open " << argv[1] << " to read\n"; return 1; } std::ofstream out_str(argv[2]); if (!out_str) { std::cerr << "Could not open " << argv[2] << " to write\n"; return 1; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
int num_homeworks, num_tests; double hw_weight; in_str >> num_homeworks >> num_tests >> hw_weight; std::vector<Student> students; Student one_student; // Read the students, one at a time. while(one_student.read(in_str, num_homeworks, num_tests)) { students.push_back(one_student); } // Compute the averages. At the same time, determine the maximum name length. unsigned int i; unsigned int max_length = 0; for (i=0; i<students.size(); ++i) { students[i].compute_averages(hw_weight); max_length = max(max_length, students[i].first_name().size() + students[i].last_name().size()); } max_length += 2; // account for the output padding with ", " // Sort the students alphabetically by name. std::sort(students.begin(), students.end(), less_names); // Output a header... out_str << "\nHere are the student semester averages\n"; const std::string header = "Name" + std::string(max_length-4, ’ ’) + " HW Test Final"; const std::string underline(header.size(), ’-’); out_str << header << ’\n’ << underline << std::endl; // Output the students... for (i=0; i<students.size(); ++i) { unsigned int length = students[i].last_name().size() + students[i].first_name().size() + 2; students[i].output_name(out_str); out_str << std::string(max_length - length, ’ ’) << " "; students[i].output_averages(out_str); } return 0; // everything fine } 4.2 Declaration of Class Student Stores names, id numbers, scores and averages. The scores are stored using a vector! Member variables of a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}