一、实验内容
建立动态时间数组类(DynamicTimeArray),包含一个长度可变的一维时间类(Time)对象数组, 对外提供的服务包括但不限于:
1 //Time.h 2 #include<iostream> 3 using namespace std; 4 class Time{ 5 protected: 6 int h; 7 int m; 8 int s; 9 public: 10 Time(); 11 Time(int,int,int); 12 int sums(); 13 void show(); 14 int geth(){return h;} 15 int getm(){return m;} 16 int gets(){return s;} 17 void settime(int h3,int m3,int s3); 18 ~Time(){}; 19 }; 20 //DTA.h 21 #include "Time.h" 22 class DTA:public Time{ 23 private: 24 Time *p; 25 int n; 26 public: 27 DTA(int n0); 28 DTA(const DTA& cdta); 29 ~DTA(); 30 int sizeDTA(); 31 void sortDTA(int n); 32 int searchDTA(int h2,int m2,int s2); 33 void edit(int ele); 34 void display(int n); 35 }; 36 //Time.cpp 37 #include "Time.h" 38 #include <iostream> 39 #include <iomanip> 40 using namespace std; 41 Time::Time() 42 { 43 h=0; 44 s=0; 45 m=0; 46 }; 47 Time::Time(int h0,int m0,int s0) 48 { 49 h=h0; 50 m=m0; 51 s=s0; 52 } 53 void Time::show() 54 { 55 cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<m<<":"<<setw(2)<<s<<endl; 56 } 57 int Time::sums() 58 { 59 return ((h*60+m)*60+s); 60 } 61 void Time::settime(int h3,int m3,int s3) 62 { 63 h=h3; 64 m=m3; 65 s=s3; 66 cout<<"set time success."<<endl; 67 } 68 //DTA.cpp 69 #include "DTA.h" 70 #include <iomanip> 71 DTA::DTA(int n0) 72 { 73 n=n0; 74 p=new Time[n](); 75 } 76 DTA::DTA(const DTA& cdta) 77 { 78 h=cdta.h; 79 m=cdta.m; 80 s=cdta.s; 81 } 82 DTA::~DTA() 83 { 84 delete[] p; 85 cout<<"The DynamicTimeArray is deleted."<<endl; 86 } 87 int DTA::searchDTA(int h2,int m2,int s2) 88 { 89 int i,flag2=0; 90 for(i=0;i<n;i++) 91 if(p[i].geth()==h2&&p[i].getm()==m2&&p[i].gets()==s2) 92 { 93 cout<<"element "<<i+1<<" "; 94 p[i].show(); 95 flag2+=1; 96 } 97 if(flag2==0) 98 cout<<"This time is not in this array."<<endl; 99 return flag2; 100 } 101 void DTA::edit(int ele=0) 102 { 103 cout<<"time "<<ele+1<<" "; 104 p[ele].show(); 105 int h3,m3,s3; 106 do{ 107 cout<<"new time(hour minute second):"; 108 cin>>h3>>m3>>s3; 109 if(h3<0||h3>23||m3<0||m3>59||s3<0||s3>59) 110 cout<<"Input error!"<<endl; 111 }while(h3<0||h3>23||m3<0||m3>59||s3<0||s3>59); 112 p[ele].settime(h3,m3,s3); 113 } 114 int DTA:: sizeDTA() 115 { 116 return n; 117 } 118 void DTA::sortDTA(int n) 119 { 120 int i,j; 121 Time h; 122 for(i=0;i<n;i++) 123 { 124 for(j=0;j<n-i;j++) 125 { 126 if(p[i].sums()>p[i+1].sums()) 127 { 128 h=p[i]; 129 p[i]=p[i+1]; 130 p[i+1]=h; 131 } 132 } 133 } 134 } 135 void DTA::display(int n) 136 { 137 int i; 138 for(i=0;i<n;i++) 139 { 140 p[i].show(); 141 } 142 } 143 //main.cpp 144 #include <iostream> 145 #include "DTA.h" 146 using namespace std; 147 int main() 148 { 149 int select,n; 150 cout<<"The size of the array:"<<endl; 151 cin>>n; 152 DTA dta(n); 153 do 154 { 155 cout<<"0 exit 1 size 2 search 3 edit 4 sort 5 display"<<endl; 156 cin>>select; 157 switch(select) 158 { 159 case 0: 160 return 0; 161 case 1: 162 { 163 int size=dta.sizeDTA(); 164 cout<<"the size is"<<size<<endl; 165 break; 166 //size 167 } 168 case 2: 169 { 170 int select2; 171 int h2,m2,s2; 172 do{ 173 cout<<"the time you want to search(hour minute second):"; 174 cin>>h2>>m2>>s2; 175 dta.searchDTA(h2,m2,s2); 176 do{ 177 cout<<"0 back to the menu 1 continue search 2 edit:"; 178 cin>>select2; 179 if(select2!=0&&select2!=1&&select2!=2) 180 cout<<"<INPUT ERROR!>"<<endl; 181 }while(select2!=0&&select2!=1&&select2!=2); 182 if(select2==0||select2==2) 183 break; 184 }while(select2==1); 185 if(select2==0) 186 break; 187 //查找元素 188 } 189 case 3://编辑 190 { 191 int ele; 192 do{ 193 cout<<"the time element you want to edit:"; 194 cin>>ele; 195 ele-=1; 196 if(ele>=dta.sizeDTA()||ele<0) 197 cout<<"the element is not in this array."<<endl; 198 }while(ele>=dta.sizeDTA()||ele<0); 199 dta.edit(ele); 200 break; 201 } 202 case 4://排序 203 { 204 dta.sortDTA(n); 205 cout<<"Sorting is complete!" <<endl; 206 dta.display(n); 207 break; 208 } 209 case 5://输出显示 210 { 211 cout<<"Time:"<<endl; 212 dta.display(n); 213 break; 214 } 215 default: 216 cout<<"Input error!"<<endl; 217 } 218 }while(select!=0); 219 return 0; 220 }
1.error:invalid use of member.
问题:p[i].sums未加().修改为p[i].sums().
2.error:member is protevted within this context.
问题:子类DTA中的指针p指向的time类对象直接访问了相当于其私有成员的保护成员h、m、s。
分析:protected: 可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问,用子类的函数访问即可。
3.报错:setw/setfill was not declared in this scope 解决方案:在DTA.cpp中加上头文件<iomanip>