实验任务1:
实验代码:
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();
t.cpp:
1 // 类T: 实现 2 // 普通函数实现 3 4 #include "t.h" 5 #include <iostream> 6 #include <string> 7 8 using std::cout; 9 using std::endl; 10 using std::string; 11 12 // static成员数据类外初始化 13 const std::string T::doc{"a simple class sample"}; 14 const int T::max_cnt = 999; 15 int T::cnt = 0; 16 17 18 // 对象方法 19 T::T(int x, int y): m1{x}, m2{y} { 20 ++cnt; 21 cout << "T constructor called.\n"; 22 } 23 24 T::T(const T &t): m1{t.m1}, m2{t.m2} { 25 ++cnt; 26 cout << "T copy constructor called.\n"; 27 } 28 29 T::T(T &&t): m1{t.m1}, m2{t.m2} { 30 ++cnt; 31 cout << "T move constructor called.\n"; 32 } 33 34 T::~T() { 35 --cnt; 36 cout << "T destructor called.\n"; 37 } 38 39 void T::adjust(int ratio) { 40 m1 *= ratio; 41 m2 *= ratio; 42 } 43 44 void T::display() const { 45 cout << "(" << m1 << ", " << m2 << ")" ; 46 } 47 48 // 类方法 49 int T::get_cnt() { 50 return cnt; 51 } 52 53 // 友元 54 void func() { 55 T t5(42); 56 t5.m2 = 2049; 57 cout << "t5 = "; t5.display(); cout << endl; 58 }
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:
问题1:不行。
实验截图2:
去掉line36后,相当于只在类T中对func()进行了友元函数声明,而未在类T外进行其函数本身的声明。
问题2:1.普通构造函数:在对象被创建时利用特定的值构造对象,将对象初始化为一个特定的状态。在对象被创建时被自动调用;
2.复制构造函数:使用一个已经存在的对象,去初始化同类的一个新对象。
3.移动构造函数:直接挪用已有右值对象的成员资源,配合move函数使用完成新对象移动构造。
4.析构函数:用来完成对象被删除前的一些清理工作。在对象的生存期即将结束的时刻被自动调用;
5.默认构造函数:调用时无须提供参数的普通构造函数。
问题3:无法正确编译运行。
实验任务2:
实验代码:
complex.h:
1 #ifndef COMPLEX_H 2 #define COMPLEX_H 3 4 #include <iostream> 5 #include <string> 6 #include <cmath> 7 8 class Complex { 9 public: 10 static const std::string doc; 11 Complex(); 12 Complex(double r); 13 Complex(double r, double i); 14 Complex(const Complex& other); 15 16 double get_real() const; 17 double get_imag() const; 18 void add(const Complex& other); 19 20 friend Complex add(const Complex& c1, const Complex& c2); 21 friend bool is_equal(const Complex& c1,const Complex& c2); 22 friend bool is_not_equal(const Complex& c1,const Complex& c2); 23 24 friend double abs(const Complex& c); 25 friend void output(const Complex& c); 26 private: 27 double real; 28 double imag; 29 }; 30 31 #endif
complex.cpp:
1 #include "Complex.h" 2 3 const std::string Complex::doc = "a simplified Complex class"; 4 5 Complex::Complex() : real{0}, imag{0} {} 6 Complex::Complex(double r) : real{r}, imag{0} {} 7 Complex::Complex(double r, double i) : real{r}, imag{i} {} 8 Complex::Complex(const Complex& other) : real{other.real}, imag{other.imag} {} 9 10 double Complex::get_real() const { 11 return real; 12 } 13 14 double Complex::get_imag() const { 15 return imag; 16 } 17 18 void Complex::add(const Complex& other) { 19 real += other.real; 20 imag += other.imag; 21 } 22 23 Complex add(const Complex& c1, const Complex& c2) { 24 return Complex(c1.real + c2.real, c1.imag + c2.imag); 25 } 26 bool is_equal(const Complex& c1,const Complex& c2) { 27 return (c1.real == c2.real) && (c1.imag == c2.imag); 28 } 29 30 bool is_not_equal(const Complex& c1,const Complex& c2) { 31 return !is_equal(c1, c2); 32 } 33 34 void output(const Complex& c) { 35 if(c.imag < 0) 36 std::cout << c.real << " - " << abs(c.imag) << "i"; 37 else 38 std::cout << c.real << " + " << c.imag << "i"; 39 } 40 41 double abs(const Complex& c) { 42 return std::sqrt(c.real * c.real + c.imag * c.imag); 43 }
main.cpp:
1 #include "Complex.h" 2 3 using std::cout; 4 using std::endl; 5 using std::boolalpha; 6 7 void test() { 8 cout << "类成员测试: " << endl; 9 cout << Complex::doc << endl; 10 11 cout << endl; 12 13 cout << "Complex对象测试: " << endl; 14 Complex c1; 15 Complex c2(3, -4); 16 const Complex c3(3.5); 17 Complex c4(c3); 18 19 cout << "c1 = "; output(c1); cout << endl; 20 cout << "c2 = "; output(c2); cout << endl; 21 cout << "c3 = "; output(c3); cout << endl; 22 cout << "c4 = "; output(c4); cout << endl; 23 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 24 25 cout << endl; 26 27 cout << "复数运算测试: " << endl; 28 cout << "abs(c2) = " << abs(c2) << endl; 29 c1.add(c2); 30 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 31 cout << boolalpha; 32 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 33 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 34 c4 = add(c2, c3); 35 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 36 } 37 38 int main() { 39 test(); 40 }
实验截图:
实验任务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 } 33 34 int main() { 35 test(); 36 }
实验截图:
实验任务4:
实验代码:
Fraction.h:
1 #ifndef FRACTION H 2 #define FRACTION H 3 4 #include <iostream> 5 #include <string> 6 #include <cmath> 7 8 class Fraction { 9 public: 10 static const std::string doc; 11 Fraction(int u); 12 Fraction(int u, int d); 13 Fraction(const Fraction& other); 14 15 int get_up() const; 16 int get_down() const; 17 Fraction negative() const; 18 void reduce(); 19 20 friend void output(const Fraction& f); 21 friend Fraction add(const Fraction& f1, const Fraction& f2); 22 friend Fraction sub(const Fraction& f1, const Fraction& f2); 23 friend Fraction mul(const Fraction& f1, const Fraction& f2); 24 friend Fraction div(const Fraction& f1, const Fraction& f2); 25 private: 26 int up; 27 int down; 28 }; 29 30 #endif
Fraction.cpp:
1 #include "Fraction.h" 2 3 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 4 5 Fraction::Fraction(int u): up{u}, down{1} {} 6 Fraction::Fraction(int u, int d): up{u}, down{d} { 7 if(down == 0) { 8 std::cout << "分母不能为0"; 9 } 10 reduce(); 11 } 12 Fraction::Fraction(const Fraction& other): up{other.up}, down{other.down} {} 13 14 int gcd(int a, int b) { 15 int r; 16 while (b > 0){ 17 r = a % b; 18 a = b; 19 b = r; 20 } 21 return a; 22 } 23 24 int Fraction::get_up() const { 25 return up; 26 } 27 28 int Fraction::get_down() const { 29 return down; 30 } 31 32 void Fraction::reduce() { 33 int x = gcd(abs(up), abs(down)); 34 up /= x; 35 down /= x; 36 } 37 38 Fraction Fraction::negative() const { 39 return Fraction(-up, down); 40 } 41 42 void output(const Fraction& f) { 43 if(f.up * f.down < 0) { 44 if (f.up % f.down == 0) 45 std::cout << "-" << abs(f.up) / abs(f.down); 46 else 47 std::cout << "-" << abs(f.up) << "/" << abs(f.down); 48 } 49 else { 50 if(f.up % f.down == 0) 51 std::cout << abs(f.up) / abs(f.down); 52 else 53 std::cout << abs(f.up) << "/" << abs(f.down); 54 } 55 } 56 57 Fraction add(const Fraction& f1, const Fraction& f2) { 58 return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down); 59 } 60 61 Fraction sub(const Fraction& f1, const Fraction& f2) { 62 return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down); 63 } 64 65 Fraction mul(const Fraction& f1, const Fraction& f2) { 66 return Fraction(f1.up * f2.up, f1.down * f2.down); 67 } 68 69 Fraction div(const Fraction& f1, const Fraction& f2) { 70 return Fraction(f1.up * f2.down, f1.down * f2.up); 71 }
main.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(f4.negative()); 21 cout << "f5 = "; output(f5); cout << endl; 22 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << 23 f5.get_down() << endl; 24 25 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 26 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 27 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 28 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 29 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 30 } 31 32 void test2() { 33 Fraction f6(42, 55), f7(0, 3); 34 cout << "f6 = "; output(f6); cout << endl; 35 cout << "f7 = "; output(f7); cout << endl; 36 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 37 } 38 39 int main() { 40 cout << "测试1: Fraction类基础功能测试\n"; 41 test1(); 42 43 cout << "\n测试2: 分母为0测试: \n"; 44 test2(); 45 }
实验截图:
实验任务5:
实验代码:
account.h:
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 class SavingsAccount { 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 SavingsAccount(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 24 void settle(int date); 25 void show() const; 26 }; 27 #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 void SavingsAccount::record(int date, double amount) { 12 accumulation = accumulate(date); 13 lastDate = date; 14 amount = floor(amount * 100 + 0.5) / 100; 15 balance += amount; 16 total += amount; 17 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 18 } 19 void SavingsAccount::deposit(int date, double amount) { 20 record(date, amount); 21 } 22 void SavingsAccount::withdraw(int date, double amount) { 23 if(amount > getBalance()) 24 cout << "Error: not enough money" << endl; 25 else 26 record(date, -amount); 27 } 28 void SavingsAccount::settle(int date) { 29 double interest = accumulate(date) * rate / 365; 30 if(interest != 0) record(date, interest); 31 accumulation = 0; 32 } 33 void SavingsAccount::show() const { 34 cout << "#" << id << "\tBalance: " << balance; 35 }
main.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 }
实验截图:
挺好的:)