实验二
任务一:
t.h
1 #pragma once 2 #include <string> 3 4 class T { 5 public: 6 T(int x = 0, int y = 0); 7 T(const T &t); 8 T(T &&t); 9 ~T(); 10 11 void adjust(int ratio); 12 void display() const; 13 private: 14 int m1, m2; 15 16 public: 17 static int get_cnt(); 18 public: 19 static const std::string doc; 20 static const int max_cnt; 21 private: 22 static int cnt; 23 friend void func(); 24 }; 25 void func();
t.cpp:
1 #include "t.h" 2 #include <iostream> 3 #include <string> 4 using std::cout; 5 using std::endl; 6 using std::string; 7 8 const std::string T::doc { 9 "a simple class sample" 10 }; 11 const int T::max_cnt = 999; 12 int T::cnt = 0; 13 14 T::T(int x, int y): m1 {x}, m2 {y} { 15 ++cnt; 16 cout << "T constructor called.\n"; 17 } 18 T::T(const T &t): m1 {t.m1}, m2 {t.m2} { 19 ++cnt; 20 cout << "T copy constructor called.\n"; 21 } 22 T::T(T &&t): m1 {t.m1}, m2 {t.m2} { 23 ++cnt; 24 cout << "T move constructor called.\n"; 25 } 26 T::~T() { 27 --cnt; 28 cout << "T destructor called.\n"; 29 } 30 void T::adjust(int ratio) { 31 m1 *= ratio; 32 m2 *= ratio; 33 } 34 void T::display() const { 35 cout << "(" << m1 << ", " << m2 << ")" ; 36 } 37 int T::get_cnt() { 38 return cnt; 39 } 40 void func() { 41 T t5(42); 42 t5.m2 = 2049; 43 cout << "t5 = "; 44 t5.display(); 45 cout << endl; 46 }
test.cpp:
1 #include "t.h" 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 void test(); 6 int main() { 7 test(); 8 cout << "\nmain: \n"; 9 cout << "T objects'current count: " << T::get_cnt() << endl; 10 } 11 void test() { 12 cout << "test class T: \n"; 13 cout << "T info: " << T::doc << endl; 14 cout << "T objects'max count: " << T::max_cnt << endl; 15 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 16 T t1; 17 cout << "t1 = "; 18 t1.display(); 19 cout << endl; 20 T t2(3, 4); 21 cout << "t2 = "; 22 t2.display(); 23 cout << endl; 24 T t3(t2); 25 t3.adjust(2); 26 cout << "t3 = "; 27 t3.display(); 28 cout << endl; 29 T t4(std::move(t2)); 30 cout << "t3 = "; 31 t4.display(); 32 cout << endl; 33 cout << "T objects'current count: " << T::get_cnt() << endl; 34 func(); 35 }
编译结果:
结论:
问题1:t.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉
line36,重新编译,是否能正确运行。
不可以,并没有对func()函数进行声明,主函数运行失败。
问题2:
t.h中,line9-12给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析
构函数的调用时机。
普通构造函数:普通构造函数用于初始化对象的状态和属性。对象创建时自动调用普通构造函数进行初始化。
复制构造函数:复制构造函数用于创建对象时复制已有对象。对象以已有对象初始化时调用复制构造函数。
移动构造函数:移动构造函数实现资源转移,高效创建新对象。对象以右值初始化时调用移动构造函数。
析构函数:析构函数用于释放对象占用的资源,清理对象。对象生命周期结束时自动调用析构函数。
问题3:
t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。
不可以。
任务二:
Complex.h
1 #pragma once 2 #include <string> 3 #include "Complex.h" 4 5 class Complex { 6 public: 7 Complex(double r = 0, double i = 0); 8 Complex(const Complex& c); 9 ~ Complex(); 10 11 void add(const Complex& c); 12 13 public: 14 double get_real() const; 15 double get_imag() const; 16 17 public: 18 friend Complex add(const Complex& c1, const Complex& c2); 19 friend bool is_equal(const Complex& c1, const Complex& c2); 20 friend bool is_not_equal(const Complex& c1, const Complex& c2); 21 friend void output(const Complex& c); 22 friend double abs(const Complex& c); 23 24 private: 25 double real; 26 double imag; 27 28 public: 29 static const std::string doc; 30 31 };
Complex.cpp
1 #include "Complex.h" 2 #include<iostream> 3 #include<string> 4 #include<cmath> 5 6 using namespace std; 7 8 const string Complex::doc { 9 "a simplified complex class" 10 }; 11 12 Complex::Complex(double r,double i):real {r},imag {i} {} 13 Complex::Complex(const Complex&c):real {c.real},imag {c.imag} {} 14 Complex::~Complex() {} 15 16 void Complex::add(const Complex&c) { 17 real +=c.real; 18 imag +=c.imag; 19 } 20 21 double Complex::get_real() const{ 22 return real; 23 } 24 double Complex::get_imag() const{ 25 return imag; 26 } 27 28 Complex add(const Complex&c1,const Complex&c2) { 29 return Complex(c1.real + c2.real, c1.imag + c2.imag); 30 } 31 bool is_equal(const Complex&c1,const Complex&c2) { 32 if(c1.real==c2.real&&c1.imag==c2.imag)return true; 33 else return false; 34 35 } 36 bool is_not_equal(const Complex&c1,const Complex&c2) { 37 if(c1.real==c2.real&&c1.imag==c2.imag)return false; 38 else return true; 39 40 } 41 void output(const Complex&c) { 42 if(c.imag<0) { 43 cout<<c.real<<"-"<<c.imag*(-1)<<"i"; 44 } else { 45 cout<<c.real<<"+"<<c.imag<<"i"; 46 } 47 } 48 double abs(const Complex&c) { 49 return sqrt(c.real*c.real+c.imag*c.imag); 50 }
tas2.cpp
1 #include <iostream> 2 #include "Complex.h" 3 #include <string> 4 5 using std::cout; 6 using std::endl; 7 using std::boolalpha; 8 void test() { 9 cout << "类成员测试: " << endl; 10 cout << Complex::doc << endl; 11 cout << endl; 12 cout << "Complex对象测试: " << endl; 13 Complex c1; 14 Complex c2(3, -4); 15 const Complex c3(3.5); 16 Complex c4(c3); 17 cout << "c1 = "; 18 output(c1); 19 cout << endl; 20 cout << "c2 = "; 21 output(c2); 22 cout << endl; 23 cout << "c3 = "; 24 output(c3); 25 cout << endl; 26 cout << "c4 = "; 27 output(c4); 28 cout << endl; 29 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() 30 << endl; 31 cout << endl; 32 cout << "复数运算测试: " << endl; 33 cout << "abs(c2) = " << abs(c2) << endl; 34 c1.add(c2); 35 cout << "c1 += c2, c1 = "; 36 output(c1); 37 cout << endl; 38 cout << boolalpha; 39 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 40 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 41 c4 = add(c2, c3); 42 cout << "c4 = c2 + c3, c4 = "; 43 output(c4); 44 cout << endl; 45 } 46 int main() { 47 test(); 48 }
编译结果:
任务三:
1 #include <iostream> 2 #include <complex> 3 using std::cout; 4 using std::endl; 5 using std::boolalpha; 6 using std::complex; 7 void test() { 8 cout << "标准库模板类comple测试: " << endl; 9 complex<double> c1; 10 complex<double> c2(3, -4); 11 const complex<double> c3(3.5); 12 complex<double> c4(c3); 13 cout << "c1 = " << c1 << endl; 14 cout << "c2 = " << c2 << endl; 15 cout << "c3 = " << c3 << endl; 16 cout << "c4 = " << c4 << endl; 17 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << 18 endl; 19 cout << endl; 20 cout << "复数运算测试: " << endl; 21 cout << "abs(c2) = " << abs(c2) << endl; 22 c1 += c2; 23 cout << "c1 += c2, c1 = " << c1 << endl; 24 cout << boolalpha; 25 cout << "c1 == c2 : " << (c1 == c2) << endl; 26 cout << "c1 != c3 : " << (c1 != c3) << endl; 27 c4 = c2 + c3; 28 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 29 } 30 int main() { 31 test(); 32 }
编译结果:
结论:
提供接口:构造函数、成员函数、友元函数
使用标注库模板类回事代码更加简洁、简单
任务四:
Fraction.h:
1 #pragma once 2 #include<string> 3 4 using std::string; 5 class Fraction { 6 7 public: 8 Fraction(int x,int y=1); 9 Fraction(const Fraction &f); 10 ~Fraction(); 11 12 public: 13 int get_up() const; 14 int get_down() const; 15 Fraction negative(); 16 17 Fraction simplify(); 18 19 20 private: 21 int up; 22 int down; 23 24 25 private: 26 friend void output(const Fraction &f); 27 friend Fraction add(const Fraction &f1,const Fraction &f2); 28 friend Fraction sub(const Fraction &f1,const Fraction &f2); 29 friend Fraction mul(const Fraction &f1,const Fraction &f2); 30 friend Fraction div(const Fraction &f1,const Fraction &f2); 31 32 public: 33 static const string doc; 34 }; 35 36 void output(const Fraction& f1); 37 Fraction add(const Fraction& f1, const Fraction& f2); 38 Fraction sub(const Fraction& f1, const Fraction& f2); 39 Fraction mul(const Fraction& f1, const Fraction& f2); 40 Fraction div(const Fraction& f1, const Fraction& f2);
Fraction.cpp:
1 #include <iostream> 2 #include <string> 3 #include "Fraction.h" 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 const string Fraction::doc{ 10 "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算." 11 }; 12 13 Fraction::Fraction(int x,int y):up{x},down{y}{} 14 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{} 15 Fraction::~Fraction(){} 16 17 18 int Fraction::get_up() const{ 19 return up; 20 } 21 22 int Fraction::get_down() const{ 23 return down; 24 } 25 26 Fraction Fraction::negative() { 27 Fraction t = simplify(); 28 return Fraction(-t.up,t.down); 29 } 30 31 Fraction Fraction::simplify() 32 { 33 int small, x, y, z; 34 if (up >= 0) 35 { 36 x = up; 37 if (down > 0) 38 { 39 y = down; 40 z = 1; 41 } 42 else 43 { 44 y = -down; 45 z = -1; 46 } 47 } 48 else 49 { 50 x = -up; 51 if (down > 0) 52 { 53 y = down; 54 z = -1; 55 } 56 else 57 { 58 y = -down; 59 z = 1; 60 } 61 } 62 if (x > y) 63 small = y; 64 else 65 small = x; 66 int i; 67 for (i = small; i >= 1; i--) 68 if (x % i == 0 && y % i == 0) 69 { 70 x = x / i; 71 y = y / i; 72 break; 73 } 74 if (z == 1) 75 return Fraction(x, y); 76 return Fraction(-x, y); 77 } 78 79 void output(const Fraction& f){ 80 Fraction t = Fraction(f.up,f.down); 81 t = t.simplify(); 82 if(t.down == 0) cout << "分母不能为0"; 83 else { 84 if(t.up == 0) 85 cout << "0"; 86 else if(t.up != 0 && t.down == 1) 87 cout << t.up; 88 else 89 cout << t.up << "/" << t.down; 90 cout << endl; 91 } 92 } 93 Fraction add(const Fraction &f1,const Fraction &f2) { 94 int num = f1.up * f2.down + f2.up * f1.down; 95 int denom = f1.down * f2.down; 96 return Fraction(num, denom); 97 } 98 Fraction sub(const Fraction &f1,const Fraction &f2) { 99 int num = f1.up * f2.down - f2.up * f1.down; 100 int denom = f1.down * f2.down; 101 return Fraction(num, denom); 102 } 103 Fraction mul(const Fraction &f1,const Fraction &f2) { 104 return Fraction(f1.up*f2.up,f1.down*f2.down); 105 106 } 107 Fraction div(const Fraction &f1,const Fraction &f2) { 108 return Fraction(f1.up*f2.down,f1.down*f2.up); 109 110 }
task4.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 void test1() { 6 cout << "Fraction类测试: " << endl; 7 cout << Fraction::doc << endl << endl; 8 Fraction f1(5); 9 Fraction f2(3, -4), f3(-18, 12); 10 Fraction f4(f3); 11 cout << "f1 = "; 12 output(f1); 13 cout << endl; 14 cout << "f2 = "; 15 output(f2); 16 cout << endl; 17 cout << "f3 = "; 18 output(f3); 19 cout << endl; 20 cout << "f4 = "; 21 output(f4); 22 cout << endl; 23 Fraction f5(f4.negative()); 24 cout << "f5 = "; 25 output(f5); 26 cout << endl; 27 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << 28 f5.get_down() << endl; 29 cout << "f1 + f2 = "; 30 output(add(f1, f2)); 31 cout << endl; 32 cout << "f1 - f2 = "; 33 output(sub(f1, f2)); 34 cout << endl; 35 cout << "f1 * f2 = "; 36 output(mul(f1, f2)); 37 cout << endl; 38 cout << "f1 / f2 = "; 39 output(div(f1, f2)); 40 cout << endl; 41 cout << "f4 + f5 = "; 42 output(add(f4, f5)); 43 cout << endl; 44 } 45 void test2() { 46 Fraction f6(42, 55), f7(0, 3); 47 cout << "f6 = "; 48 output(f6); 49 cout << endl; 50 cout << "f7 = "; 51 output(f7); 52 cout << endl; 53 cout << "f6 / f7 = "; 54 output(div(f6, f7)); 55 cout << endl; 56 } 57 int main() { 58 cout << "测试1: Fraction类基础功能测试\n"; 59 test1(); 60 cout << "\n测试2: 分母为0测试: \n"; 61 test2(); 62 }
编译结果:
任务五:
account.h
1 #pragma once 2 #ifndef ACCOUNT_H 3 #define ACCOUNT_H 4 class SavingsAccount { 5 private: 6 int id; 7 double balance; 8 double rate; 9 int lastDate; 10 double accumulation; 11 static double total; 12 13 void record(int date, double amount); 14 double accumulate(int date)const { 15 return accumulation + balance * (date - lastDate); 16 } 17 18 19 public: 20 SavingsAccount(int date, int id, double rate); 21 int getId()const { 22 return id; 23 } 24 double getBanlance()const { 25 return balance; 26 } 27 double getRate()const { 28 return rate; 29 } 30 static double getTotal() { 31 return total; 32 } 33 void deposit(int date, double amcount); 34 void withdraw(int date, double amount); 35 void settle(int date); 36 void show()const; 37 }; 38 #endif
account.cpp
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 8 SavingsAccount::SavingsAccount(int date, int id, double rate):id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 9 cout << date << "\t#" << id << " is created" << endl; 10 } 11 12 void SavingsAccount::record(int date, double amount) { 13 accumulation = accumulate(date); 14 lastDate = date; 15 amount = floor(amount * 100 + 0.5) / 100; 16 balance += amount; 17 total += amount; 18 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 19 } 20 21 void SavingsAccount::deposit(int date, double amount) { 22 record(date, amount); 23 } 24 25 void SavingsAccount::withdraw(int date, double amount) { 26 if (amount > getBanlance()) 27 cout << "Error: not enough money" << endl; 28 else 29 record(date, -amount); 30 } 31 32 void SavingsAccount::settle(int date) { 33 double interest = accumulate(date) * rate / 365; 34 if (interest != 0) 35 record(date, interest); 36 accumulation = 0; 37 } 38 39 void SavingsAccount::show()const { 40 cout << "#" << id << "\tBalance:" << balance; 41 }
task5.cpp
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 SavingsAccount sa0(1, 21325302, 0.015); 6 SavingsAccount 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 16 sa0.show(); cout << endl; 17 sa1.show(); cout << endl; 18 cout << "Total:" << SavingsAccount::getTotal() << endl; 19 return 0; 20 }
编译结果:
结论:
此程序未考虑到有输入变量为0的情况,
变量名称遵循驼峰命名法
实验总结:
1.利用多文件的方法来书写程序,程序的可读性更高,更为清楚简洁,有利于去实现一些要求较多的项目。同时这也需要在.h文件中明确自己需要的。
2.变量名不是越短越好,而是要清楚明了。