实验2 类和对象_基础编程1
1. 实验任务1
t.h源代码:
1 #pragma once 2 #include <string> 3 4 // 类T: 声明 5 class T { 6 public: 7 // 对象属性、方法 8 T(int x = 0, int y = 0); // 普通构造函数 9 T(const T& t); // 复制构造函数 10 T(T&& t); // 移动构造函数 11 ~T(); // 析构函数 12 13 void adjust(int ratio); // 按系数成倍调整数据 14 void display() const; // 以(m1, m2)形式显示T类对象信息 15 // 类属性、方法 16 static int get_cnt(); // 显示当前T类对象总数 17 static const std::string doc; // 类T的描述信息 18 static const int max_cnt; // 类T对象上限 19 20 private: 21 int m1, m2; 22 static int cnt; // 当前T类对象数目 23 friend void func(); // 类T友元函数声明 24 }; 25 26 // 普通函数声明 27 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" }; // 类T的描述信息 14 const int T::max_cnt = 999; // 类T对象上限 15 int T::cnt = 0; //private // 当前T类对象数目 16 17 18 // 对象方法 19 // 普通构造函数 20 T::T(int x, int y) : m1{ x }, m2{ y } { 21 ++cnt; 22 cout << "T constructor called.\n"; 23 } 24 // 复制构造函数 25 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } { 26 ++cnt; 27 cout << "T copy constructor called.\n"; 28 } 29 // 移动构造函数 30 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } { 31 ++cnt; 32 cout << "T move constructor called.\n"; 33 } 34 // 析构函数 35 T::~T() { 36 --cnt; 37 cout << "T destructor called.\n"; 38 } 39 // 按系数成倍调整数据 40 void T::adjust(int ratio) { 41 m1 *= ratio; 42 m2 *= ratio; 43 } 44 // 以(m1, m2)形式显示T类对象信息 45 void T::display() const { 46 cout << "(" << m1 << ", " << m2 << ")"; 47 } 48 49 // 类方法 50 // 显示当前T类对象总数 51 int T::get_cnt() { 52 return cnt; 53 } 54 55 // 友元 56 void func() { 57 T t5(42); 58 t5.m2 = 2049; 59 cout << "t5 = "; t5.display(); cout << endl; 60 }
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 }
task1运行截图:
问题1:t.h中,普通函数func作为类X的友元,在类的内部声明了友元关系。在类外部,去掉line27,重新编译,是否能正确运行。如果能,回答说明可以去掉line27。如果不能,以截图形式给出编译报错信息,分析可能的原因。
回答:不能正确运行。 在这里,如果对代码进行些许修改(VS2022):func()声明改为func(T t)(定义及实现中做相同改动,这样改动并不会改变输出结果),可以发现,如果去掉类外部对func的声明,代码依旧可以正常运行!但如果将“T t”改为“int a”,那么代码无法正确运行。所以可以做出以下推断:在某些情况下,编译器可能会对未显式声明的函数进行隐式声明,这里的某些情况可能是函数的参数中存在当前类的元素。但采用分文件编译的方式,即类的定义在一个头文件中,而友元函数的定义在另一个源文件中,在类所在的头文件中额外对友元函数进行声明是一个好习惯,不仅代码可读性增强,其编译的逻辑也更加完整。
问题2:t.h中,line8-11给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析构函数的调用时机。
回答:普通构造函数T(int x = 0, int y = 0):用于创建T
类的对象,可以接受两个整数参数x
和y
,如果不提供参数则使用默认值 0,将参数值分别赋值给对象的成员变量以初始化对象。
复制构造函数T(const T& t):当通过一个已存在的T
对象创建新对象时被调用,确保新对象与原对象具有相同的状态。
移动构造函数T(T&& t):右值引用来实现资源的转移。
调用时机:调用构造函数->结束->调用析构函数,其中析构的顺序与构造的顺序相反。
问题3:t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。
回答:不能将静态成员变量的初始化放在头文件中。除非使用内联说明符,但使用内联变量至少需要C++17 。
2. 实验任务2
Complex.h源代码:
1 #pragma once 2 #include <string> 3 #ifndef COMPLEX_H 4 #define COMPLEX_H 5 6 class Complex { 7 public: 8 // 公有常量,用于对自定义复数类 Complex 做描述说明 9 static const std::string doc; 10 // 构造函数 11 Complex(); 12 Complex(double realPart); 13 Complex(double realPart, double imagPart); 14 Complex(const Complex& other); 15 // 获取实部和虚部 16 double get_real() const; 17 double get_imag() const; 18 // 加法操作 19 void add(const Complex& other); 20 21 private: 22 double real; 23 double imag; 24 25 public: 26 // 友元函数声明 27 friend Complex add(const Complex& c1, const Complex& c2); 28 friend bool is_equal(const Complex& c1, const Complex& c2); 29 friend bool is_not_equal(const Complex& c1, const Complex& c2); 30 friend void output(const Complex& c); 31 friend double abs(const Complex& c); 32 }; 33 34 #endif
Complex.cpp源代码:
1 #include <iostream> 2 #include <cmath> 3 #include "Complex.h" 4 5 //公有信息描述 6 const std::string Complex::doc = "a simplified complex class"; 7 8 Complex::Complex() : real{ 0 }, imag{ 0 } {} 9 Complex::Complex(double realPart) : real{ realPart }, imag{ 0 } {} 10 Complex::Complex(double realPart, double imagPart) : real{ realPart }, imag{ imagPart } {} 11 Complex::Complex(const Complex& other) : real{ other.real }, imag{ other.imag } {} 12 13 double Complex::get_real() const { 14 return real; 15 } 16 17 double Complex::get_imag() const { 18 return imag; 19 } 20 21 void Complex::add(const Complex& other) { 22 real += other.real; 23 imag += other.imag; 24 } 25 26 //友元函数实现 27 Complex add(const Complex& c1, const Complex& c2) { 28 return Complex(c1.real + c2.real, c1.imag + c2.imag); 29 } 30 31 bool is_equal(const Complex& c1, const Complex& c2) { 32 return (c1.real == c2.real && c1.imag == c2.imag); 33 } 34 35 bool is_not_equal(const Complex& c1, const Complex& c2) { 36 return!(is_equal(c1, c2)); 37 } 38 39 void output(const Complex& c) { 40 std::cout << c.real << (c.imag >= 0 ? " + " : " - ") << std::abs(c.imag) << "i" ; 41 } 42 43 double abs(const Complex& c) { 44 return std::sqrt(c.real * c.real + c.imag * c.imag); 45 }
task2.cpp源代码:
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 }
task2运行截图:
3. 实验任务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 //声明一个名为c1的复数对象,由于没有显式初始化,其初始值由complex类的默认构造函数决定,通常实部和虚部都初始化为 0 13 complex<double> c2(3, -4); 14 //创建一个实部为 3,虚部为 -4 的复数对象c2 15 const complex<double> c3(3.5); 16 //声明一个常量复数对象c3,实部初始化为 3.5,虚部初始化为 0 17 complex<double> c4(c3); 18 //使用拷贝构造函数创建一个新的复数对象c4,初始值与c3相同 19 20 cout << "c1 = " << c1 << endl; 21 cout << "c2 = " << c2 << endl; 22 cout << "c3 = " << c3 << endl; 23 cout << "c4 = " << c4 << endl; 24 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl; 25 cout << endl; 26 27 cout << "复数运算测试: " << endl; 28 cout << "abs(c2) = " << abs(c2) << endl; 29 c1 += c2; 30 cout << "c1 += c2, c1 = " << c1 << endl; 31 cout << boolalpha; 32 //设置布尔值的输出格式,使其以 “true” 或 “false” 的形式输出 33 cout << "c1 == c2 : " << (c1 == c2) << endl; 34 cout << "c1 != c3 : " << (c1 != c3) << endl; 35 c4 = c2 + c3; 36 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 37 } 38 39 int main() { 40 test(); 41 }
task3运行截图:
complex模板类:#include <complex>,通常使用complex<double>
表示实部和虚部为双精度浮点数的复数类型,也可以使用其他数值类型如float
、long double
等;支持基本的算术运算,包括加法、减法、乘法、除法;可以进行赋值运算,如c1=c2
将一个复数赋值给另一个复数;支持复合赋值运算,如c1+=c2、
c1-=c2
;提供了一些数学函数,如计算复数的模(abs
函数);通过成员函数real()
和imag()
可以轻松获取复数的实部和虚部;可以直接使用流输出操作符<<
输出复数对象。
4. 实验任务4
Fraction.h源代码:
1 #pragma once 2 #include <string> 3 #ifndef Fraction_h 4 #define Fraction_h 5 6 class Fraction 7 { 8 public: 9 //公有常量,用于对自定义分数类 Fraction 做描述说明 10 static const std::string doc; 11 //构造函数 12 Fraction(); 13 Fraction(int uppart, int downpart = 1); 14 Fraction(const Fraction& other); 15 16 //获取分子分母 17 int get_up() const; 18 int get_down() const; 19 //求负运算 20 Fraction negative() const; 21 22 private: 23 int up, down; 24 25 public: 26 //友元函数 27 friend void output(Fraction f); 28 friend Fraction add(const Fraction& f1, const Fraction& f2); 29 friend Fraction sub(const Fraction& f1, const Fraction& f2); 30 friend Fraction mul(const Fraction& f1, const Fraction& f2); 31 friend Fraction div(const Fraction& f1, const Fraction& f2); 32 }; 33 34 #endif
Fraction.cpp源代码:
1 #include <iostream> 2 #include "Fraction.h" 3 4 //公有常量 5 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 6 //构造函数 7 Fraction::Fraction() :up{ 0 }, down{ 0 } { } 8 Fraction::Fraction(int uppart, int downpart) :up{ uppart }, down{ downpart } { } 9 Fraction::Fraction(const Fraction& other):up{ other.up }, down{ other.down } { } 10 11 //获取分子分母 12 int Fraction::get_up() const { 13 //化简 14 int num1 = abs(up), num2 = abs(down), temp; 15 while (num2 > 0) { 16 temp = num1 % num2; 17 num1 = num2; 18 num2 = temp; 19 } 20 int newup = up / num1; 21 return newup; 22 } 23 int Fraction::get_down() const { 24 //化简 25 int num1 = abs(up), num2 = abs(down), temp; 26 while (num2 > 0) { 27 temp = num1 % num2; 28 num1 = num2; 29 num2 = temp; 30 } 31 int newdown = down / num1; 32 return newdown; 33 } 34 35 //求负运算 36 Fraction Fraction::negative() const { 37 return Fraction(-up, down); 38 } 39 40 void output(Fraction f) { 41 //化简 42 int num1 = abs(f.up), num2 = abs(f.down), temp; 43 while (num2 > 0) { 44 temp = num1 % num2; 45 num1 = num2; 46 num2 = temp; 47 } 48 f.up /= num1, f.down /= num1; 49 //输出 50 if (f.down == 0) std::cout << "分母不能为0"; 51 else if (f.up == 0) std::cout << "0"; 52 else if (f.down == 1) std::cout << f.up; 53 else if (f.up * f.down < 0) std::cout << -abs(f.up) << "/" << abs(f.down); 54 else std::cout << abs(f.up) << "/" << abs(f.down); 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 }
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 }
task4运行截图:
5. 实验任务5
account.h源代码:
1 #pragma once 2 #ifndef ACCOUNT_H 3 #define ACCOUNT_H 4 class SavingsAccount { 5 //储蓄账户类 6 private: 7 int id; //账号 8 double balance; //余额 9 double rate; //年利率 10 int lastDate; //上次变更余额的时期 11 double accumulation; //余额按日累加之和 12 static double total; 13 14 //记录一笔账, date 为日期, amount 为金额,desc 为说明 15 void record(int date, double amount); 16 //获得到定日期为止的存款金额日紫积值 17 double accumulate(int date) const { 18 return accumulation + balance * (date - lastDate); 19 } 20 public: 21 //构造函数 22 SavingsAccount(int date, int id, double rate); 23 int getId() const { return id; } 24 double getBalance() const { return balance; } 25 double getRate() const { return rate; } 26 static double getTotal() { return total; } 27 void deposit(int date, double amount); //存入现金 28 void withdraw(int date, double amount); //取出现金 29 30 //结算利息,每年 1 月 1 日调用一次该函数 31 void settle(int date); 32 //显示账户信息 33 void show() const; 34 }; 35 36 #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): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 9 std::cout << date << "\t#" << id << " is created" << std::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 std::cout << date << "\t#" << id << "\t" << amount << "\t"<< balance << std::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 std::cout << "Error: not enough money" << std::endl; 25 } 26 else { 27 record(date, -amount); 28 } 29 } 30 void SavingsAccount::settle(int date) { 31 double interest = accumulate(date) * rate / 365; //计算年息 32 if (interest != 0) 33 record(date, interest); 34 accumulation = 0; 35 } 36 void SavingsAccount::show() const { 37 std::cout << "#" << id << "\tBalance: " << balance; 38 }
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 sal(1, 58320212, 0.015); 8 //几笔账目 9 sa0.deposit(5, 5000); 10 sal.deposit(25, 10000); 11 sa0.deposit(45, 5500); 12 sal.withdraw(60, 4000); 13 //开户后第 90 天到了银行的计息日,结算所有账户的年息 14 sa0.settle(90); 15 sal.settle(90); 16 //输出各个账户信息 17 sa0.show(); 18 cout << endl; 19 sal.show(); 20 cout << endl; 21 cout << "Total: " << SavingsAccount::getTotal() << endl; 22 return 0; 23 }
task5运行截图: