试验2 c++
任务1:
t.h:
#pragma once #include <string> // 类T: 声明 class T { // 对象属性、方法 public: T(int x = 0, int y = 0);// // 普通构造函数 T(const T& t);// // 复制构造函数 T(T&& t);// // 移动构造函数 ~T(); // // 析构函数 void adjust(int ratio);// // 按系数成倍调整数据 void display() const; // // 以(m1, m2)形式显示T类对象信息 private: int m1, m2; // 类属性、方法 public: static int get_cnt(); // // 显示当前T类对象总数 public: static const std::string doc; // // 类T的描述信息 static const int max_cnt;// // 类T对象上限 private: static int cnt; // // 当前T类对象数目 // 类T友元函数声明 friend void func(); }; // 普通函数声明 void func();
t.cpp:
// 类T: 实现 // 普通函数实现 #include "t.h" #include <iostream> #include <string> using std::cout; using std::endl; using std::string; // static成员数据类外初始化 const std::string T::doc{ "a simple class sample" }; const int T::max_cnt = 999; int T::cnt = 0; // 对象方法 T::T(int x, int y) : m1{ x }, m2{ y } { ++cnt; cout << "T constructor called.\n"; } T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } { ++cnt; cout << "T copy constructor called.\n"; } T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } { ++cnt; cout << "T move constructor called.\n"; } T::~T() { --cnt; cout << "T destructor called.\n"; } void T::adjust(int ratio) { m1 *= ratio; m2 *= ratio; } void T::display() const { cout << "(" << m1 << ", " << m2 << ")"; } // 类方法 int T::get_cnt() { return cnt; } // 友元 void func() { T t5(42); t5.m2 = 2049; cout << "t5 = "; t5.display(); cout << endl; }
task1.cpp:
#include "t.h" #include <iostream> using std::cout; using std::endl; void test(); int main() { test(); cout << "\nmain: \n"; cout << "T objects'current count: " << T::get_cnt() << endl; } void test() { cout << "test class T: \n"; cout << "T info: " << T::doc << endl; cout << "T objects'max count: " << T::max_cnt << endl; cout << "T objects'current count: " << T::get_cnt() << endl << endl; T t1; cout << "t1 = "; t1.display(); cout << endl; T t2(3, 4); cout << "t2 = "; t2.display(); cout << endl; T t3(t2); t3.adjust(2); cout << "t3 = "; t3.display(); cout << endl; T t4(std::move(t2)); cout << "t3 = "; t4.display(); cout << endl; cout << "T objects'current count: " << T::get_cnt() << endl; func(); }
运行结果截图:
问题1:不能编译,声明友元函数不是定义函数,编译器找不到该函数的定义
问题2:
构造函数:
功能:用于创建类的实例并初始化对象的成员变量,参数 x
和 y
用于初始化对象的 m1
和 m2
成员,该构造函数还提供默认参数,允许在创建对象时不传递参数。
时机:当通过 T t;
或 T t(5, 10);
这样的方式实例化对象时,普通构造函数被调用。
复制构造函数:
功能:用于通过现有对象创建新对象,复制其成员变量的值。该构造函数特别用于需要传递对象副本的情况,比如通过值传递的函数参数。
时机:当对象通过复制进行初始化时,如在函数传参、返回对象等情况下将会调用复制构造函数。例如,T t2 = t1;
或函数参数为 T t(T t1)
。
移动构造函数:
功能:用于通过转移(而非复制)另一个对象的资源来创建新对象。它允许对象“窃取”另一个对象的成员数据而不是复制,以提高性能,尤其是在处理动态分配的资源时。
时机:当一个对象通过移动语义(例如,使用 std::move
)进行初始化时调用,如 T t2 = std::move(t1);
。
析构函数:
功能:通常用于释放对象所占用的任何资源,比如动态内存、文件句柄或其他系统资源。析构函数在对象生命周期结束时执行,用于执行清理工作。
时机:当栈上的对象超出作用域或动态分配的对象通过 delete
被销毁时,析构函数会被调用。例如,局部对象在函数结束时自动调用析构函数,而使用 new
创建的对象在调用 delete
时会调用析构函数。
问题3:
不能编译 const std::string类型的成员不能包含类内初始值设定项,static int cnt = 0;带有类内初始值的设定项必须是常数
实验2:
Complex.h:
#ifndef COMPLEX_H #define COMPLEX_H #include <string> class Complex { private: double real; double imag; public: static const std::string doc; Complex(double r = 0.0, double i = 0.0); double get_real() const; double get_imag() const; void add(const Complex& c); friend Complex add(const Complex& c1, const Complex& c2); friend bool is_equal(const Complex& c1, const Complex& c2); friend bool is_not_equal(const Complex& c1, const Complex& c2); friend void output(const Complex& c); friend double abs(const Complex& c); }; #endif
Complex.cpp:
#include <iostream> #include <cmath> #include "Complex.h" const std::string Complex::doc = "a simplified complex class"; Complex::Complex(double r, double i) : real(r), imag(i) {} double Complex::get_real() const { return real; } double Complex::get_imag() const { return imag; } void Complex::add(const Complex& c) { real += c.real; imag += c.imag; } Complex add(const Complex& c1, const Complex& c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } bool is_equal(const Complex& c1, const Complex& c2) { return (c1.real == c2.real) && (c1.imag == c2.imag); } bool is_not_equal(const Complex& c1, const Complex& c2) { return !is_equal(c1, c2); } void output(const Complex& c) { std::cout << c.real << " + " << c.imag << "i" << std::endl; } double abs(const Complex& c) { return std::sqrt(c.real * c.real + c.imag * c.imag); }
task2.cpp:
#include "Complex.h" #include <iostream> using std::cout; using std::endl; using std::boolalpha; void test() { cout << "类成员测试: " << endl; cout << Complex::doc << endl; cout << endl; cout << "Complex对象测试: " << endl; Complex c1; Complex c2(3, -4); const Complex c3(3.5); Complex c4(c3); cout << "c1 = "; output(c1); cout << "c2 = "; output(c2); cout << "c3 = "; output(c3); cout << "c4 = "; output(c4); cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; cout << endl; cout << "复数运算测试: " << endl; cout << "abs(c2) = " << abs(c2) << endl; c1.add(c2); cout << "c1 += c2, c1 = "; output(c1); cout << boolalpha<<"c1 == c2 : " << is_equal(c1, c2) << endl; cout << boolalpha<<"c1 != c3 : " << is_not_equal(c1, c3) << endl; c4 = add(c2, c3); cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; } int main() { test(); }
运行结果截图:
实验三:
task3:
#include <iostream> #include <complex> using std::cout; using std::endl; using std::boolalpha; using std::complex; void test() { cout << "标准库模板类comple测试: " << endl; complex<double> c1; complex<double> c2(3, -4); const complex<double> c3(3.5); complex<double> c4(c3); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c3 = " << c3 << endl; cout << "c4 = " << c4 << endl; cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl; cout << endl; cout << "复数运算测试: " << endl; cout << "abs(c2) = " << abs(c2) << endl; c1 += c2; cout << "c1 += c2, c1 = " << c1 << endl; cout << boolalpha; cout << "c1 == c2 : " << (c1 == c2) << endl; cout << "c1 != c3 : " << (c1 != c3) << endl; c4 = c2 + c3; cout << "c4 = c2 + c3, c4 = " << c4 << endl; } int main() { test(); }
运行结果截图:
实验四:
Fraction.h:
#pragma once #include <string> class Fraction { // 对象属性、方法 public: Fraction(int x , int y = 1); // 构造函数 Fraction(const Fraction& t);// 拷贝构造函数 Fraction(std::string s);// 字符串构造函数 ~Fraction(); // 析构函数 int get_up() const; int get_down() const; Fraction negative() const { return Fraction(-m1, m2); // 创建并返回负的分数 }// // 以(m1, m2)形式显示T类对象信息 private: int m1, m2; std::string str; // 类属性、方法 // 化简分数的方法 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } void simplify() { int gcd1 = gcd(m1, m2); // 求最大公约数 m1 /= gcd1; m2 /= gcd1; // 处理负号确保分母为正 if (m2 < 0) { m1 = -m1; m2 = -m2; } } public: static const std::string doc;// // 类T的描述信息 private: // 类T友元函数声明 friend void output(const Fraction& f); friend Fraction add(const Fraction& f1, const Fraction& f2); friend Fraction sub(const Fraction& f1, const Fraction& f2); friend Fraction mul(const Fraction& f1, const Fraction& f2); friend Fraction div(const Fraction& f1, const Fraction& f2); }; // 普通函数声明 void func();
Fraction.cpp:
// 类T: 实现 // 普通函数实现 #include "Fraction.h" #include <iostream> #include <string> using std::cout; using std::endl; using std::string; // static成员数据类外初始化 const std::string Fraction::doc{ "Fraction类 v 0.01版. \n目前仅支持分数对象的构造、输出、加 / 减 / 乘 / 除运算." }; // 对象方法 Fraction::Fraction(int x, int y) : m1{ x }, m2{ y } { simplify(); } Fraction::Fraction(const Fraction& t) : m1{ t.m1 }, m2{ t.m2 } { } Fraction::Fraction(std::string s):str{ s } { cout << s << endl; } Fraction::~Fraction() { } int Fraction::get_up() const { return m1; } int Fraction::get_down() const { return m2; } // 友元 // 输出分数的友元函数 void output(const Fraction& f) { std::cout << f.m1 << "/" << f.m2; } void output(const std::string& s) { std::cout << s; } // 加法友元函数 Fraction add(const Fraction& f1, const Fraction& f2) { return Fraction(f1.m1 * f2.m2 + f2.m1 * f1.m2, f1.m2 * f2.m2); } // 减法友元函数 Fraction sub(const Fraction& f1, const Fraction& f2) { return Fraction(f1.m1 * f2.m2 - f2.m1 * f1.m2, f1.m2 * f2.m2); } // 乘法友元函数 Fraction mul(const Fraction& f1, const Fraction& f2) { return Fraction(f1.m1 * f2.m2, f1.m1 * f2.m2); } // 除法友元函数 Fraction div(const Fraction& f1, const Fraction& f2) { if (f2.m1 == 0) { return Fraction(std::string("分母不能为0.报错!!")); } return Fraction(f1.m1 * f2.m2, f1.m2 * f2.m1); }
task4.cpp:
#include "Fraction.h" #include <iostream> using std::cout; using std::endl; void test1() { cout << "Fraction类测试: " << endl; cout << Fraction::doc << endl << endl; Fraction f1(5); Fraction f2(3, -4), f3(-18, 12); Fraction f4(f3); cout << "f1 = "; output(f1); cout << endl; cout << "f2 = "; output(f2); cout << endl; cout << "f3 = "; output(f3); cout << endl; cout << "f4 = "; output(f4); cout << endl; Fraction f5(f4.negative()); cout << "f5 = "; output(f5); cout << endl; cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; } void test2() { Fraction f6(42, 55), f7(0, 3); cout << "f6 = "; output(f6); cout << endl; cout << "f7 = "; output(f7); cout << endl; cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; } int main() { cout << "测试1: Fraction类基础功能测试\n"; test1(); cout << "\n测试2: 分母为0测试: \n"; test2(); }
运行结果截图:
实验五:
account.h:
#ifndef ACCOUNT_H #define ACCOUNT_H class SavingsAccount { private: int id; // 账号 double balance; // 余额 double rate; // 存款的年利率 int lastDate; // 上次变更余额的时期 double accumulation; // 余额按日累加之和 static double total; // 所有账户的总金额 // 记录一笔账, date为日期, amount为金额, desc为说明 void record(int date, double amount); // 获得到指定日期为止的存款金额按日积值 double accumulate(int date)const { return accumulation + balance * (date - lastDate); } public: //构造函数 SavingsAccount(int date, int id, double rate); int getId() const { return id; } double getBalance() const { return balance; } double getRate() const { return rate; } static double getotal() { return total; } void deposit(int date, double amount); //存入现金 void withdraw(int date, double amount); //取出现金 // 结算利息, 每年1月1日调用一次该函数 void settle(int date); // 显示账户信息 void show() const; }; #endif // _ _ACCOUNT_H_
account.cpp:
#include "account.h" #include<iostream> #include<cmath> using namespace std; double SavingsAccount :: total =0; // SavingsAccount类相关成员函数的实现 SavingsAccount::SavingsAccount(int date, int id, double rate) : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { cout << date << "\t#" << id << "is created" << endl; } void SavingsAccount::record(int date, double amount) { accumulation = accumulate(date); lastDate = date; amount = floor(amount * 100 + 0.5) / 100; balance += amount; total += amount; cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; } void SavingsAccount::deposit(int date, double amount) { record(date, amount); } void SavingsAccount :: withdraw(int date, double amount){ if (amount > getBalance()) cout << "Error: not enough money" << endl; else record(date, -amount); } void SavingsAccount :: settle(int date) { double interest = accumulate(date) * rate / 365; if (interest != 0) record(date, interest); accumulation = 0; } void SavingsAccount::show() const { cout << "#" << id << "\tbalance:" << balance; }
5_11.cpp:
#include "account.h" #include<iostream> using namespace std; int main() { // 建立几个账户 SavingsAccount sa0(1, 21325302, 0.015); SavingsAccount sal(1, 58320212, 0.015); //几笔账目 sa0.deposit(5, 5000); sal.deposit(25, 10000); sa0.deposit(45, 5500); sal.withdraw(60, 4000); // 开户后第90天到了银行的计息日, 结算所有账户的年息 sa0.settle(90); sal.settle(90); // 输出各个账户信息 sa0.show(); cout << endl; sal.show(); cout << endl; cout << "Total: " <<SavingsAccount::getotal() << endl; return 0; }
运行结果截图: