任务1:
源代码task1.cpp
t.cpp
1 #include "t.h" 2 #include <iostream> 3 #include <string> 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 // static成员数据类外初始化 10 const std::string T::doc{"a simple class sample"}; 11 const int T::max_cnt = 999; 12 int T::cnt = 0; 13 14 15 // 对象方法 16 T::T(int x, int y): m1{x}, m2{y} { 17 ++cnt; 18 cout << "T constructor called.\n"; 19 } 20 21 T::T(const T &t): m1{t.m1}, m2{t.m2} { 22 ++cnt; 23 cout << "T copy constructor called.\n"; 24 } 25 26 T::T(T &&t): m1{t.m1}, m2{t.m2} { 27 ++cnt; 28 cout << "T move constructor called.\n"; 29 } 30 31 T::~T() { 32 --cnt; 33 cout << "T destructor called.\n"; 34 } 35 36 void T::adjust(int ratio) { 37 m1 *= ratio; 38 m2 *= ratio; 39 } 40 41 void T::display() const { 42 cout << "(" << m1 << ", " << m2 << ")" ; 43 } 44 45 // 类方法 46 int T::get_cnt() { 47 return cnt; 48 } 49 50 // 友元 51 void func() { 52 T t5(42); 53 t5.m2 = 2049; 54 cout << "t5 = "; t5.display(); cout << endl; 55 }
t.h
1 #pragma once 2 3 #include <string> 4 5 // 类T: 声明 6 class T { 7 // 对象属性、方法 8 public: 9 T(int x = 0, int y = 0); // 普通构造函数 10 T(const T &t); // 复制构造函数 11 T(T &&t); // 移动构造函数 12 ~T(); // 析构函数 13 14 void adjust(int ratio); // 按系数成倍调整数据 15 void display() const; // 以(m1, m2)形式显示T类对象信息 16 17 private: 18 int m1, m2; 19 20 // 类属性、方法 21 public: 22 static int get_cnt(); // 显示当前T类对象总数 23 24 public: 25 static const std::string doc; // 类T的描述信息 26 static const int max_cnt; // 类T对象上限 27 28 private: 29 static int cnt; // 当前T类对象数目 30 31 // 类T友元函数声明 32 friend void func(); 33 }; 34 35 // 普通函数声明 36 void func();
task1.cpp
1 #include "t.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test(); 8 9 int main() { 10 test(); 11 cout << "\nmain: \n"; 12 cout << "T objects'current count: " << T::get_cnt() << endl; 13 } 14 15 void test() { 16 cout << "test class T: \n"; 17 cout << "T info: " << T::doc << endl; 18 cout << "T objects'max count: " << T::max_cnt << endl; 19 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 20 21 22 T t1; 23 cout << "t1 = "; t1.display(); cout << endl; 24 25 T t2(3, 4); 26 cout << "t2 = "; t2.display(); cout << endl; 27 28 T t3(t2); 29 t3.adjust(2); 30 cout << "t3 = "; t3.display(); cout << endl; 31 32 T t4(std::move(t2)); 33 cout << "t3 = "; t4.display(); cout << endl; 34 35 cout << "T objects'current count: " << T::get_cnt() << endl; 36 37 func(); 38 }
运行结果截图:
问题1:
不能删除。如果删除,整个函数中就不存在func()函数的声明,只存在对友元函数的声明。
问题2:
line 9 是普通构造函数,参数x,y在默认情况下有默认的初值为0。当生成一个对象,有初值时则给其赋值,否则x,y默认值均为0。
line 10 是复制构造函数,传入一个已经存在的T类对象,再生成一个新的T类对象,且新的对象x,y 的值都与传过来的参数的x,y值相等。当创建一个对象且参数值等于另外一个已经存在的对象时,调用复制函数。
line 11是移动构造函数,右值引用,新生成一个对象,将原有的对象的x,y值剪切给新的对象,原来的对象不再拥有本来的x,y。当创建一个对象,其参数值等于另外一个已经存在的对象且已经存在的对象在之后都没有用处时,可以调用移动函数将参数值赋给新对象,已经存在的对象的原参数值不再存在,这样能节省空间。
line 12 是析构函数,它在每个生命周期结束时会被调用,进行清理工作。
问题3:
不能
任务2:
源代码task2.cpp
Complex.h
1 #pragma once 2 #include<bits/stdc++.h> 3 4 class Complex{ 5 public: 6 Complex(double r=0,double i=0); 7 Complex(const Complex& c); 8 9 public: 10 static const std::string doc; 11 private: 12 double imag,real; 13 14 public: 15 double get_real() const; 16 double get_imag() const; 17 Complex add(const Complex& c); 18 19 friend Complex add(const Complex& c1,const Complex& c2); 20 friend bool is_equal(const Complex& c1,const Complex& c2); 21 friend bool is_not_equal(const Complex& c1,const Complex& c3); 22 friend double abs(const Complex& c); 23 friend void output(const Complex& c); 24 25 ~Complex(); 26 };
Complex.cpp
1 #include"Complex.h" 2 #include<bits/stdc++.h> 3 4 using namespace std; 5 6 const string Complex::doc{"a simplified complex class"}; 7 8 Complex::Complex(double r,double i):real{r},imag{i}{ 9 } 10 11 Complex::Complex(const Complex& c):real{c.real},imag{c.imag}{ 12 13 } 14 15 16 double Complex::get_real() const{ 17 return real; 18 } 19 20 double Complex::get_imag() const{ 21 return imag; 22 } 23 24 Complex Complex::add(const Complex &c){ 25 real+=c.real; 26 imag+=c.imag; 27 } 28 29 Complex add(const Complex& c1,const Complex& c2) 30 { 31 Complex c4; 32 c4.imag=c1.imag+c2.imag; 33 c4.real=c1.real+c2.real; 34 return c4; 35 } 36 bool is_equal(const Complex& c1,const Complex& c2){ 37 if(c1.imag==c2.imag&&c1.real==c2.real) 38 return true; 39 else 40 return false; 41 } 42 bool is_not_equal(const Complex &c1,const Complex &c3){ 43 if(c1.imag==c3.imag&&c1.real==c3.real) 44 return false; 45 else 46 return true; 47 } 48 double abs(const Complex &c) 49 { 50 double s; 51 s=sqrt(c.imag*c.imag+c.real*c.real); 52 return s; 53 } 54 void output(const Complex &c){ 55 cout<<c.real; 56 if(c.imag>=0) cout<<" + "<<c.imag<<"i"<<endl; 57 else cout<<" - "<<(-1)*c.imag<<"i"<<endl; 58 } 59 Complex::~Complex(){ 60 61 }
task2.cpp
1 #include "Complex.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 8 void test() { 9 cout << "类成员测试: " << endl; 10 cout << Complex::doc << endl; 11 12 cout << endl; 13 14 cout << "Complex对象测试: " << endl; 15 Complex c1; 16 Complex c2(3, -4); 17 const Complex c3(3.5); 18 Complex c4(c3); 19 20 cout << "c1 = "; output(c1); cout << endl; 21 cout << "c2 = "; output(c2); cout << endl; 22 cout << "c3 = "; output(c3); cout << endl; 23 cout << "c4 = "; output(c4); cout << endl; 24 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 25 26 cout << endl; 27 28 cout << "复数运算测试: " << endl; 29 cout << "abs(c2) = " << abs(c2) << endl; 30 c1.add(c2); 31 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 32 cout << boolalpha; 33 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 34 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 35 c4 = add(c2, c3); 36 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 37 } 38 39 int main() { 40 test(); 41 }
运行结果截图:
任务3:
1 #include <iostream> 2 #include <complex> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 using std::complex; 8 9 void test() { 10 cout << "标准库模板类comple测试: " << endl; 11 complex<double> c1; 12 complex<double> c2(3, -4); 13 const complex<double> c3(3.5); 14 complex<double> c4(c3); 15 16 cout << "c1 = " << c1 << endl; 17 cout << "c2 = " << c2 << endl; 18 cout << "c3 = " << c3 << endl; 19 cout << "c4 = " << c4 << endl; 20 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl; 21 cout << endl; 22 23 cout << "复数运算测试: " << endl; 24 cout << "abs(c2) = " << abs(c2) << endl; 25 c1 += c2; 26 cout << "c1 += c2, c1 = " << c1 << endl; 27 cout << boolalpha; 28 cout << "c1 == c2 : " << (c1 == c2) << endl; 29 cout << "c1 != c3 : " << (c1 != c3) << endl; 30 c4 = c2 + c3; 31 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 32 }
运行结果截屏:
1 #include"Fraction.h" 2 #include<bits/stdc++.h> 3 using namespace std; 4 5 const string Fraction::doc{"Fraction类 v 0.01版.目前仅支持分数对象的构造、输出、加/减/乘/除运算."}; 6 7 Fraction::Fraction(int x,int y):up{x},down{y}{ 8 int max; 9 if(abs(up)>abs(down)) 10 max=abs(up); 11 else 12 max=abs(down); 13 int i=1; 14 while(i<max) 15 { 16 i++; 17 if(up%i==0&&down%i==0) 18 { 19 up/=i; 20 down/=i; 21 i=1; 22 } 23 } 24 //cout<<endl; 25 //cout<<"* "<<up<<" "<<down<<endl; 26 } 27 28 Fraction::Fraction( Fraction &f):up{f.up},down{f.down}{ 29 } 30 31 Fraction::~Fraction(){ 32 } 33 34 int Fraction::get_up(){ 35 return up; 36 } 37 int Fraction::get_down(){ 38 return down; 39 } 40 41 Fraction Fraction::negative(){ 42 Fraction f4; 43 f4.up=-up; 44 f4.down=down; 45 return f4; 46 } 47 48 void output( Fraction &f){ 49 if(f.down<0) 50 cout<<"-"<<f.up<<"/"<<abs(f.down)<<endl; 51 else 52 if(f.down==1) 53 cout<<f.up<<endl; 54 else if(f.down==0) 55 cout<<"分母不能为0"<<endl; 56 else 57 cout<<f.up<<"/"<<f.down<<endl; 58 } 59 60 Fraction add( Fraction &f1, Fraction &f2){ 61 Fraction f3; 62 if(f1.up>0&&f1.down>0&&f2.up>0&&f2.down>0) 63 { 64 f3.up=f1.up*f2.down+f2.up*f1.down; 65 f3.down=f1.down*f2.down; 66 int max; 67 if(f3.up>f3.down) 68 max=f3.up; 69 else 70 max=f3.down; 71 int i=1; 72 while(i<max) 73 { 74 i++; 75 if(f3.up%i==0&&f3.down%i==0) 76 { 77 f3.up/=i; 78 f3.down/=i; 79 i=1; 80 } 81 } 82 } 83 else if(f1.up*f1.down>0&&f2.up*f2.down>0){ 84 Fraction f11,f22; 85 f11.up=abs(f1.up); 86 f11.down=abs(f1.down); 87 f22.up=abs(f2.up); 88 f22.down=abs(f2.down); 89 f3.up=f11.up*f22.down+f22.up*f11.down; 90 f3.down=f11.down*f22.down; 91 int max; 92 if(f3.up>f3.down) 93 max=f3.up; 94 else 95 max=f3.down; 96 int i=1; 97 while(i<max) 98 { 99 i++; 100 if(f3.up%i==0&&f3.down%i==0) 101 { 102 f3.up/=i; 103 f3.down/=i; 104 i=1; 105 } 106 } 107 f3.up=-f3.up; 108 } 109 else if(f1.up*f1.down<0) 110 { 111 Fraction f11,f22; 112 f11.up=abs(f1.up); f11.down=abs(f1.down); 113 f3.down=f2.down*f11.down; 114 f3.up=f2.up*f11.down-f11.up*f2.down; 115 int max; 116 if(f3.up>f3.down) 117 max=f3.up; 118 else 119 max=f3.down; 120 int i=1; 121 while(i<max) 122 { 123 i++; 124 if(f3.up%i==0&&f3.down%i==0) 125 { 126 f3.up/=i; 127 f3.down/=i; 128 i=1; 129 } 130 } 131 } 132 else{ 133 Fraction f11,f22; 134 f22.up=abs(f2.up); f22.down=abs(f2.down); 135 f3.down=f22.down*f1.down; 136 f3.up=f1.up*f22.down-f22.up*f1.down; 137 int max; 138 if(f3.up>f3.down) 139 max=f3.up; 140 else 141 max=f3.down; 142 int i=1; 143 while(i<max) 144 { 145 i++; 146 if(f3.up%i==0&&f3.down%i==0) 147 { 148 f3.up/=i; 149 f3.down/=i; 150 i=1; 151 } 152 } 153 } 154 return f3; 155 } 156 157 Fraction sub( Fraction &f1, Fraction &f2) 158 { 159 Fraction f3; 160 if(f1.up>0&&f1.down>0&&f2.up>0&&f2.down>0) 161 { 162 f3.up=f1.up*f2.down-f2.up*f1.down; 163 f3.down=f1.down*f2.down; 164 int max; 165 if(abs(f3.up)>abs(f3.down)) 166 max=abs(f3.up); 167 else 168 max=abs(f3.down); 169 int i=1; 170 while(i<max) 171 { 172 i++; 173 if(f3.up%i==0&&f3.down%i==0) 174 { 175 f3.up/=i; 176 f3.down/=i; 177 i=1; 178 } 179 } 180 } 181 else if(f1.up*f1.down>0&&f2.up*f2.down>0){ 182 Fraction f11,f22; 183 f11.up=abs(f1.up); 184 f11.down=abs(f1.down); 185 f22.up=abs(f2.up); 186 f22.down=abs(f2.down); 187 f3.up=f11.up*f22.down-f22.up*f11.down; 188 f3.down=f11.down*f22.down; 189 int max; 190 if(abs(f3.up)>abs(f3.down)) 191 max=abs(f3.up); 192 else 193 max=abs(f3.down); 194 int i=1; 195 while(i<max) 196 { 197 i++; 198 if(f3.up%i==0&&f3.down%i==0) 199 { 200 f3.up/=i; 201 f3.down/=i; 202 i=1; 203 } 204 } 205 f3.up=-f3.up; 206 } 207 else if(f1.up*f1.down<0) 208 { 209 Fraction f11,f22; 210 f11.up=abs(f1.up); f11.down=abs(f1.down); 211 f3.down=f2.down*f11.down; 212 f3.up=-f11.up*f2.down-f2.up*f11.down; 213 int max; 214 if(abs(f3.up)>abs(f3.down)) 215 max=abs(f3.up); 216 else 217 max=abs(f3.down); 218 int i=1; 219 while(i<max) 220 { 221 i++; 222 if(f3.up%i==0&&f3.down%i==0) 223 { 224 f3.up/=i; 225 f3.down/=i; 226 i=1; 227 } 228 } 229 } 230 else{ 231 Fraction f11,f22; 232 f22.up=abs(f2.up); f22.down=abs(f2.down); 233 f3.down=f22.down*f1.down; 234 f3.up=f1.up*f22.down+f22.up*f1.down; 235 int max; 236 if(abs(f3.up)>abs(f3.down)) 237 max=abs(f3.up); 238 else 239 max=abs(f3.down); 240 int i=1; 241 while(i<max) 242 { 243 i++; 244 if(f3.up%i==0&&f3.down%i==0) 245 { 246 f3.up/=i; 247 f3.down/=i; 248 i=1; 249 } 250 } 251 } 252 return f3; 253 } 254 255 Fraction mul( Fraction &f1, Fraction &f2){ 256 Fraction f3; 257 f3.up=f1.up*f2.up; 258 // cout<<"*"<<f1.up<<" "<<f2.up<<" "<<f3.up; 259 f3.down=f1.down*f2.down; 260 // cout<<"*"<<f1.down<<" "<<f2.down<<" "<<f3.down; 261 int max; 262 if(abs(f3.up)>abs(f3.down)) 263 max=abs(f3.up); 264 else 265 max=abs(f3.down); 266 int i=1; 267 while(i<max) 268 { 269 i++; 270 if(f3.up%i==0&&f3.down%i==0) 271 { 272 f3.up/=i; 273 f3.down/=i; 274 i=1; 275 } 276 277 } 278 return f3; 279 } 280 281 Fraction div( Fraction &f1, Fraction &f2){ 282 Fraction f3; 283 f3.up=f1.up*f2.down; 284 f3.down=f1.down*f2.up; 285 int max; 286 if(abs(f3.up)>abs(f3.down)) 287 max=abs(f3.up); 288 else 289 max=abs(f3.down); 290 int i=1; 291 while(i<max) 292 { 293 i++; 294 if(f3.up%i==0&&f3.down%i==0) 295 { 296 f3.up/=i; 297 f3.down/=i; 298 i=1; 299 } 300 301 } 302 return f3; 303 }
Fraction.h
1 #pragma once 2 #include<bits/stdc++.h> 3 4 class Fraction{ 5 public: 6 Fraction(int x=1,int y=1); 7 Fraction( Fraction &f); 8 ~Fraction(); 9 10 private: 11 int up,down; 12 13 public: 14 static const std::string doc; 15 16 public: 17 int get_up(); 18 int get_down(); 19 20 Fraction negative(); 21 22 23 friend void output( Fraction &f); 24 friend Fraction add( Fraction &f1, Fraction &f2); 25 friend Fraction sub( Fraction &f1, Fraction &f2); 26 friend Fraction mul( Fraction &f1, Fraction &f2); 27 friend Fraction div( Fraction &f1, Fraction &f2); 28 29 }; 30 void output( Fraction &f); 31 Fraction add( Fraction &f1, Fraction &f2); 32 Fraction sub( Fraction &f1, Fraction &f2); 33 Fraction mul( Fraction &f1, Fraction &f2); 34 Fraction div( Fraction &f1, Fraction &f2);
task4.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 8 void test1() { 9 cout << "Fraction类测试: " << endl; 10 cout << Fraction::doc << endl << endl; 11 12 Fraction f1(5); 13 Fraction f2(3, -4), f3(-18, 12); 14 Fraction f4(f3); 15 cout << "f1 = "; output(f1); cout << endl; 16 cout << "f2 = "; output(f2); cout << endl; 17 cout << "f3 = "; output(f3); cout << endl; 18 cout << "f4 = "; output(f4); cout << endl; 19 20 Fraction f5; 21 f5=f4.negative(); 22 cout << "f5 = "; output(f5); cout << endl; 23 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 24 25 Fraction f; 26 cout << "f1 + f2 = "; 27 f=add(f1, f2); output(f); cout << endl; 28 cout << "f1 - f2 = "; 29 f=sub(f1, f2); output(f); cout << endl; 30 cout << "f1 * f2 = "; 31 f=mul(f1, f2); output(f); cout << endl; 32 cout << "f1 / f2 = "; 33 f=div(f1, f2); output(f); cout << endl; 34 cout << "f4 + f5 = "; 35 f=add(f4, f5); output(f); cout << endl; 36 } 37 38 void test2() { 39 Fraction f6(42, 55), f7(0, 3); 40 cout << "f6 = "; output(f6); cout << endl; 41 cout << "f7 = "; output(f7); cout << endl; 42 cout << "f6 / f7 = "; 43 Fraction f; 44 f=div(f6, f7); output(f); cout << endl; 45 } 46 47 int main() { 48 cout << "测试1: Fraction类基础功能测试\n"; 49 test1(); 50 51 cout << "\n测试2: 分母为0测试: \n"; 52 test2(); 53 }
运行结果截图:
任务5:
源代码task5.cpp
account.h
1 #pragma once 2 #include<bits/stdc++.h> 3 class SavingAccount{ 4 private: 5 int id; 6 double balance; 7 double rate; 8 int lastDate; 9 double accumulation; 10 static double total; 11 void record(int date,double amount); 12 double accumulate(int date) const{ 13 return accumulation+balance*(date-lastDate); 14 } 15 public: 16 SavingAccount (int date,int id,double rate); 17 int getID() const {return id;} 18 double getBalance() const {return balance;} 19 double getRate() const {return rate;} 20 static double getTotal() {return total;} 21 void deposit(int date,double amount); 22 void withdraw(int date,double amount); 23 void settle(int date); 24 void show() const; 25 }; 26
account.cpp
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingAccount::total=0; 7 SavingAccount::SavingAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 8 cout<<date<<"\t#"<<id<<" is created"<<endl; 9 } 10 void SavingAccount::record(int date,double amount){ 11 accumulation=accumulate(date); 12 lastDate=date; 13 amount=floor(amount*100+0.5)/100; 14 balance+=amount; 15 total+=amount; 16 cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; 17 } 18 void SavingAccount::deposit(int date,double amount){ 19 record(date,amount); 20 } 21 void SavingAccount::withdraw(int data,double amount){ 22 if(amount>getBalance()) 23 cout<<"Error: not enough money"<<endl; 24 else 25 record(data,-amount); 26 } 27 void SavingAccount::settle(int date){ 28 double interest=accumulate(date)*rate/365; 29 if(interest!=0) 30 record(date,interest); 31 accumulation=0; 32 } 33 void SavingAccount::show() const{ 34 cout<<"# "<<id<<"\tBalance: "<<balance; 35 }
5_11.cpp
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 SavingAccount sa0(1,21325302,0.015); 6 SavingAccount sa1(1,58320212,0.015); 7 8 sa0.deposit(5,5000); 9 sa1.deposit(25,10000); 10 sa0.deposit(45,5500); 11 sa1.withdraw(60,4000); 12 13 sa0.settle(90); 14 sa1.settle(90); 15 sa0.show(); cout<<endl; 16 sa1.show(); cout<<endl; 17 cout<<"Total: "<<SavingAccount::getTotal()<<endl; 18 return 0; 19 }
运行结果截图:
挺合理的,程序中固定不变的值和函数尽量用const保护起来,有效避免在程序中改变其值。
实验总结:
在编写代码的时候private和public需要清楚,不然在后续使用中权限会出现差错。
不需要改变的值和函数尽量用const保护起来,避免不小心的情况下的改变导致程序结果出错。
标准库在一定程度上可以简洁代码,减少代码量,可以去学习掌握,在必要时灵活使用。