p427.4
1 //头文件: 2 #include<iostream> 3 #ifndef STACK_H_ 4 #define STACK_H_ 5 typedef unsigned long Item; 6 7 class Stack{ 8 private: 9 enum{MAX=10}; 10 Item *pitems; 11 int size; 12 int top; 13 public: 14 Stack(int n = 7); 15 Stack(const Stack &st); 16 ~Stack(); 17 bool isempty()const; 18 bool isfull()const; 19 bool push(const Item & item); 20 bool pop(); 21 Stack & operator=(const Stack & st); 22 friend std::ostream & operator<<(std::ostream & os, const Stack &st); 23 }; 24 25 #endif 26 27 //方法: 28 #include<iostream> 29 #include<cstring> 30 #include"Stack.h" 31 using std::cout; 32 using std::endl; 33 34 Stack::Stack(int n){ 35 size = n; 36 top = n; 37 pitems = new Item[n]; 38 for (int i = 0; i < size; i++) 39 pitems[i] = 0; 40 } 41 42 Stack::Stack(const Stack &st){ 43 size = st.size; 44 top = st.top; 45 pitems = new Item[size]; 46 for (int i = 0; i < size; i++) 47 pitems[i] = st.pitems[i]; 48 } 49 50 Stack::~Stack(){ 51 delete[]pitems; 52 } 53 54 bool Stack::isempty()const{ 55 if (top == 0) 56 return true; 57 else return false; 58 } 59 60 bool Stack::isfull()const{ 61 if (top == MAX) 62 return true; 63 else return false; 64 } 65 66 bool Stack::push(const Item & item){ 67 if (isfull()){ 68 cout << "the stack is already full" << endl; 69 return false; 70 } 71 else{ 72 Item *temp; 73 temp = new Item[size + 1]; 74 for (int i = 0; i < size; i++) 75 *(temp + i) = *(pitems + i); 76 *(temp + size) = item; 77 delete[]pitems; 78 pitems = temp; 79 size++; 80 top++; 81 return true; 82 } 83 } 84 85 bool Stack::pop(){ 86 if (isempty()){ 87 cout << "the stack is already empty" << endl; 88 return false; 89 } 90 else{ 91 Item *temp; 92 temp = new Item[size - 1]; 93 for (int i = 0; i < (size - 1); i++) 94 temp[i] = pitems[i]; 95 delete[]pitems; 96 pitems = temp; 97 size--; 98 top--; 99 return true; 100 } 101 } 102 103 Stack & Stack::operator=(const Stack & st){ 104 if (this == &st) 105 return *this; 106 size = st.size; 107 top = st.top; 108 delete[]pitems; 109 pitems = new Item[st.size]; 110 for (int i = 0; i < size; i++) 111 pitems[i] = st.pitems[i]; 112 return *this; 113 } 114 115 std::ostream & operator<<(std::ostream & os, const Stack &st){ 116 os << "size: " << st.size << endl 117 << "top: " << st.top << endl; 118 for (int i = 0; i < st.size; i++) 119 os << st.pitems[i] << " "; 120 return os; 121 } 122 123 //驱动: 124 #include<iostream> 125 #include<cstdlib> 126 #include"Stack.h" 127 using namespace std; 128 129 int main(){ 130 Stack ct1; 131 Stack ct2(5); 132 cout << "ct1 "<< ct1 <<endl; 133 cout << "ct2 "<< ct2 <<endl; 134 Stack ct3 = ct2; 135 cout << "ct3 "<< ct3 << endl; 136 ct1 = ct2; 137 cout << "ct1 " << ct1 << endl; 138 ct2.push(4); 139 cout << "ct2 " << ct2 << endl; 140 ct1.pop(); 141 cout << "ct1 "<< ct1 << endl; 142 143 system("pause"); 144 return 0; 145 }
p474.1
1 //头文件: 2 class Cd{ 3 private: 4 char performers[50]; 5 char label[20]; 6 int selections; 7 double playtime; 8 public: 9 Cd(char *s1, char *s2, int n, double x); 10 Cd(const Cd & st); 11 Cd(); 12 virtual ~Cd(); 13 virtual void Report()const; 14 Cd & operator = (const Cd & st); 15 }; 16 17 class Classic : public Cd 18 { 19 private: 20 char production[50]; 21 public: 22 Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = 0, double x = 0); 23 Classic(const Classic & st); 24 virtual void Report()const; 25 Classic & operator=(const Classic & st); 26 }; 27 28 //方法: 29 #include<iostream> 30 #include<cstring> 31 #include"classic.h" 32 33 using std::cout; 34 using std::endl; 35 36 Cd::Cd(char *s1, char *s2, int n, double x){ 37 strncpy(performers, s1, 50); 38 performers[49] = '