实验2 类和对象_基础编程1
1.实验任务1
t.h
1 #pragma once 2 3 #include<string> 4 5 // 类T: 声明 6 class T{ 7 public: 8 T(int x=0,int y=0); // 普通构造函数 9 T(const T &t); // 复制构造函数 10 T(T &&t); // 移动构造函数 11 ~T(); //析构函数 12 void adjust(int ratio); // 按系数成倍调整数据 13 void display() const; // 以(m1, m2)形式显示T类对象信息 14 15 private: 16 int m1,m2; 17 18 // 类属性、方法 19 public: 20 static int get_cnt(); // 显示当前T类对象总数 21 22 public: 23 static const std::string doc; // 类T的描述信息 24 static const int max_cnt; // 类T对象上限 25 26 private: 27 static int cnt; // 当前T类对象数目 28 29 // 类T友元函数声明 30 friend void func(); 31 }; 32 33 // 普通函数声明 34 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 T::T(int x,int y):m1{x},m2{y} { 19 ++cnt; 20 cout<<"T constructor called.\n"; 21 } 22 23 T::T(const T &t):m1{t.m1},m2{t.m2} { 24 ++cnt; 25 cout<<"T copy constructor called.\n"; 26 } 27 28 T::T(T &&t):m1{t.m1},m2{t.m2} { 29 ++cnt; 30 cout<<"T move constructor called.\n"; 31 } 32 33 T::~T() { 34 --cnt; 35 cout<<"T destructor called.\n"; 36 } 37 38 void T::adjust(int ratio) { 39 m1 *=ratio; 40 m2 *=ratio; 41 } 42 43 void T::display() const { 44 cout<<"("<<m1<<","<<m2<<")"; 45 } 46 47 //类方法 48 int T::get_cnt() { 49 return cnt; 50 } 51 52 //友元 53 void func() { 54 T t5(42); 55 t5.m2=2049; 56 cout<<"t5= ";t5.display(); cout<<endl; 57 }
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 T t1; 22 cout<<"t1= "; t1.display(); cout<<endl; 23 24 T t2(3,4); 25 cout<<"t2= "; t2.display(); cout<<endl; 26 27 T t3(t2); 28 t3.adjust(2); 29 cout<<"t3= "; t3.display(); cout<<endl; 30 31 T t4(std::move(t2)); 32 cout<<"t3= "; t4.display(); cout<<endl; 33 34 cout<<"T object'current count: "<<T::get_cnt()<<endl; 35 36 func(); 37 }
测试结果截图:
问题1:不能正确运行
原因:友元函数仅仅在类内声明,而缺少定义,编译器无法识别此函数
问题2:
普通构造函数 功能:用于初始化对象的属性 复制构造函数 功能:通过复制已存在的对象来初始化新对象
调用时机:在创建对象时调用 调用时机:当对象被复制时调用
移动构造函数 功能:通过移动已存在的对象的资源来初始化新对象 析构函数 功能:清理对象占用的资源
调用时机:当对象被移动时调用 调用时机:对象超出作用域或动态分配内存被释放时调用
问题3:不能
2.实验任务2
Complex.h
1 #pragma once 2 #include<string> 3 4 class Complex{ 5 public: 6 static const std::string doc; 7 8 private: 9 double real,imag; 10 11 public: 12 Complex(double x=0,double y=0); 13 Complex(const Complex &c); 14 15 double get_real() const; 16 double get_imag() const; 17 void add(const Complex &c); 18 19 friend Complex add(const Complex &c1,const Complex &c2); 20 friend bool is_equal(const Complex &c1,const Complex &c2); 21 friend bool is_not_equal(const Complex &c1,const Complex &c2); 22 friend void output(const Complex &c); 23 friend double abs(const Complex &c); 24 }; 25 26 Complex add(const Complex &c1,const Complex &c2); 27 bool is_equal(const Complex &c1,const Complex &c2); 28 bool is_not_equal(const Complex &c1,const Complex &c2); 29 void output(const Complex &c); 30 double abs(const Complex &c);
Complex.cpp
1 #include "Complex.h" 2 #include <iostream> 3 #include<string> 4 #include<cmath> 5 6 using namespace std; 7 8 const string Complex::doc{"a simplified complex class"}; 9 10 Complex::Complex(double x,double y):real{x},imag{y} {} 11 Complex::Complex(const Complex &c):real{c.real},imag{c.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 &c){ 22 real+=c.real; 23 imag+=c.imag; 24 } 25 26 Complex add(const Complex &c1,const Complex &c2){ 27 return Complex(c1.real+c2.real,c1.imag+c2.imag); 28 } 29 30 bool is_equal(const Complex &c1,const Complex &c2){ 31 return c1.real==c2.real&&c1.imag==c2.imag; 32 } 33 34 bool is_not_equal(const Complex &c1,const Complex &c2){ 35 return !is_equal(c1,c2); 36 } 37 38 void output(const Complex &c){ 39 cout<<c.real<<(c.imag>=0?"+":"")<<c.imag<<"i"; 40 } 41 42 double abs(const Complex &c){ 43 return sqrt(c.real*c.real+c.imag*c.imag); 44 }
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 }
测试结果截图:
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<<"标准库模板类complex测试:"<<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= "<<c3<<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 }
测试结果截图:
使用的接口:
- std::Complex的构造函数
- 成员函数c.real() c.imag()
- abs()
- c1+=c2
- c4=c2+c3
- c1==c2 c1!=c3
对比(Q1):
- std::complex 中,直接支持了运算符的重载( +, -, ==, !=),方便基本运算和比较操作,无需额外定义
- 可以调用real() 和 imag() 成员函数,直接访问实部和虚部
- std::complex 的构造更加简单,可以直接用实数初始化
- 可以直接通过 cout 输出,不需要定义输出函数output()
(Q2):
- 设计类时,可以考虑重载标准运算符,以简化运算
- 使用标准模板库类可以使代码更简洁
4.实验任务4
Fraction.h
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 7 class Fraction{ 8 public: 9 static const string doc; 10 static int gys(int a,int b); 11 12 private: 13 int up,down; 14 15 public: 16 Fraction(int x); 17 Fraction(int x,int y); 18 Fraction(const Fraction &f); 19 20 void simplify(); 21 int get_up() const; 22 int get_down() const; 23 Fraction negative(); 24 25 friend void output(const Fraction &f); 26 friend Fraction add(const Fraction &f1,const Fraction &f2); 27 friend Fraction sub(const Fraction &f1,const Fraction &f2); 28 friend Fraction mul(const Fraction &f1,const Fraction &f2); 29 friend Fraction div(const Fraction &f1,const Fraction &f2); 30 }; 31 void output(const Fraction &f); 32 Fraction add(const Fraction &f1,const Fraction &f2); 33 Fraction sub(const Fraction &f1,const Fraction &f2); 34 Fraction mul(const Fraction &f1,const Fraction &f2); 35 Fraction div(const Fraction &f1,const Fraction &f2);
Fraction.cpp
1 #include<iostream> 2 #include<string> 3 #include"Fraction.h" 4 5 using namespace std; 6 7 const string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."}; 8 9 int Fraction::gys(int a,int b){ 10 while(b){ 11 int t=a%b; 12 a=b; 13 b=t; 14 } 15 return a; 16 } 17 18 void Fraction::simplify(){ 19 int r=Fraction::gys(up,down); 20 up/=r; 21 down/=r; 22 if(down<0){ 23 up=-up; 24 down=-down; 25 } 26 } 27 28 Fraction::Fraction(int x):up{x},down{1} {simplify();} 29 Fraction::Fraction(int x,int y):up{x},down{y} {simplify();} 30 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down} {} 31 32 33 int Fraction::get_up() const{ 34 return up; 35 } 36 37 int Fraction::get_down() const{ 38 return down; 39 } 40 41 Fraction Fraction::negative(){ 42 return Fraction(-up,down); 43 } 44 45 void output(const Fraction &f){ 46 if(f.get_down()==1||f.get_up()==0) 47 cout<<f.get_up(); 48 else if(f.get_down()==0) 49 cout<<"分母不能为0"; 50 else 51 cout<<f.get_up()<<"/"<<f.get_down(); 52 } 53 54 Fraction add(const Fraction &f1,const Fraction &f2){ 55 return Fraction(f1.up*f2.down+f2.up*f1.down,f1.down*f2.down); 56 } 57 58 Fraction sub(const Fraction &f1,const Fraction &f2){ 59 return Fraction(f1.up*f2.down-f2.up*f1.down,f1.down*f2.down); 60 } 61 62 Fraction mul(const Fraction &f1,const Fraction &f2){ 63 return Fraction(f1.up*f2.up,f1.down*f2.down); 64 } 65 66 Fraction div(const Fraction &f1,const Fraction &f2){ 67 return Fraction(f1.up*f2.down,f1.down*f2.up); 68 }
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 }
测试结果截图:
5.实验任务5
account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 class SavingsAccount{ 4 private: 5 int id; 6 double balance; 7 double rate; 8 int lastDate; 9 double accumulation; 10 static double total; 11 void record(int date, double amount); 12 double accumulate(int date) const { 13 return accumulation + balance * (date - lastDate); 14 } 15 public: 16 SavingsAccount(int date, int id, double rate); int getId() const { return id; } 17 double getBalance() const { return balance; } 18 double getRate() const { return rate; } 19 static double getTotal() { return total; } 20 void deposit(int date, double amount); 21 void withdraw(int date, double amount); 22 void settle(int date); 23 void show() const; 24 }; 25 #endif//_ACCOUNT_H
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 double SavingsAccount::total = 0; 6 SavingsAccount::SavingsAccount(int date, int id, double rate) 7 : id(id), balance(0), rate(rate), lastDate(date), 8 accumulation(0) { 9 cout << date << "\t#" << id << " is created" << 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 cout << date << "\t#" << id << "\t" << amount << "\t" << 18 balance << endl; 19 } 20 21 void SavingsAccount::deposit(int date, double amount) { 22 record(date, amount); 23 } 24 void SavingsAccount::withdraw(int date, double amount) { 25 if (amount > getBalance()) 26 cout << "Error: not enough money" << endl; 27 else 28 record(date, -amount); 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 cout << "#" << id << "\tBalance: " << balance; 38 }
5_11.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 SavingsAccount sa0(1, 21325302, 0.015); 6 SavingsAccount sa1(1, 58320212, 0.015); 7 sa0.deposit(5, 5000); 8 sa1.deposit(25, 10000); 9 sa0.deposit(45, 5500); 10 sa1.withdraw(60, 4000); 11 sa0.settle(90); 12 sa1.settle(90); 13 sa0.show(); cout << endl; 14 sa1.show(); cout << endl; 15 cout << "Total: " << SavingsAccount::getTotal() << endl; 16 return 0; 17 }
测试结果截图:
合理,改进建议:
- 可以用 std::round 来实现四舍五入
- settle 方法中的 365 可以定义为一个常量,以提高可读性
- 可以提供一个初始余额作为参数来初始化balance