实验2
实验任务1:
源码及截图:
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); // 复制构造函数
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 }
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:
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 }
29 static int cnt; // 当前T类对象数目 30 31 // 类T友元函数声明 32 friend void func(); 33 }; 34 35 // 普通函数声明 36 void func();
问题1:
类外的普通函数没有声明导致报错
问题2:
普通构造函数:通过读入的数值构造一个类
复制构造函数:将一个类的信息复制到另一个类里
移动构造函数:将一个类的信息移动到另一个类里,并释放原类的内存空间
问题3:
不能通过编译
实验任务2:
源码及截图:
1 #pragma once 2 #include<string> 3 class Complex{ 4 private: 5 double real; 6 double imag; 7 public: 8 static const std::string doc; 9 Complex(double r=0,double i=0); 10 Complex(const Complex &c); 11 double get_real()const; 12 double get_imag()const; 13 void add(const Complex &c); 14 friend Complex add(const Complex &c1,const Complex &c2); 15 friend bool is_equal(const Complex &c1,const Complex &c2); 16 friend bool is_not_equal(const Complex &c1,const Complex &c2); 17 friend double abs(const Complex &c); 18 friend void output(const Complex &c); 19 }; 20 Complex add(const Complex &c1,const Complex &c2); 21 bool is_equal(const Complex &c1,const Complex &c2); 22 bool is_not_equal(const Complex &c1,const Complex &c2); 23 double abs(const Complex &c); 24 void output(const Complex &c);
1 #include"Complex.h" 2 #include<iostream> 3 #include<string> 4 #include<math.h> 5 using std::cout; 6 using std::endl; 7 using std::string; 8 const std::string Complex::doc{"a simplified Complex class"}; 9 10 Complex::Complex(double r,double i):real{r},imag{i}{ 11 } 12 Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{ 13 } 14 double Complex::get_real()const{ 15 return real; 16 } 17 double Complex::get_imag()const{ 18 return imag; 19 } 20 void Complex::add(const Complex &c){ 21 real+=c.real; 22 imag+=c.imag; 23 24 } 25 Complex add(const Complex &c1,const Complex &c2){ 26 return Complex(c1.real+c2.real,c1.imag+c2.imag); 27 } 28 bool is_equal(const Complex &c1,const Complex &c2){ 29 if(c1.real==c2.real&&c1.imag==c2.imag){ 30 return true; 31 } 32 else 33 return false; 34 } 35 bool is_not_equal(const Complex &c1,const Complex &c2){ 36 if(c1.real==c2.real&&c1.imag==c2.imag){ 37 return false; 38 } 39 else 40 return true; 41 } 42 void output(const Complex &c){ 43 if(c.imag>=0){ 44 cout<<c.real<<"+"<<c.imag<<"i"<<endl; 45 } 46 else 47 cout<<c.real<<c.imag<<"i"<<endl; 48 } 49 double abs(const Complex &c){ 50 double t; 51 t=sqrt(pow(c.real,2)+pow(c.imag,2)); 52 return t; 53 }
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 } 33 34 int main() { 35 test(); 36 }
实验任务4:
源码及截图:
1 #include<iostream> 2 #include<string> 3 #include<math.h> 4 using namespace std; 5 class Fraction{ 6 public: 7 static const std::string doc; 8 Fraction(int r,int i=1){ 9 up=r; 10 down=i; 11 if(down<0){ 12 up=-1*up; 13 down=-1*down; 14 } 15 } 16 Fraction(const Fraction &c){ 17 up=c.up; 18 down=c.down; 19 } 20 int get_up(){ 21 return up; 22 } 23 int get_down(){ 24 return down; 25 } 26 Fraction negative(){ 27 return Fraction(-1*up,down); 28 } 29 friend void output(const Fraction &c); 30 31 friend Fraction add(const Fraction &f1,const Fraction &f2); 32 33 friend Fraction sub(const Fraction &f1,const Fraction &f2); 34 35 friend Fraction mul(const Fraction &f1,const Fraction &f2); 36 37 friend Fraction div(const Fraction &f1,const Fraction &f2); 38 39 private: 40 int up; 41 int down; 42 43 }; 44 const std::string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."}; 45 void output(const Fraction &c){ 46 int i,n,m; 47 if(c.down==0){ 48 cout<<"分母不能为0"; 49 } 50 else if(c.up==0){ 51 cout<<0; 52 } 53 54 else if(c.down==1){ 55 cout<<c.up<<endl; 56 } 57 else{ 58 59 for(i=2;i<=abs(c.up)&&i<=c.down;i++){ 60 if(abs(c.up)%i==0&&c.down%i==0){ 61 n=c.up/i; 62 m=c.down/i; 63 } 64 } 65 if(i<abs(c.up)) 66 cout<<n<<"/"<<m; 67 else 68 cout<<c.up<<"/"<<c.down; 69 } 70 } 71 Fraction add(const Fraction &f1,const Fraction &f2){ 72 return Fraction(f1.up*f2.down+f2.up*f1.down,f1.down*f2.down); 73 } 74 Fraction sub(const Fraction &f1,const Fraction &f2){ 75 return Fraction(f1.up*f2.down-f2.up*f1.down,f1.down*f2.down); 76 } 77 Fraction mul(const Fraction &f1,const Fraction &f2){ 78 return Fraction(f1.up*f2.up,f1.down*f2.down); 79 } 80 Fraction div(const Fraction &f1,const Fraction &f2){ 81 return Fraction(f1.up*f2.down,f1.down*f2.up); 82 } 83 84 85 void test1() { 86 cout << "Fraction类测试: " << endl; 87 cout << Fraction::doc << endl << endl; 88 89 Fraction f1(5); 90 Fraction f2(3, -4), f3(-18, 12); 91 Fraction f4(f3); 92 cout << "f1 = "; output(f1); cout << endl; 93 cout << "f2 = "; output(f2); cout << endl; 94 cout << "f3 = "; output(f3); cout << endl; 95 cout << "f4 = "; output(f4); cout << endl; 96 97 Fraction f5(f4.negative()); 98 cout << "f5 = "; output(f5); cout << endl; 99 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 100 101 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 102 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 103 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 104 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 105 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 106 } 107 108 void test2() { 109 Fraction f6(42, 55), f7(0, 3); 110 cout << "f6 = "; output(f6); cout << endl; 111 cout << "f7 = "; output(f7); cout << endl; 112 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 113 } 114 115 int main() { 116 cout << "测试1: Fraction类基础功能测试\n"; 117 test1(); 118 119 cout << "\n测试2: 分母为0测试: \n"; 120 test2(); 121 }
实验任务5:
源码及截图:
1 #include<cmath> 2 #include<iostream> 3 4 using namespace std; 5 #ifndef _ACCOUNT_H_ 6 7 #define _ACCOUNT_H_ 8 9 class SavingsAccount //储蓄账户类 10 11 { 12 13 private: 14 15 int id; //账号 16 17 double balance; //余额 18 19 double rate; //存款的年利率 20 21 int lastDate; //上次变更余额的日期 22 23 double accumulation; //余额按日累加之和 24 25 static double total; //所有账户的总金额 26 27 //记录一笔账,date为日期,desc为说明 28 29 void record(int date, double amount); 30 31 //获得到指定日期为止的存款金额按日累积值 32 33 double accumulate(int date) const 34 35 { 36 37 return accumulation + balance*(date - lastDate); 38 39 } 40 41 public: 42 43 //构造函数 44 45 SavingsAccount(int date, int id, double rate); 46 47 int getId() const { return id; } 48 49 double getBalance() const { return balance; } 50 51 double getRate() const { return rate; } 52 53 static double getTotal() { return total; } 54 55 void deposit(int date, double amount); //存入现金 56 57 void withdraw(int date, double amount); //取出现金 58 59 //结算利息,每年1月1日调用一次该函数 60 61 void settle(int date); 62 63 //显示账户信息 64 65 void show() const; 66 67 }; 68 69 #endif//_ACCOUNT_H_ 70 71 72 73 74 75 76 using namespace std; 77 78 double SavingsAccount::total = 0; 79 80 //SavingsAccount类相关成员函数的实现 81 82 SavingsAccount::SavingsAccount(int date, int id, double rate) 83 84 : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) 85 86 { 87 88 cout << date << "\t#" << id << " is created" << endl; 89 90 } 91 92 void SavingsAccount::record(int date, double amount) 93 94 { 95 96 accumulation = accumulate(date); 97 98 lastDate = date; 99 100 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 101 102 balance += amount; 103 104 total += amount; 105 106 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 107 108 } 109 110 void SavingsAccount::deposit(int date, double amount) 111 112 { 113 114 record(date, amount); 115 116 } 117 118 void SavingsAccount::withdraw(int date, double amount) 119 120 { 121 122 if (amount>getBalance()) 123 124 cout << "Error:not enough money" << endl; 125 126 else 127 128 record(date, -amount); 129 130 } 131 132 void SavingsAccount::settle(int date) 133 134 { 135 136 double interest = accumulate(date)*rate / 365; //计算年息 137 138 if (interest != 0) 139 140 record(date, interest); 141 142 accumulation = 0; 143 144 } 145 146 void SavingsAccount::show() const 147 148 { 149 150 cout << "#" << id << "\tBalance:" << balance; 151 152 } 153 int main() 154 155 { 156 157 //建立几个账户 158 159 SavingsAccount sa0(1, 21325302, 0.015); 160 161 SavingsAccount sa1(1, 58320212, 0.015); 162 163 //几笔账目 164 165 sa0.deposit(5, 5000); 166 167 sa1.deposit(25, 10000); 168 169 sa0.deposit(45, 5500); 170 171 sa1.withdraw(60, 4000); 172 173 //开户后第90天到了银行的计息日,结算所有账户的年息 174 175 sa0.settle(90); 176 177 sa1.settle(90); 178 179 //输出各个账户信息 180 181 sa0.show(); cout << endl; 182 183 sa1.show(); cout << endl; 184 185 cout << "Total:" << SavingsAccount::getTotal() << endl; 186 187 return 0; 188 189 }