实验2 类和对象_基础编程1
任务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
t.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉line36,重
新编译,编译器报错,报错信息如下:
可能的原因:当只在类内部声明友元函数时,编译器无法找到函数的声明,必须在类外部声明。
问题2
t.h中,line9-12给出了各种构造函数、析构函数,总结功能及调用时机。
line9:普通构造函数(带参数) 功能:在对象被创建时利用特定值构造对象,将对象初始化为一个特定的状态(传递参数或使用默认参数)。调用时机:对象被创建时。
line10:复制构造函数 功能:使用一个已经存在的对象(由复制构造函数参数&t决定),去初始化同类的一个新对象。调用时机:当一个对象以另一个对象为初始化参数进行初始化时。
line11:移动构造函数 功能:将已有对象的值移动给同类的一个新对象。调用时机:当临时对象用于初始化新对象时。
line12:析构函数 功能:清理对象占用的资源。调用时机:对象的生存期即将结束时(对象作用域结束)。
问题3
t.cpp中,line13-15,调整到t.h,重新编译,程序能正确编译运行。说明static成员数据可以在头文件中(类外)初始化。
任务2:
Complex.h
1 #pragma once 2 #include <string> 3 4 class Complex { 5 public: 6 Complex(); 7 Complex(double r = 0); 8 Complex(double r = 0, double i = 0); 9 Complex(const Complex& other); 10 ~ Complex(); 11 12 double get_real() const; 13 double get_imag() const; 14 void add(const Complex& other); 15 16 friend Complex add(const Complex& c1, const Complex& c2); 17 friend bool is_equal(const Complex& c1, const Complex& c2); 18 friend bool is_not_equal(const Complex& c1, const Complex& c2); 19 friend void output(const Complex& c); 20 friend double abs(const Complex& c); 21 22 private: 23 double real; 24 double imag; 25 26 public: 27 static const std::string doc; 28 29 };
Complex.cpp
1 #include "x.h" 2 #include <iostream> 3 #include <cmath> 4 #include <string> 5 6 Complex::Complex() : real(0), imag(0) {} 7 Complex::Complex(double r) : real(r), imag(0) {} 8 Complex::Complex(double r, double i) : real(r), imag(i) {} 9 Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {} 10 Complex::~ Complex() {} 11 12 double Complex::get_real() const { 13 return real; 14 } 15 16 double Complex::get_imag() const { 17 return imag; 18 } 19 20 void Complex::add(const Complex& other) { 21 real += other.get_real(); 22 imag += other.get_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 29 bool is_equal(const Complex& c1, const Complex& c2) { 30 return (c1.real == c2.real) && (c1.imag == c2.imag); 31 } 32 33 bool is_not_equal(const Complex& c1, const Complex& c2) { 34 return !is_equal(c1, c2); 35 } 36 37 void output(const Complex& c) { 38 std::cout << c.get_real() << (c.get_imag() >= 0 ? " + " : " - ") << std::fabs(c.get_imag()) << "i"; 39 } 40 41 double abs(const Complex& c) { 42 return std::sqrt(c.real * c.real + c.imag * c.imag); 43 } 44 45 const std::string Complex::doc {"a simplified complex class"};#include "x.h" 46 #include <iostream> 47 #include <cmath> 48 #include <string> 49 50 Complex::Complex() : real(0), imag(0) {} 51 Complex::Complex(double r) : real(r), imag(0) {} 52 Complex::Complex(double r, double i) : real(r), imag(i) {} 53 Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {} 54 Complex::~ Complex() {} 55 56 double Complex::get_real() const { 57 return real; 58 } 59 60 double Complex::get_imag() const { 61 return imag; 62 } 63 64 void Complex::add(const Complex& other) { 65 real += other.get_real(); 66 imag += other.get_imag(); 67 } 68 69 Complex add(const Complex& c1, const Complex& c2) { 70 return Complex(c1.real + c2.real, c1.imag + c2.imag); 71 } 72 73 bool is_equal(const Complex& c1, const Complex& c2) { 74 return (c1.real == c2.real) && (c1.imag == c2.imag); 75 } 76 77 bool is_not_equal(const Complex& c1, const Complex& c2) { 78 return !is_equal(c1, c2); 79 } 80 81 void output(const Complex& c) { 82 std::cout << c.get_real() << (c.get_imag() >= 0 ? " + " : " - ") << std::fabs(c.get_imag()) << "i"; 83 } 84 85 double abs(const Complex& c) { 86 return std::sqrt(c.real * c.real + c.imag * c.imag); 87 } 88 89 const std::string Complex::doc {"a simplified complex class"};#include "x.h" 90 #include <iostream> 91 #include <cmath> 92 #include <string> 93 94 Complex::Complex() : real(0), imag(0) {} 95 Complex::Complex(double r) : real(r), imag(0) {} 96 Complex::Complex(double r, double i) : real(r), imag(i) {} 97 Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {} 98 Complex::~ Complex() {} 99 100 double Complex::get_real() const { 101 return real; 102 } 103 104 double Complex::get_imag() const { 105 return imag; 106 } 107 108 void Complex::add(const Complex& other) { 109 real += other.get_real(); 110 imag += other.get_imag(); 111 } 112 113 Complex add(const Complex& c1, const Complex& c2) { 114 return Complex(c1.real + c2.real, c1.imag + c2.imag); 115 } 116 117 bool is_equal(const Complex& c1, const Complex& c2) { 118 return (c1.real == c2.real) && (c1.imag == c2.imag); 119 } 120 121 bool is_not_equal(const Complex& c1, const Complex& c2) { 122 return !is_equal(c1, c2); 123 } 124 125 void output(const Complex& c) { 126 std::cout << c.get_real() << (c.get_imag() >= 0 ? " + " : " - ") << std::fabs(c.get_imag()) << "i"; 127 } 128 129 double abs(const Complex& c) { 130 return std::sqrt(c.real * c.real + c.imag * c.imag); 131 } 132 133 const std::string Complex::doc {"a simplified complex class"};
task2.cpp
1 #include "x.h" 2 #include <iostream> 3 #include <string> 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 cout << endl; 13 cout << "Complex对象测试: " << endl; 14 Complex c1; 15 Complex c2(3, -4); 16 const Complex c3(3.5); 17 Complex c4(c3); 18 cout << "c1 = "; output(c1); cout << endl; 19 cout << "c2 = "; output(c2); cout << endl; 20 cout << "c3 = "; output(c3); cout << endl; 21 cout << "c4 = "; output(c4); cout << endl; 22 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() 23 << endl; 24 cout << endl; 25 cout << "复数运算测试: " << endl; 26 cout << "abs(c2) = " << abs(c2) << endl; 27 c1.add(c2); 28 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 29 cout << boolalpha; 30 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 31 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 32 c4 = add(c2, c3); 33 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 34 } 35 int main() { 36 test(); 37 }#include "x.h" 38 #include <iostream> 39 #include <string> 40 41 using std::cout; 42 using std::endl; 43 using std::boolalpha; 44 45 void test() { 46 cout << "类成员测试: " << endl; 47 cout << Complex::doc << endl; 48 cout << endl; 49 cout << "Complex对象测试: " << endl; 50 Complex c1; 51 Complex c2(3, -4); 52 const Complex c3(3.5); 53 Complex c4(c3); 54 cout << "c1 = "; output(c1); cout << endl; 55 cout << "c2 = "; output(c2); cout << endl; 56 cout << "c3 = "; output(c3); cout << endl; 57 cout << "c4 = "; output(c4); cout << endl; 58 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() 59 << endl; 60 cout << endl; 61 cout << "复数运算测试: " << endl; 62 cout << "abs(c2) = " << abs(c2) << endl; 63 c1.add(c2); 64 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 65 cout << boolalpha; 66 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 67 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 68 c4 = add(c2, c3); 69 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 70 } 71 int main() { 72 test(); 73 }
运行测试结果截图

总结
1.注意string在namespace标准库中,用std::访问。
2.static成员数据在类外进行初始化。
3.适当使用const保护数据。
任务3:
task3.cpp
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 }
运行测试结果截图
总结
1.注意标准模板类complex在namespace标准库中,用std::访问。
2.模板类complex可以直接通过.real() .imag()分别返回对象的实部和虚部;直接通过“+” “-”进行复数的加法减法运算;abs()为求模运算;直接通过“==”判等
3.复数作为常见的一种数,通过直接使用模板类complex可以无需自己定义复数类使用较方便。
任务4:
Fraction.h
1 #pragma once 2 #include <string> 3 4 using std::string; 5 6 class Fraction { 7 8 public: 9 Fraction(int u, int d = 1); 10 Fraction(const Fraction &other); 11 ~Fraction(); 12 13 int get_up() const; 14 int get_down() const; 15 Fraction negative() const; 16 17 friend void output(const Fraction &f); 18 friend Fraction add(const Fraction &f1, const Fraction &f2); 19 friend Fraction sub(const Fraction &f1, const Fraction &f2); 20 friend Fraction mul(const Fraction &f1, const Fraction &f2); 21 friend Fraction div(const Fraction &f1, const Fraction &f2); 22 23 private: 24 int up, down; 25 26 public: 27 static const string doc; 28 29 };
Fraction.cpp
1 #include "Fraction.h" 2 #include <iostream> 3 #include <string> 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 const string Fraction::doc {"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."}; 10 11 Fraction::Fraction(int u, int d): up{u}, down{d} { 12 } 13 14 Fraction::Fraction(const Fraction& other): up{other.up}, down{other.down} {} 15 16 Fraction::~Fraction() {} 17 18 int Fraction::get_up() const{ 19 int a = up, b = down; 20 int u = up, d = down; 21 while (b != 0) { 22 int temp = b; 23 b = a % b; 24 a = temp; 25 } 26 int gcd = a; 27 u /= gcd; 28 d /= gcd; 29 if (d < 0) { 30 d = -d; 31 u = -u; 32 } 33 return u; 34 } 35 36 int Fraction::get_down() const{ 37 int a = up, b = down; 38 int u = up, d = down; 39 while (b != 0) { 40 int temp = b; 41 b = a % b; 42 a = temp; 43 } 44 int gcd = a; 45 u /= gcd; 46 d /= gcd; 47 if (d < 0) { 48 d = -d; 49 u = -u; 50 } 51 return d; 52 } 53 54 Fraction Fraction::negative() const{ 55 return Fraction(-up, down); 56 } 57 58 void output(const Fraction &f) { 59 if(f.down == 0) { 60 cout << "分母不能为0"; 61 return; 62 } 63 64 int a = f.up, b = f.down; 65 int u = f.up, d = f.down; 66 while (b != 0) { 67 int temp = b; 68 b = a % b; 69 a = temp; 70 } 71 int gcd = a; 72 u /= gcd; 73 d /= gcd; 74 if (d < 0) { 75 d = -d; 76 u = -u; 77 } 78 if(u == 0) 79 cout << 0; 80 else if(d == 1) 81 cout << u; 82 else 83 cout << u << "/" << d ; 84 } 85 86 Fraction add(const Fraction& f1, const Fraction& f2) { 87 return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down); 88 } 89 90 Fraction sub(const Fraction& f1, const Fraction& f2) { 91 return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down); 92 } 93 94 Fraction mul(const Fraction& f1, const Fraction& f2) { 95 return Fraction(f1.up * f2.up, f1.down * f2.down); 96 } 97 98 Fraction div(const Fraction& f1, const Fraction& f2) { 99 return Fraction(f1.up * f2.down, f1.down * f2.up); 100 }
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(f4.negative()); 21 cout << "f5 = "; output(f5); cout << endl; 22 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 23 24 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 25 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 26 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 27 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 28 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 29 } 30 31 void test2() { 32 Fraction f6(42, 55), f7(0, 3); 33 cout << "f6 = "; output(f6); cout << endl; 34 cout << "f7 = "; output(f7); cout << endl; 35 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 36 } 37 38 int main() { 39 cout << "测试1: Fraction类基础功能测试\n"; 40 test1(); 41 42 cout << "\n测试2: 分母为0测试: \n"; 43 test2(); 44 }
运行测试结果截图

总结
1.在设计友元函数output时出现了问题,查看报错信息发现友元函数只能访问类的私有成员,不能修改私有成员。于是重新设计代码,只在输出时输出最简分数,而不改变私有成员up,down的值使其最简。此外,在output函数中要首先判断分母是否等于0(分数是否合理)。
2.在接口get_up和get_down时也要返回化简后的分子和分母,同1。
任务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 //记录一笔帐,date为日期,amount为金额,desc为说明 12 void record(int date, double amount); 13 //获得到指定日期为止的存款金额按日累积值 14 double accumulate(int date) const { 15 return accumulation + balance * (date - lastDate); 16 } 17 public: 18 //构造函数 19 SavingsAccount(int date, int id, double rate); 20 int getId() const { return id; } 21 double getBalance() const { return balance; } 22 double getRate() const { return rate; } 23 static double getTotal() { return total; } 24 void deposit(int date, double amount); //存入现金 25 void withdraw(int date, double amount); //取出现金 26 //结算利息,每年1月1日调用一次该函数 27 void settle(int date); 28 //显示账户信息 29 void show() const; 30 }; 31 #endif //__ACCOUNT_H__
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 //SavingsAccount类相关成员函数的实现 8 SavingsAccount::SavingsAccount(int date, int id, double rate) 9 : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 10 cout << date << "\t#" << id << " is created" << endl; 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 void SavingsAccount::deposit(int date, double amount) { 21 record(date, amount); 22 } 23 void SavingsAccount::withdraw(int date, double amount) { 24 if (amount > getBalance()) 25 cout << "Error: not enough money" << endl; 26 else 27 record(date, -amount); 28 } 29 void SavingsAccount::settle(int date) { 30 double interest = accumulate(date) * rate / 365; //计算年息 31 if (interest != 0) 32 record(date, interest); 33 accumulation = 0; 34 } 35 void SavingsAccount::show() const { 36 cout << "#" << id << "\tBalance: " << balance; 37 }
5_11.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 //建立几个账户 6 SavingsAccount sa0(1, 21325302, 0.015); 7 SavingsAccount sa1(1, 58320212, 0.015); 8 //几笔账目 9 sa0.deposit(5, 5000); 10 sa1.deposit(25, 10000); 11 sa0.deposit(45, 5500); 12 sa1.withdraw(60, 4000); 13 //开户第90天到了银行的计息日,结算所有账户的年息 14 sa0.settle(90); 15 sa1.settle(90); 16 //输出各个账户信息 17 sa0.show(); cout << endl; 18 sa1.show(); cout << endl; 19 cout << "Total: " << SavingsAccount::getTotal() << endl; 20 return 0; 21 }
运行测试结果截图

分析
改进:1.此储蓄账户类的设计较第四章相比增加了部分const修饰成员函数,一定程度上增加了账户的安全性。2.增加了静态数据成员total记录账户总金额,使用户使用更便捷。
不足:1.此储蓄账户的安全性不足,主要体现在没有设计密码成员,导致查询余额等函数接口不安全。2.结息等操作数据精度不够。
浙公网安备 33010602011771号