高分求助 模版类问题 编写一个带溢出检测的数组模版
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#include "course.h"
#include "safearray.h"
void init_courses(safearray<course>);
void display_menu(safearray<course>);
const int NUM_COURSES = 10;
const int QUIT = 99;
int main(int argc, char* argv[])
{
safearray<course> courses(NUM_COURSES);
init_courses(courses);
int choice = 0;
while (choice != QUIT)
{
display_menu(courses);
cout << "Enter number of course to see more information on\n";
cin >> choice;
cout << "\n";
if (choice >= 1 && choice <= NUM_COURSES)
{
cout << courses[choice - 1] << "\n\n";
}
cout<<"a"<<endl;
}
return EXIT_SUCCESS;
}
void display_menu(safearray<course> courses)
{
for (int i = 1; i <= NUM_COURSES; i++)
{
cout << i << ". " << courses[i - 1].name << "\n";
}
cout << "99. Quit\n";
}
void init_courses(safearray<course> courses)
{
ifstream inf("courses.txt");
if (! inf)
{
cerr << "Could not open courses.txt" << endl;
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUM_COURSES; i++)
{
inf >> courses[i];
}
inf.close();
}
//safearray.h
#ifndef SAFEARRAY_H
#define SAFEARRAY_H
template <class T>
class safearray {
private:
T *storage;
int capacity;
public:
safearray() : storage(NULL), capacity(0) {} // default constructor
safearray(int size)
{
storage= new T[size];
capacity=size;
}
~safearray(void){delete []storage ;}
T& operator[] (int i) throw(out_of_range)
{
if(i<0)
throw(out_of_range)("err: input < 0");
else if(i>capacity)
throw(out_of_range)("err: input > max");
else
return *(storage+i);
}
};
#endif
//course.h
#ifndef COURSE_H
#define COURSE_H
int const MAX_LINES = 10;
class course {
public:
string name;
string title;
string description[MAX_LINES];
course() : name(""), title("") {}
course(string name, string title) :
name(name), title(title) {}
friend ostream& operator<<(ostream&, const course&);
friend istream& operator>>(istream&, course&);
};
ostream& operator<<(ostream& out, const course& c) {
out << c.name << ": " << c.title << "\n";
int index = 0;
while (c.description[index] != "") {
out << c.description[index++] << "\n";
}
return out;
}
istream& operator>>(istream& in, course& c) {
getline(in, c.name);
getline(in, c.title);
string line;
getline(in, line);
int number = 0;
while (line != "") {
c.description[number++] = line;
getline(in, line);
}
return in;
}
#endif
在同一个文件夹下有这个文件
//courses.txt
SSD1
Introduction to Information Systems
This course introduces students to fundamental concepts
and skills of software development. Students learn how
software is written, what its fundamental building blocks
are, and how multiple pieces of software cooperate to make
the Web work. Students learn to create web pages in HTML
and applets in Java.
SSD2
Introduction to Computer Systems
This course introduces students to the fundamentals of
using and maintaining computer systems in an Internet
environment. The basic components and functions of the
computer and the network are introduced, along with tools
and procedures for their operation and maintenance.
SSD3
Object-Oriented Programming and Design
This course introduces students to problem solving by
means of object-oriented design and implementation.
Emphasis is on problem analysis and solution design,
documentation and implementation. Students use commercial
software libraries, and create software projects.
Programming assignments are carried out in Java.
SSD4
User-Centered Design and Testing
This course focuses on human-computer interaction teaching
basic skills in designing, creating, and evaluating user
interfaces. Students learn how to design usable,
human-friendly user interfaces using a rapid-prototyping
programming language. They also learn how to evaluate
interfaces empirically with two usability tools. Visual
Basic is used in programming assignments.
SSD5
Data Structures and Algorithms
This course focuses on understanding the dependence of
execution time, bandwidth and memory requirements on
the data structures and algorithms chosen. Students
learn to reason informally about algorithm and data
structure correctness and complexity. Primary emphasis
is given to intelligent selection of algorithms and
representations. Programming assignments use C++ and the
Standard Template Library.
SSD6
System-Level Programming
This course provides students with a programmer's view
of processors, memory, and operating systems. Students
learn explicitly about memory organization and hierarchies,
about context-switching and threads, and about the
transformations that a high-level program undergoes before
it is executed on actual hardware. These enable students
to eliminate obscure "bugs" and to measure and optimize
program performance. Programming assignments use the C
programming language.
SSD7
Database Systems
This course introduces students to database concepts
including database design. Relational data models are
emphasized. Students develop client-server applications
in Java and/or Visual Basic, using commercial database
management systems. Example applications include e-commerce
systems.
SSD8
Networks and Distributed Computing
This course focuses on principles and practices of
network-based computing. It begins with an overview of
networking technology in support of data and multimedia
communication. It continues with application-oriented
protocols and approaches to distributed object-oriented
programming using Java.
SSD9
Software Specification, Testing, and Maintenance
This course focuses on the principles of development of
software systems following software engineering practices.
Students work individually or in multi-person teams on
Internet-based software projects of significant scale,
acquiring experience in all phases of the software product
lifecycle.
SSD10
Software Project Organization and Management
This course focuses on the organizational and management
aspects of software projects. Students learn techniques of
project planning, scheduling, costing and organization, from
an iterative perspective, and examine and critique various kinds
of planning and management artifacts. A key emphasis is management
of client issues.