实验二
task 1
main.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(); }
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 }
运行结果:
问题一:能
问题二:普通构造函数可以初始化两个成员变量或者传递参数来完成初始化;
复制构造函数可以创建一个新对象,并将参数对象的成员变量值复制到新创建的对象中;
移动构造函数在不进行复制的情况下,将一个临时对象或即将销毁的对象的资源移动到新创建的对象中;
析构函数用于在对象的生命周期结束时进行清理工作;
问题三:不能
task 2
Complex.h
#ifndef COMPLEX_H #define COMPLEX_H #include <string> class Complex { public: static const std::string doc; Complex(); Complex(double real); // 构造复数,实部为real,虚部为0 Complex(double real, double imag); // 构造复数,实部为real,虚部为imag Complex(const Complex& other); // 获取实部和虚部 double get_real() const; double get_imag() const; // 加法操作 void add(const Complex& other); 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 std::ostream& operator<<(std::ostream& os, const Complex& c); friend void output(const Complex& c); // 取模运算 friend double abs(const Complex& c); private: double real; double imag; }; #endif
Complex.cpp
#include "Complex.h" #include <iostream> #include <cmath> const std::string Complex::doc = "a simplified complex class"; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double real) : real(real), imag(0) {} Complex::Complex(double real, double imag) : real(real), imag(imag) {} Complex::Complex(const Complex& other) : real(other.real), imag(other.imag) {} double Complex::get_real() const { return real; } double Complex::get_imag() const { return imag; } void Complex::add(const Complex& other) { real += other.real; imag += other.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); } std::ostream& operator<<(std::ostream& os, const Complex& c) { os << c.real << " + " << c.imag << "i"; return os; } void output(const Complex& c) { std::cout << c; } double abs(const Complex& c) { return std::sqrt(c.real * c.real + c.imag * c.imag); }
main
#include <iostream> #include "Complex.h" 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 << endl; cout << "c2 = "; output(c2); cout << endl; cout << "c3 = "; output(c3); cout << endl; cout << "c4 = "; output(c4); cout << endl; 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 << endl; cout << boolalpha; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; c4 = add(c2, c3); cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; } int main() { test(); return 0; }
运行代码:
task 3
#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(); }
task 4
Fraction.h
#pragma once #include <iostream> class Fraction { public: static const std::string doc; // 描述信息 Fraction(int numerator = 0, int denominator = 1); // 构造函数 Fraction(const Fraction &f); // 拷贝构造函数 int get_up() const; // 返回分子 int get_down() const; // 返回分母 Fraction negative() const; // 求负 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); private: int up; // 分子 int down; // 分母 void simplify(); // 简化分数 };
Fraction.cpp
#include "Fraction.h" #include<iostream> const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算。"; int gcd1(int a,int b) { return b==0?a:gcd1(b,a%b); } Fraction::Fraction(int numerator, int denominator) : up(numerator), down(denominator) { if (down == 0) { std::cout<<"分母不能为0"<<std::endl; } simplify(); } Fraction::Fraction(const Fraction &f) : up(f.up), down(f.down) {} void Fraction::simplify() { int gcd = gcd1(up, down); up /= gcd; down /= gcd; if (down < 0) { // 保持负号在分子上 up = -up; down = -down; } } int Fraction::get_up() const { return up; } int Fraction::get_down() const { return down; } Fraction Fraction::negative() const { return Fraction(-up, down); } void output(const Fraction &f){ if(f.down!=0){ if(f.down==1) std::cout<<f.up<<std::endl; else std::cout<<f.up<<"/"<<f.down<<std::endl; } } Fraction add(const Fraction &f1, const Fraction &f2) { return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down); } Fraction sub(const Fraction &f1, const Fraction &f2) { return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down); } Fraction mul(const Fraction &f1, const Fraction &f2) { return Fraction(f1.up * f2.up, f1.down * f2.down); } Fraction div(const Fraction &f1, const Fraction &f2) { if (f2.up == 0) { std::cout<<"分母不能为0"<<std::endl; } else return Fraction(f1.up * f2.down, f1.down * f2.up); }
main
#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(); }
运行截图
task 5
account.h
#pragma once #ifndef ACCOUNT_H__ #define ACCOUNT_H__ class SavingAccount{ private: int id; double balance; double rate; int lastDate; double accumulation; static double total; void record(int date,double amount); double accumulate(int date) const{ return accumulation+balance*(date-lastDate); } public: SavingAccount(int date,int id,double rate); int getId() const{return id;} double getBalance() const{return balance;} double getRate() const{return rate;} static double getTotal() {return total;} void deposit(int date,double amount); void withdraw(int date,double amount); void settle(int date); void show() const; }; #endif
account.app
#include "account.h" #include <cmath> #include <iostream> using namespace std; double SavingAccount::total=0; SavingAccount::SavingAccount(int date,int id,double rate) :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ cout<<date<<"\t#"<<id<<"is created"<<endl; } void SavingAccount::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 SavingAccount::deposit(int date,double amount){ record(date,amount); } void SavingAccount::withdraw(int date,double amount){ if(amount>getBalance()){ cout<<"Error:not enough money"<<endl; } else{ record(date,-amount); } } void SavingAccount::settle(int date){ double interest=accumulate(date)*rate/365; if(interest!=0) record(date,interest); accumulation=0; } void SavingAccount::show() const{ cout<<"#"<<id<<"\tBalance:"<<balance; }
main.cpp
#include "account.h" #include <iostream> using namespace std; int main(){ SavingAccount sa0(1,21325302,0.015); SavingAccount sa1(1,58320212,0.015); sa0.deposit(5,5000); sa1.deposit(25,10000); sa0.deposit(45,5500); sa1.withdraw(60,4000); sa0.settle(90); sa1.settle(90); sa0.show();cout<<endl; sa1.show();cout<<endl; cout<<"Total:"<<SavingAccount::getTotal()<<endl; return 0; }
截图:
问题:我认为合理,不需要改进。