任务一:
//t.h
#include"t.h" #include<iostream> #include<string> using std::cout; using std::endl; using std::string; 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; }
//t.cpp
#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; private: int m1,m2; public: static int get_cnt(); public: static const std::string doc; static const int max_cnt; private: static int cnt; friend void func(); }; void func();
//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 object'max xount:"<<T::max_cnt<<endl; cout<<"T object'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 objeacts'current count:"<<T::get_cnt()<<endl; func(); }
问题1:不能
func()函数未声明
问题2:复制构造函数:用于创建一个新的对象,作为现有对象的副本
移动构造函数:用于从一个“右值”移动资源到新对象,避免不必要的复制
析构函数:在对象生命周期结束时自动调用,用于释放对象占用的资源
问题3:程序不能正常编译
任务二:
#ifndef COMPLEX_H #define COMPLEX_H #include <iostream> #include <string> #include <cmath> class Complex { public: static const std::string doc; Complex(); Complex(double real); Complex(double real, double 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 void output(const Complex& c); friend double abs(const Complex& c); private: double real; double imag; }; #endif // COMPLEX_H
#include "Complex.h" 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 !(c1.real == c2.real && c1.imag == c2.imag); } void output(const Complex& c) { std::cout << c.real; if (c.imag >= 0) std::cout << "+"; std::cout << c.imag << "i" << std::endl; } double abs(const Complex& c) { return std::sqrt(c.real * c.real + c.imag * c.imag); }
#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 << 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(); }
任务三:
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 }
任务四:
#pragma once #include<string> using namespace std; class Fraction{ public: static const string doc; public: Fraction(int x,int y=1); Fraction(const Fraction &f); const int get_up(); const int get_down(); Fraction negative(); public: friend void output(const Fraction &f); friend Fraction add(Fraction f1,Fraction f2); friend Fraction sub(Fraction f1,Fraction f2); friend Fraction mul(Fraction f1,Fraction f2); friend Fraction div(Fraction f1,Fraction f2); private: int up; int down; };
#include<iostream> #include"Fraction.h" #include<string> using namespace std; int max(int x,int y){ int a=abs(x); int b=abs(y); while (b) { int tmp=a; a=b; b=tmp%b; } return a; } const string Fraction::doc="Fraction类 v 0.01版.\n目前仅支持分数对象的构造,输出,加/减/乘/除运算.\n"; Fraction::Fraction(int x,int y):up{x},down{y}{} Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{} const int Fraction::get_up(){ return up/max(up,down); } const int Fraction::get_down(){ return down/max(up,down); } Fraction Fraction::negative(){ int new_up=-up; Fraction f(new_up,down); return f; } void output(const Fraction &f){ if (f.down==0) { cout<<"分母不能为0"; return ; } int new_down=f.down/max(f.up,f.down); if (new_down==1) cout<<f.up/max(f.up,f.down); else if (f.up*f.down>0) cout<<abs(f.up)/max(f.up,f.down)<<"/"<<abs(f.down)/max(f.up,f.down); else cout<<"-"<<abs(f.up)/max(f.up,f.down)<<"/"<<abs(f.down)/max(f.up,f.down); } Fraction sub(Fraction f1,Fraction f2) { int new_down=f1.down*f2.down; int new_up=f1.up*f2.down-f2.up*f1.down; new_up/=max(new_up,new_down);new_down/=max(new_up,new_down); Fraction f(new_up,new_down); return f; } Fraction add(const Fraction f1,const Fraction f2) { int new_down=f1.down*f2.down; int new_up=f1.up*f2.down+f2.up*f1.down; new_up/=max(new_up,new_down);new_down/=max(new_up,new_down); Fraction f(new_up,new_down); return f; } Fraction div(Fraction f1,Fraction f2) { int new_up=f1.up*f2.down; int new_down=f1.down*f2.up; new_up/=max(new_up,new_down);new_down/=max(new_up,new_down); Fraction f(new_up,new_down); return f; } Fraction mul(Fraction f1,Fraction f2) { int new_up=f1.up*f2.up; int new_down=f1.down*f2.down; new_up/=max(new_up,new_down);new_down/=max(new_up,new_down); Fraction f(new_up,new_down); return f; }
#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(); }
任务五:
1 //account.h 2 #ifndef__ACCOUNT_H__ 3 #define__ACCOUNT_H__ 4 class SavingAccount{ //储蓄账户类 5 private: 6 int id; //账号 7 double balance; //余额 8 double rate; //存款的年利率 9 int lastDate; //上次变更余额的时间 10 double accumulation; //余额按日累加之和 11 static double total; //所有账户的总金额 12 //记录一笔账,date为日期,amount为金额,desc为说明 13 void record(int date,double amount); 14 //获得到指定日期为止的存款金额按日累积值 15 double accumulate(int date)const{ 16 return accumulation+balance*(date-lastDate); 17 } 18 public: 19 //构造函数 20 SavingsAccount(int date,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()const{return total;} 25 void deposit(int date ,double amount); //存入现金 26 void withdraw(int date,double amount); //取出现金 27 //结算利息,每年1月1日调用一次该函数 28 void settle(int date); 29 //显示账户信息 30 void show()const; 31 }; 32 #endif/ /__ACCOUNT_H__
1 //account.cpp 2 #include"account.h" 3 #include<cmath> 4 #include<iostream> 5 using namespace std; 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 SavingsAcount::record(int date,double amount){ 13 accumulation=accmulate(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 }
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 }