实验二
1. 实验任务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); // 复制构造函数 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();
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 }
问题1:
不能去掉line36
分析原因:line32只是表明func与这个类的关系,没有起到声明函数的作用
问题2:
1. 普通构造函数
功能:
初始化对象的基本属性。
可以提供默认参数值。
调用时机:
当使用new创建对象时。
当在栈上定义对象时。
当创建对象的临时副本时。
2. 复制构造函数
功能:
创建一个对象的副本。
适用于需要深拷贝的情况。
调用时机:
当传递对象作为参数时。
当返回对象时。
当对象被复制初始化时。
3. 移动构造函数
功能:
允许将资源的所有权从一个对象转移到另一个对象,而不进行深拷贝。
通常用于优化性能,尤其是涉及动态内存分配时。
调用时机:
当对象被移动。
当临时对象被初始化到另一个对象时。
4.析构函数
功能:
清理对象的资源,释放动态分配的内存。
在对象生命周期结束时被调用。
调用时机:
当对象超出作用域。
当使用delete释放动态分配的对象。
当一个对象的生命周期结束。
问题3:
调整后无法正常编译运行
2.实验任务2
Complex.h
1 #ifndef COMPLEX_H 2 #define COMPLEX_H 3 4 #include <iostream> 5 6 class Complex { 7 private: 8 double real; 9 double imag; 10 11 public: 12 13 Complex(double r = 0.0, double i = 0.0); 14 Complex(const Complex &other); 15 16 double get_real() const; 17 double get_imag() const; 18 19 20 void add(const Complex &other) ; 21 22 friend bool is_equal(const Complex &other1 , const Complex &other2); 23 friend bool is_not_equal(const Complex &other1 , const Complex &other2); 24 25 friend Complex add(const Complex &other1 , const Complex &other2); 26 friend void output(const Complex &other); 27 friend double abs(const Complex &other); 28 29 static char doc[40]; 30 }; 31 32 bool is_equal(const Complex &other1 , const Complex &other2); 33 bool is_not_equal(const Complex &other1 , const Complex &other2); 34 35 Complex add(const Complex &other1 , const Complex &other2); 36 void output(const Complex &other); 37 double abs(const Complex &other); 38 39 #endif
Complex.cpp
1 #include "Complex.h" 2 #include <cmath> 3 4 Complex::Complex(double r, double i) : real(r), imag(i) {} 5 6 double Complex::get_real() const { 7 return real; 8 } 9 10 double Complex::get_imag() const { 11 return imag; 12 } 13 14 Complex::Complex(const Complex &other){ 15 real = other.get_real(); 16 imag = other.get_imag(); 17 } 18 19 void Complex::add(const Complex& other){ 20 real += other.real; 21 imag += other.imag; 22 23 return ; 24 } 25 26 double abs(const Complex& other){ 27 28 double sum; 29 sum = other.imag * other.imag + other.real * other.real; 30 double ave = sqrt(sum); 31 return ave; 32 } 33 34 bool is_equal(const Complex& other1 , const Complex& other2){ 35 if(other1.get_imag() == other2.get_imag() && other1.get_real() == other2.get_real()){ 36 return true; 37 } 38 39 else 40 return false; 41 } 42 43 bool is_not_equal(const Complex& other1 , const Complex& other2){ 44 if(other1.get_imag() == other2.get_imag() && other1.get_real() == other2.get_real()){ 45 return false; 46 } 47 48 else 49 return true; 50 } 51 52 Complex add(const Complex& other1 , const Complex& other2){ 53 return Complex(other1.get_real() + other2.get_real() , other1.get_imag() + other2.get_imag()); 54 } 55 56 void output(const Complex& other){ 57 std::cout << other.get_real(); 58 double imagy; 59 if(other.get_imag() < 0){ 60 std::cout << " - "; 61 imagy = (-1.0) * other.get_imag(); 62 } 63 else{ 64 std::cout << " + "; 65 imagy = other.get_imag(); 66 } 67 std::cout << imagy << 'i'; 68 69 70 return; 71 } 72 73 char Complex::doc[40] = {"a simplified Complex class"};
task2.cpp
1 #include <iostream> 2 #include "Complex.h" 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.实验任务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.实验任务4
Fraction.h
1 #ifndef FRACTION_H 2 #define FRACTION_H 3 4 #include <iostream> 5 #include <numeric> 6 #include <stdexcept> 7 #include <string> 8 9 using std::string; 10 11 class Fraction { 12 private: 13 int up; 14 int down; 15 16 void reduce(); 17 18 public: 19 static string doc; 20 21 Fraction(int num = 0, int denom = 1); 22 Fraction(const Fraction &f); 23 24 Fraction negative() const; 25 int get_up() const; 26 int get_down() const; 27 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 friend void output(const Fraction &f); 33 }; 34 35 #endif
Fraction.cpp
1 #include "Fraction.h" 2 #include <cstdlib> 3 4 string Fraction::doc {"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."}; 5 6 Fraction::Fraction(int num, int denom) : up(num), down(denom) { 7 if (denom == 0) { 8 std::cout << "分母不能为零" << std::endl; 9 exit(0); 10 } 11 reduce(); 12 } 13 14 Fraction::Fraction(const Fraction &f) : up(f.up), down(f.down) {} 15 16 17 int gcd(int a, int b) { 18 19 while (b != 0) { 20 int temp = b; 21 b = a % b; 22 a = temp; 23 } 24 return a ; 25 } 26 27 int abs(int a){ 28 if(a < 0){ 29 return -a; 30 } 31 else{ 32 return a; 33 } 34 } 35 36 void Fraction::reduce() { 37 38 int a = up; 39 int b = down; 40 41 int oka = 1; 42 int okb = 1; 43 44 if(a < 0){ 45 a *= -1; 46 oka = 0; 47 } 48 if(b < 0){ 49 b *= -1; 50 okb = 0; 51 } 52 53 int divi = gcd(a, b); 54 up /= divi; 55 down /= divi; 56 57 if(down < 0){ 58 down *= -1; 59 up *= -1; 60 } 61 } 62 63 Fraction Fraction::negative() const { 64 return Fraction(-up, down); 65 } 66 67 int Fraction::get_up() const { 68 return up; 69 } 70 71 int Fraction::get_down() const { 72 return down; 73 } 74 75 Fraction add(const Fraction &f1, const Fraction &f2) { 76 int up_1; 77 int down_1; 78 79 down_1 = f1.get_down() * f2.get_down(); 80 up_1 = f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(); 81 return Fraction(up_1 , down_1); 82 } 83 84 Fraction sub(const Fraction &f1, const Fraction &f2) { 85 return add(f1, f2.negative()); 86 } 87 88 Fraction mul(const Fraction &f1, const Fraction &f2) { 89 return Fraction(f1.up * f2.up, f1.down * f2.down); 90 } 91 92 Fraction div(const Fraction &f1, const Fraction &f2) { 93 return mul(f1, Fraction(f2.down, f2.up)); 94 } 95 96 void output(const Fraction &f) { 97 int ok = 1; 98 99 if(f.get_down() < 0 || f.get_up() < 0){ 100 ok = 0; 101 std::cout << '-'; 102 } 103 104 int a = abs(f.get_up()); 105 int b = abs(f.get_down()); 106 107 if(b == 1){ 108 std::cout << a; 109 } 110 else{ 111 std::cout << a << "/" << b; 112 } 113 }
task4.cpp
1 #include <iostream> 2 #include "Fraction.h" 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 }
5.实验任务5
account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 4 class SavingAccount{ 5 private: 6 int id; 7 double balance; 8 double rate; 9 double lastData; 10 double accumulation; 11 12 static double total; 13 14 void record(int data,double amount); 15 double accumulate(int data) const 16 { 17 return accumulation+balance*(data-lastData); 18 } 19 public: 20 SavingAccount(int data,int id,double rate); 21 int getId() const {return id;} 22 double getBalance() const {return balance;} 23 double getRate() const {return rate;} 24 static double getTotal() {return total;} 25 void deposit(int data,double amount); 26 void withdraw(int data,double amount); 27 void settle(int data); 28 void show() const; 29 }; 30 #endif
account.cpp
1 #include <bits/stdc++.h> 2 #include "account.h" 3 using namespace std; 4 5 double SavingAccount::total=0; 6 SavingAccount::SavingAccount(int data,int id,double rate) 7 :id(id),balance(0),rate(rate),lastData(data),accumulation(0) 8 { 9 cout<<data<<"\t#"<<id<<"is created"<<endl; 10 } 11 12 void SavingAccount::record(int data,double amount) 13 { 14 accumulation=accumulate(data); 15 lastData=data; 16 amount=floor(amount*100+0.5)/100; 17 balance+=amount; 18 total+=amount; 19 cout<<data<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; 20 } 21 22 void SavingAccount::deposit(int data,double amount) 23 { 24 record(data,amount); 25 } 26 27 void SavingAccount::withdraw(int data,double amount) 28 { 29 if (amount>getBalance()) 30 cout<<"Error:not enough money"<<endl; 31 else 32 record(data,-amount); 33 } 34 35 void SavingAccount::settle(int data) 36 { 37 double interest=accumulate(data)*rate/365; 38 if (interest!=0) 39 record(data,interest); 40 accumulation=0; 41 } 42 void SavingAccount::show() const 43 { 44 cout<<"#"<<id<<"\tBalance:"<<balance; 45 }
main.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 5 int main () 6 { 7 SavingAccount sa0(1,21325302,0.015); 8 SavingAccount sa1(1,58320212,0.015); 9 10 sa0.deposit(5,5000); 11 sa1.deposit(25,10000); 12 sa0.deposit(45,5500); 13 sa1.deposit(60,4000); 14 15 sa0.settle(90); 16 sa1.settle(90); 17 18 sa0.show();cout<<endl; 19 sa1.show();cout<<endl; 20 cout<<"Total:"<<SavingAccount::getTotal()<<endl; 21 return 0; 22 }