=========================preview======================
(COMP2012)[2013](s)midterm~=v0vd57a^_99090.pdf
Back to COMP2012 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY
Department of Computer Science & Engineering
COMP 2012: Object-Oriented Programming and Data Structures
Spring 2013
Midterm Examination
Date: Saturday, 13 April 2013
Time: 10:30pm C 12:00pm
Venue: LTD (L1), LTC (L2) and LTJ (L3)
.
This is a closed-book examination. However, you are allowed to bring with you a piece of A4-sized paper with notes written, drawn or typed on both sides for use in the exam-ination.
.
Your answers will be graded according to correctness, ef.ciency, precision, and clarity.
.
During the examination, you must put aside your calculators, mobile phones, PDAs and all other electronic devices. All mobile phones must be turned off.
.
You may use the reverse side of the pages for your rough work or continuation of work. This booklet has 12 single-sided pages. Please check that all pages are properly printed before you start working.
Student name: English nickname (if any):
Student ID: Email: Lecture:
I have not violated the Academic Honor Code in this examination (signature):
Question Your score Maximum score Question Your score Maximum score
1 10 6 10
2 10 7 10
3 8 8 10
4 12 9 10
5 10 10 10
Total 100
A Vector Class
(100 points total)
In mathematics classes, you may have learnt vector. In this problem, you are to develop a Vector class holding double type supporting I/O and operator overloading. A vector with s 1 components is an ordered collection of s doubles given by
v =(v0,v1,...,vs.1).
s is also called the size or dimension of the vector. Note that we index the components from 0 onwards. We de.ne the case s =0 a null or empty vector.
You are to implement a Vector class with a dynamic array double *data which holds the components, and a .eld int size which is the dimension of the vector.
We show below the C++ header .le Vector.h, which de.nes the class Vector as follows:
#include <iostream> #include <sstream> #include <fstream> using namespace std;
class Vector { public:
Vector(int s=0, double v=0); //Constructor
Vector(const Vector& other_vector);
.Vector();
const Vector& operator = (const Vector&); //assignment
double& operator [] (int);
const double& operator [] (int) const;
Vector operator+(const Vector&) const; //overloading of add
void set_size(int d){ (d<0)? exit(-1): size=d; } //set the size of a vector int get_size() const{ return size; } //get the size of a vector
void read_vector(const char* fileName); //read vector from a file void write_vector(const char* fileName) const; //write vector to a file
private:
int size; //dimensions of the vector
double *data; // pointer to vector components
// utility/helper functions
void display(ostream& out) const; //display a vector to out
void readin(istream& in); //read a vector from in
};
Given the above Vector.h, you are to write Vector.cpp so as to separate implementation detai