实验二
任务一:
1 t.h 2 #pragma once 3 4 #include <string> 5 6 // 类T: 声明 7 class T { 8 // 对象属性、方法 9 public: 10 T(int x = 0, int y = 0); // 普通构造函数 11 T(const T &t); // 复制构造函数 12 T(T &&t); // 移动构造函数 13 ~T(); // 析构函数 14 15 void adjust(int ratio); // 按系数成倍调整数据 16 void display() const; // 以(m1, m2)形式显示T类对象信息 17 18 private: 19 int m1, m2; 20 21 // 类属性、方法 22 public: 23 static int get_cnt(); // 显示当前T类对象总数 24 25 public: 26 static const std::string doc; // 类T的描述信息 27 static const int max_cnt; // 类T对象上限 28 29 private: 30 static int cnt; // 当前T类对象数目 31 32 // 类T友元函数声明 33 friend void func(); 34 }; 35 36 // 普通函数声明 37 void func(); 38 39 t.cpp 40 // 类T: 实现 41 // 普通函数实现 42 43 #include "t.h" 44 #include <iostream> 45 #include <string> 46 47 using std::cout; 48 using std::endl; 49 using std::string; 50 51 // static成员数据类外初始化 52 const std::string T::doc{"a simple class sample"}; 53 const int T::max_cnt = 999; 54 int T::cnt = 0; 55 56 57 // 对象方法 58 T::T(int x, int y): m1{x}, m2{y} { 59 ++cnt; 60 cout << "T constructor called.\n"; 61 } 62 63 T::T(const T &t): m1{t.m1}, m2{t.m2} { 64 ++cnt; 65 cout << "T copy constructor called.\n"; 66 } 67 68 T::T(T &&t): m1{t.m1}, m2{t.m2} { 69 ++cnt; 70 cout << "T move constructor called.\n"; 71 } 72 73 T::~T() { 74 --cnt; 75 cout << "T destructor called.\n"; 76 } 77 78 void T::adjust(int ratio) { 79 m1 *= ratio; 80 m2 *= ratio; 81 } 82 83 void T::display() const { 84 cout << "(" << m1 << ", " << m2 << ")" ; 85 } 86 87 // 类方法 88 int T::get_cnt() { 89 return cnt; 90 } 91 92 // 友元 93 void func() { 94 T t5(42); 95 t5.m2 = 2049; 96 cout << "t5 = "; t5.display(); cout << endl; 97 } 98 99 task1.cpp 100 #include "t.h" 101 #include <iostream> 102 103 using std::cout; 104 using std::endl; 105 106 void test(); 107 108 int main() { 109 test(); 110 cout << "\nmain: \n"; 111 cout << "T objects'current count: " << T::get_cnt() << endl; 112 } 113 114 void test() { 115 cout << "test class T: \n"; 116 cout << "T info: " << T::doc << endl; 117 cout << "T objects'max count: " << T::max_cnt << endl; 118 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 119 120 121 T t1; 122 cout << "t1 = "; t1.display(); cout << endl; 123 124 T t2(3, 4); 125 cout << "t2 = "; t2.display(); cout << endl; 126 127 T t3(t2); 128 t3.adjust(2); 129 cout << "t3 = "; t3.display(); cout << endl; 130 131 T t4(std::move(t2)); 132 cout << "t3 = "; t4.display(); cout << endl; 133 134 cout << "T objects'current count: " << T::get_cnt() << endl; 135 136 func(); 137 }
结果截图:
问题1:
t.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉line36,重
新编译,是否能正确运行。如果能,回答说明可以去掉line36。如果不能,以截图形式给出编译报
错信息,分析可能的原因。
答:不能,友元函数func没有被单独声明。
问题2:
t.h中,line9-12给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析构函数
的调用时机。
答:普通构造函数:对m1,m2赋值。在每次创建新对象时被调用, 用来初始化对象。
复制构造函数:复制另一个对象的m1,m2值给当前对象的m1,m2。在 T t3(t2);这一句被调用。
移动构造函数:在T t4(std::move(t2));这一句被调用。
析构函数:对象被删除前自动调用,依次是t1,t2,t3,t4的析构函数被调用,最后是友元函数func的析构函数被调用。
问题3:
t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。
答:可以。
任务二:
Complex.h
1 #pragma once 2 #include<vector> 3 #include<string> 4 #include <iostream> 5 6 class Complex{ 7 public: 8 Complex (double a=0,double b=0); 9 Complex (const Complex &other); 10 add(const Complex &other); 11 private: 12 double real;//实部 13 double imag;//虚部 14 public: 15 static const std::string doc; 16 double get_real() const; 17 double get_imag() const; 18 friend bool is_equal(const Complex c1,const Complex c2); 19 friend bool is_not_equal(const Complex c1,const Complex c2); 20 friend Complex add(const Complex &c1,const Complex &c2); 21 friend void output(const Complex &c); 22 friend double abs(const Complex c); 23 ~Complex(); 24 };
Complex.cpp
1 #include "Complex.h" 2 #include<cmath> 3 #include <iostream> 4 #include<string> 5 using std::cout; 6 using std::endl; 7 using std::boolalpha; 8 9 Complex::Complex(double a,double b):real{a},imag{b} {} 10 11 Complex::Complex(const Complex &other) 12 { 13 real=other.real;imag=other.imag; 14 } 15 16 const std::string Complex::doc{"a simplified complex class"}; 17 18 double Complex::get_real() const{return real;} 19 20 double Complex::get_imag() const{return imag;} 21 22 bool is_equal(const Complex c1,const Complex c2) 23 { 24 if(c1.real==c2.real&&c1.imag==c2.imag) 25 return 1; 26 else 27 return 0; 28 } 29 30 bool is_not_equal(const Complex c1,const Complex c2) 31 { 32 if(c1.real!=c2.real||c1.imag!=c2.imag) 33 return 1; 34 else 35 return 0; 36 } 37 38 Complex::add(const Complex &other) 39 { 40 real=other.real+real; 41 imag=other.imag+imag; 42 } 43 44 Complex add(const Complex &c1,const Complex &c2) 45 { 46 Complex sum(0,0); 47 sum.real=c1.real+c2.real; 48 sum.imag=c1.imag+c2.imag; 49 return sum; 50 } 51 void output(const Complex &c) 52 { 53 cout<<c.real<<"+"<<c.imag<<"i"<<endl; 54 } 55 56 double abs(const Complex c) 57 { 58 double h; 59 h=sqrt(c.real*c.real+c.imag*c.imag); 60 return h; 61 } 62 63 Complex::~Complex() {}
task 2.cpp
1 #include "Complex.h" 2 #include <iostream> 3 #include <cmath> 4 5 using std::cout; 6 using std::endl; 7 using std::boolalpha; 8 9 void test() { 10 cout << "类成员测试: " << endl; 11 cout << Complex::doc << endl; 12 13 cout << endl; 14 15 cout << "Complex对象测试: " << endl; 16 Complex c1; 17 Complex c2(3, -4); 18 const Complex c3(3.5); 19 Complex c4(c3); 20 21 cout << "c1 = "; output(c1); cout << endl; 22 cout << "c2 = "; output(c2); cout << endl; 23 cout << "c3 = "; output(c3); cout << endl; 24 cout << "c4 = "; output(c4); cout << endl; 25 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 26 27 cout << endl; 28 29 cout << "复数运算测试: " << endl; 30 cout << "abs(c2) = " << abs(c2) << endl; 31 c1.add(c2); 32 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 33 cout << boolalpha; 34 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 35 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 36 c4 = add(c2, c3); 37 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 38 }
任务三:
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 << "标准库模板类complex测试: " << 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 }
使用了接口abs,运算功能+,判断功能==和!=
任务四:
fraction.h
1 #pragma once 2 #include<string> 3 #include<iostream> 4 5 using std::string; 6 7 class Fraction{ 8 public: 9 static const string doc; 10 Fraction(int a,int b=1); 11 Fraction(const Fraction &f); 12 ~Fraction(); 13 int get_up() const; 14 int get_down() const; 15 Fraction negative(); 16 private: 17 int up,down; 18 public: 19 friend void output(Fraction a); 20 friend Fraction add(const Fraction f1,const Fraction f2); 21 friend Fraction sub(const Fraction f1,const Fraction f2); 22 friend Fraction mul(const Fraction f1,const Fraction f2); 23 friend Fraction div(const Fraction f1,const Fraction f2); 24 }; 25 26 void output(Fraction a); 27 Fraction add(const Fraction f1,const Fraction f2); 28 Fraction sub(const Fraction f1,const Fraction f2); 29 Fraction mul(const Fraction f1,const Fraction f2); 30 Fraction div(const Fraction f1,const Fraction f2);
fraction.cpp
1 #include "Fraction.h" 2 #include<iostream> 3 #include<string> 4 #include<cmath> 5 6 using std::cout; 7 using std::endl; 8 using std::string; 9 10 const string Fraction::doc="Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 11 12 Fraction::~Fraction() {} 13 14 Fraction::Fraction(int a,int b):up{a},down{b} {} 15 16 Fraction::Fraction(const Fraction &f){ 17 up=f.up; 18 down=f.down; 19 } 20 21 int Fraction::get_up() const{ 22 return up; 23 } 24 25 int Fraction::get_down() const{ 26 return down; 27 } 28 29 Fraction Fraction::negative(){ 30 Fraction f(0,0); 31 f.up=-up; 32 f.down=down; 33 return f; 34 } 35 36 void output(Fraction a){ 37 int i,u=a.up,d=a.down; 38 for(i=2;i<=9;i++){ 39 if(u%i==0&&d%i==0){ 40 u/=i; 41 d/=i; 42 } 43 } 44 if((u<0&&d<0)||(u>0&&d<0)) 45 { 46 u=-u;d=-d; 47 } 48 if(d==0){ 49 cout<<"分母不能为0。"; 50 } 51 else if(u==0) 52 cout<<"0"; 53 else if(d==1) 54 cout<<u; 55 else 56 cout<<u<<"/"<<d; 57 } 58 59 Fraction add(const Fraction f1,const Fraction f2){ 60 Fraction f(0,0); 61 f.down=f1.down*f2.down; 62 f.up=f1.up*f2.down+f2.up*f1.down; 63 return f; 64 } 65 66 Fraction sub(const Fraction f1,const Fraction f2){ 67 Fraction f(0,0); 68 f.down=f1.down*f2.down; 69 f.up=f1.up*f2.down-f2.up*f1.down; 70 return f; 71 } 72 73 Fraction mul(const Fraction f1,const Fraction f2){ 74 Fraction f(0,0); 75 f.up=f1.up*f2.up; 76 f.down=f1.down*f2.down; 77 return f; 78 } 79 80 Fraction div(const Fraction f1,const Fraction f2){ 81 Fraction f(0,0); 82 f.up=f1.up*f2.down; 83 f.down=f1.down*f2.up; 84 return f; 85 }
task 4.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test1() { 8 cout << "Fraction类测试: " << endl; 9 cout << Fraction::doc << endl << endl; 10 11 Fraction f1(5); 12 Fraction f2(3, -4), f3(-18, 12); 13 Fraction f4(f3); 14 cout << "f1 = "; output(f1); cout << endl; 15 cout << "f2 = "; output(f2); cout << endl; 16 cout << "f3 = "; output(f3); cout << endl; 17 cout << "f4 = "; output(f4); cout << endl; 18 19 Fraction f5(f4.negative()); 20 cout << "f5 = "; output(f5); cout << endl; 21 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 22 23 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 24 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 25 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 26 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 27 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 28 } 29 30 void test2() { 31 Fraction f6(42, 55), f7(0, 3); 32 cout << "f6 = "; output(f6); cout << endl; 33 cout << "f7 = "; output(f7); cout << endl; 34 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 35 } 36 37 int main() { 38 cout << "测试1:Fraction类基础功能测试\n"; 39 test1(); 40 41 cout << "\n测试2:分母为0测试: \n"; 42 test2(); 43 }
任务五:
account.h
1 #include<iostream> 2 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 12 void record(int date,double amount); 13 double accumulate(int date) const{ 14 return accumulation+balance*(date-lastDate); 15 } 16 17 public: 18 SavingsAccount(int date,int id,double rate); 19 int getId() const {return id;} 20 double getBalance() const{return balance;} 21 double getRate() const{return rate;} 22 static double getTotal(){return total;} 23 void deposit(int rate,double amount); 24 void withdraw(int rate,double amount); 25 void settle(int date); 26 void show() const; 27 28 };
account.cpp
1 #include "account.h" 2 #include<cmath> 3 #include<iostream> 4 5 using std::cout; 6 using std::endl; 7 8 double SavingsAccount::total=0; 9 10 SavingsAccount::SavingsAccount(int date,int id,double rate) 11 :id(id),balance{0},rate{rate},lastDate{date},accumulation{0}{ 12 cout<<date<<"\t#"<<id<<"is created"<<endl; 13 } 14 15 void SavingsAccount::record(int date,double amount){ 16 accumulation=accumulate(date); 17 lastDate=date; 18 amount=floor(amount*100+0.5)/100; 19 balance+=amount; 20 total+=amount; 21 cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; 22 } 23 24 void SavingsAccount::deposit(int date,double amount){ 25 record(date,amount); 26 } 27 28 void SavingsAccount::withdraw(int date,double amount){ 29 if(amount>getBalance()){ 30 cout<<"Erroe:not enough money"<<endl; 31 } 32 else{ 33 record(date,-amount); 34 } 35 } 36 37 void SavingsAccount::settle(int date){ 38 double interest=accumulate(date)*rate/365; 39 if(interest!=0){ 40 record(date,interest); 41 } 42 accumulation=0; 43 } 44 45 void SavingsAccount::show() const{ 46 cout<<"#"<<id<<"\tBalance:"<<balance; 47 }
5_11.cpp
1 #include "account.h" 2 #include<iostream> 3 4 using namespace std; 5 6 int main(){ 7 SavingsAccount sa0(1,21325302,0.015); 8 SavingsAccount sa1(1,58320212,0.015); 9 10 sa0.deposit(5,5000); 11 sa1.deposit(25,10000); 12 sa0.deposit(45,5500); 13 sa1.withdraw(60,4000); 14 15 sa0.settle(90); 16 sa1.settle(90); 17 18 sa0.show();cout<<endl; 19 sa1.show();cout<<endl; 20 21 cout<<"Total:"<<SavingsAccount::getTotal()<<endl; 22 23 return 0; 24 }