C/C++代写 - CS52
时间:2020-11-22
Assignment Description
Build an integer Vector class defined in namespace CS52 that has similar behavior to std::vector. The
integer Vector will have a default constructor, an overloaded constructor, a copy constructor, and overloaded operators (e.g. bracket [], assignment = and stream insertion <<). Use C++’s object oriented
programming constructs like classes and resource management that relies on constructors and destructors
for memory management. Refer to the interface described on page 2 of this handout for the Vector
class declaration.
Include your name, your student id, the assignment number, the submission date, and a program description in comments at the top of your A04.cpp file. As in previous assignments use the CS52 Programming
Guide for coding style guidance.
Where to do the assignment
You can do this assignment on your own computer, or using the SMC Virtual labs with Citrix. Due
to COVID-19 the campus labs are closed for in person usage. Ensure the code compiles and runs on
Windows. Name your program (file) ’A04.cpp’ for full credit. Do not use any other name or else points
will be deducted.
Submitting the Assignment
Submit the assignment on Canvas (https://signon.smc.edu) by uploading your program file to the
Assignment 4 entry. Go to your Dashboard and to the CS 052 Assignments page. Upload the file
containing your program as an attachment. Do not cut-and-paste your program into a text window. Do
not hand in a screenshot of your program’s output. Do not hand in a text file containing the output of
your program. Submit the A04.cpp file that contains your code.
Saving your work
Save your work often on a flash-drive or to the cloud (e.g., GoogleDrive, Microsoft OneDrive, Canvas,
etc.). Always save a personal copy of your files (e.g. .cpp, .h, etc.).
Do your own work
Do not distribute this handout. Do not upload to chegg, coursehero, ankitcodinghub, or any other
online platform. Do not pay someone to write the code and submit their code as your solution. You
are expected to do your own work. Turning in code that is not your own work will result in a referral to
Student Judicial Affairs.
2020 ➞ Scott Bishop | Assistant Professor of Computer Science, Santa Monica College 1
Vector Interface
namespace CS52 {
class Vector {
public :
Vector (); // Default constructor
Vector ( int size , int int_val ); // Overloaded constructor
Vector ( const Vector & ); // Copy constructor
~ Vector (); // Destructor
// Returns a reference to the element at index i, throws a string if i is out -of - bounds .
int & at ( int i ) const throw ( std :: string );
// Returns the allocated storage for the Vector .
int capacity () const ;
// Erases the elements of the Vector but does not change capacity .
void clear ();
// Returns pointer to the first element in the Vector .
int * data () const ;
// If Vector is empty return true , else false .
bool empty () const ;
// Deletes the element at the end of the vector .
void pop_back ();
// Add an element to the end of the vector .
void push_back ( int element );
// Returns the number of elements in the vector .
int size () const ;
// Overloaded operators
int & operator [] ( int index ); // [] array syntax
Vector & operator =( const Vector & ); // copy assignment
// Overloaded stream insertion operator
friend std :: ostream & operator < <( std :: ostream & , Vector &);
private :
int _size = 0;
int _capacity = 0;
int * _data = nullptr ;
}; // Vector
}// namespace
Assignment 4 References:
❼ Refer to string class in Sec. 11.4 of textbook for dynamic memory & OOP resource management.
❼ MSDN STL vector docs and cplusplus.com vector docs
❼ resize docs at cppreference.com and resize docs at microsoft
❼ reserve docs at cppreference.com and reserve docs at microsoft
2020 ➞ Scott Bishop | Assistant Professor of Computer Science, Santa Monica College 2
Example driver code to test functions in the CS52::Vector interface:
void info ( CS52 :: Vector & v ){
std :: cout << " Size is: " << v . size () << "\n";
std :: cout << " Capacity is: " << v . capacity () << "\n";
std :: cout << " Contents : ";
for (int i = 0; i < v . size (); i ++) {
std :: cout << v [ i ] << " "; }
std :: cout << "\n"; }// info
int main () {
// default constructor , push_back , at methods
std :: cout << "// default constructor , push_back , at \ nCS52 :: Vector a;\n";
CS52 :: Vector a ;
std :: cout << "\na. push_back (10); a. push_back (88)\ n";
a . push_back (10); a . push_back (88);
std :: cout << "a.at (0) = 99;\ n";
a . at (0) = 99;
info ( a );
//b
std :: cout << "\n// overloaded constructor , [] op , at , empty ,\
clear , exception handling : at \ nCS52 :: Vector b (2 ,5);\ n";
CS52 :: Vector b (2 ,5);
std :: cout << "b is " << b << "\n";
std :: cout << "\n// Add more elements to b\n";
std :: cout << "b. push_back (10); b. push_back (2);\ n";
std :: cout << "b. push_back (99); b. push_back ( -5);\n";
b . push_back (10); b . push_back (2);
b . push_back (99); b . push_back ( -5);
std :: cout << "\n// array index [] and at ()\n";
std :: cout << "b[0] = 25; b[1] = 1;\n";
b [0] = 25; b [1] = 1;
std :: cout << "b.at (0) ; b.at (1) ;\n";
std :: cout << b . at (0) << " " << b . at (1) << "\n";
std :: cout << "\n\n// empty method , size , and capacity \n";
std :: cout << "b. empty () " << ( b . empty () ? " True " : " False ") << "\n";
std :: cout << "b. clear ()\n";
b . clear ();
info ( b );
std :: cout << "b. empty () " << ( b . empty () ? " True " : " False ") << "\n";
std :: cout << "\n\n// Exception handling :";
try {
std :: cout << "\nb.at (9) = ";
std :: cout << b . at (9);
}
catch ( std :: string msg ) { std :: cerr << "\n" << msg << std :: endl ; }
2020 ➞ Scott Bishop | Assistant Professor of Computer Science, Santa Monica College 3
Example driver code continued:
//c
std :: cout << "\n// copy constructor , copy assignment , pop_back ,\
\n// capacity , size \ nCS52 :: Vector c(b);\n";
std :: cout << "\n// Add more elements to b\n";
std :: cout << "b. push_back (11); b. push_back (7);\ n";
std :: cout << "b. push_back (3); b. push_back (23);\ n";
b . push_back (11); b . push_back (7);
b . push_back (3); b . push_back (23);
CS52 :: Vector c ( b );
info ( c );
//d
std :: cout << "\n// copy assignment \n";
CS52 :: Vector d ;
d = c ;
std :: cout << " CS52 :: Vector d = c; " << "\n";
std :: cout << "d is " << d << "\n";
std :: cout << "\n// size vs capacity ()\n";
std :: cout << "d. size () is " << d . size () << "\n";
std :: cout << "d. capacity () is " << d . capacity () << "\n";
std :: cout << "\n// pop_back ()\n";
std :: cout << "d. pop_back ();d. pop_back ();\ n";
d . pop_back (); d . pop_back ();
std :: cout << "\n// size vs capacity \n";
std :: cout << "d. size () is " << d . size () << "\n";
std :: cout << "d. capacity () is " << d . capacity () << "\n";
char stop ; std :: cin >> stop ; return 0;} // main
2020 ➞ Scott Bishop | Assistant Professor of Computer Science, Santa Monica College 4
Example test output from code in main above that uses our Vector class:
//default constructor, push_back, at
CS52::Vector a;
a.push_back(10); a.push_back(88)
a.at(0) = 99;
Size is: 2
Capacity is: 2
Contents : 99 88
//overloaded constructor, [] op, at, empty, clear, exception handling: at
CS52::Vector b(2,5);
b is 5 5
//Add more elements to b
b.push_back(10); b.push_back(2);
b.push_back(99); b.push_back(-5);
//array index [] and at()
b[0] = 25; b[1] = 1;
b.at(0) ; b.at(1) ;
25 1
//empty method, size, and capacity
b.empty() False
b.clear()
Size is: 0
Capacity is: 8
Contents :
b.empty() True
//Exception handling:
b.at(9) =
out-of-bounds
//copy constructor, copy assignment,
//pop_back, capacity, size
CS52::Vector c(b);
//Add more elements to b
b.push_back(11); b.push_back(7);
b.push_back(3); b.push_back(23);
Size is: 4
Capacity is: 8
Contents : 11 7 3 23
2020 ➞ Scott Bishop | Assistant Professor of Computer Science, Santa Monica College 5
Example test output continued:
//copy assignment
CS52::Vector d = c;
d is 11 7 3 23
//size vs capacity()
d.size() is 4
d.capacity() is 8
//pop_back()
d.pop_back();d.pop_back();
//size vs capacity
d.size() is 2
d.capacity() is 8
}