$30
Object-Oriented Programming
Homework Assignment #5
Instructions
1. If any question is unclear, please ask for a clarification.
2. Note that all the line numbers in the source listings are for references only.
3. You are required to do all the homework assignments on Linux and use g++ version 10.2.0 or later.
4. You are required to give your TA a demo of your program. Make sure that your program can compile and
run on the server machine, which will be used for the demo.
5. For the program that you write, you are required to include a Makefile. Otherwise, the grade for your
program will be zero.
6. Unless stated otherwise, you are required to work on the homework assignment individually.
7. No late homework will be accepted.
Programming Project
Part I (50%)
This assignment requires that you complete the implementation of the user-defined manipulators as given in
Section 21.4.6.3 of the book entitled The C++ Programming Language, Special Edition by Bjarne Stroustrup.
The test program and the output of the test program are as given in Listing 1 and Listing 2. In order to make it
easier for you to integrate your program with the test program, as given in Listing 1, you are required to name
your program Form.h.
Listing 1: The test program.
1 #include <iostream>
2 #include "Form.h"
3
4 using std::cout;
5 using std::endl;
6
1
7
8
9 Form gen4(4); // general format, precision is 4
10
11 void f(double d)
12 {
13 Form sci8 = gen4;
14 sci8.scientific().precision(8); // scientific format, precision 8
15 Form fix8 = gen4;
16 fix8.fixed().precision(8); // fixed format, precision 8
17 cout << "default = " << d << endl
18 << "gen4 = " << gen4(d) << endl
19 << "sci8 = " << sci8(d) << endl
20 << "fix8 = " << fix8(d) << endl
21 << "default = " << d << endl;
22 }
23
24
25
26 int main()
27 {
28 f(1234.56789);
29 f(12.3456789);
30
31 return 0;
32 }
Listing 2: The output of the test program.
1 default = 1234.57
2 gen4 = 1235
3 sci8 = 1.23456789e+03
4 fix8 = 1234.56789000
5 default = 1234.57
6 default = 12.3457
7 gen4 = 12.35
8 sci8 = 1.23456789e+01
9 fix8 = 12.34567890
10 default = 12.3457
Part II (50%)
This assignment requires that you complete the implementation of the class template hierarchy as given in Section
13.5 of the book entitled The C++ Programming Language, Special Edition by Bjarne Stroustrup.
The test program and the output of the test program are as given in Listing 3 and Listing 4. In order to make it
easier for you to integrate your program with the test program, as given in Listing 3, you are required to name
your program Vector.h.
Listing 3: The test program.
1 #include "Vector.h"
2 #include "Trace.h"
3
4 #include <iostream>
5
6 using std::cout;
7 using std::endl;
2
8
9
10
11 vector<float> fvec(100);
12 vector<int> ivec(200);
13 vector<char> cvec(300);
14
15 vector<float*> pfvec(100);
16 vector<int*> pivec(200);
17 vector<char*> pcvec(300);
18
19 int main()
20 {
21 TRACE(dummy, "main()");
22
23 fvec[3] = 10.10;
24 cout << " fvec[3] = " << fvec[3] << endl;
25
26 ivec[3] = 10;
27 cout << " ivec[3] = " << ivec[3] << endl;
28
29 cvec[3] = ’A’;
30 cout << " cvec[3] = " << cvec[3] << endl;
31
32 float f = 10.10;
33 pfvec.elem(3) = &f;
34 pfvec[3] = &f;
35 cout << " pfvec.elem(3) = " << *pfvec.elem(3) << endl;
36 cout << " pfvec[3] = " << *pfvec[3] << endl;
37
38 int a = 10;
39 pivec.elem(3) = &a;
40 pivec[3] = &a;
41 cout << " pivec.elem(3) = " << *pivec.elem(3) << endl;
42 cout << " pivec[3] = " << *pivec[3] << endl;
43
44 char c = ’A’;
45 pcvec.elem(3) = &c;
46 pcvec[3] = &c;
47 cout << " pcvec.elem(3) = " << *pcvec.elem(3) << endl;
48 cout << " pcvec[3] = " << *pcvec[3] << endl;
49
50 return 0;
51 }
Listing 4: The output of the test program.
1 Entering vector<T>::vector(int) (0)
2 count = 1
3 Leaving vector<T>::vector(int) (0)
4 Entering vector<T>::vector(int) (0)
5 count = 1
6 Leaving vector<T>::vector(int) (0)
7 Entering vector<T>::vector(int) (0)
8 count = 1
9 Leaving vector<T>::vector(int) (0)
10 Entering vector<void*>::vector(int) (0)
11 count = 1
12 Leaving vector<void*>::vector(int) (0)
13 Entering vector<T*>::vector(int) (0)
14 Leaving vector<T*>::vector(int) (0)
15 Entering vector<void*>::vector(int) (0)
16 count = 2
17 Leaving vector<void*>::vector(int) (0)
3
18 Entering vector<T*>::vector(int) (0)
19 Leaving vector<T*>::vector(int) (0)
20 Entering vector<void*>::vector(int) (0)
21 count = 3
22 Leaving vector<void*>::vector(int) (0)
23 Entering vector<T*>::vector(int) (0)
24 Leaving vector<T*>::vector(int) (0)
25 Entering main() (0)
26 fvec[3] = 10.1
27 ivec[3] = 10
28 cvec[3] = A
29 pfvec.elem(3) = 10.1
30 pfvec[3] = 10.1
31 pivec.elem(3) = 10
32 pivec[3] = 10
33 pcvec.elem(3) = A
34 pcvec[3] = A
35 Leaving main() (0)
36 Entering vector<T*>::~vector (0)
37 Leaving vector<T*>::~vector (0)
38 Entering vector<void*>::~vector (0)
39 count = 3
40 Leaving vector<void*>::~vector (0)
41 Entering vector<T*>::~vector (0)
42 Leaving vector<T*>::~vector (0)
43 Entering vector<void*>::~vector (0)
44 count = 2
45 Leaving vector<void*>::~vector (0)
46 Entering vector<T*>::~vector (0)
47 Leaving vector<T*>::~vector (0)
48 Entering vector<void*>::~vector (0)
49 count = 1
50 Leaving vector<void*>::~vector (0)
51 Entering vector<T>::~vector (0)
52 count = 1
53 Leaving vector<T>::~vector (0)
54 Entering vector<T>::~vector (0)
55 count = 1
56 Leaving vector<T>::~vector (0)
57 Entering vector<T>::~vector (0)
58 count = 1
59 Leaving vector<T>::~vector (0)
Also, to make it easier for you to test your implementation, also given are the trace program, which includes both
Trace.h (Listing 5) and Trace.cpp (Listing 6).
Listing 5: Trace.h.
1 #ifndef __TRACE_H_INCLUDED__
2 #define __TRACE_H_INCLUDED__
3
4 #include <iostream>
5 #include <string>
6
7 using std::cerr;
8 using std::endl;
9 using std::string;
10
11 class Trace {
12 public:
13 Trace(const string n)
14 : name(n)
15 {
4
16 depth++;
17 print_name("Entering");
18 }
19
20 ~Trace()
21 {
22 print_name("Leaving ");
23 depth--;
24 }
25 private:
26 void print_name(const string prefix)
27 {
28 for (int i = 0; i < depth; i++)
29 cerr << " ";
30 cerr << prefix << " " << name << " (" << depth << ")" << endl;
31 }
32 private:
33 static int depth;
34 private:
35 const string name;
36 };
37
38 #ifdef __TRACE__
39 #define TRACE(v,n) Trace v(n)
40 #else
41 #define TRACE(v,n)
42 #endif
43
44 #endif
Listing 6: Trace.cpp.
1 #include "Trace.h"
2
3 int Trace::depth = -1;
Grading Policy
For each part, the following policy applies.
• 10 points for compiling without errors and warnings.
• 30 points for the correctness of your program. Each error will cost you 10 point.
• 10 points for the program structure, readability, efficiency,
5