实验2 类和对象_基础编程1
实验任务1:
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 }
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 22 T t1; 23 cout << "t1 = "; t1.display(); cout << endl; 24 25 T t2(3, 4); 26 cout << "t2 = "; t2.display(); cout << endl; 27 28 T t3(t2); 29 t3.adjust(2); 30 cout << "t3 = "; t3.display(); cout << endl; 31 32 T t4(std::move(t2)); 33 cout << "t3 = "; t4.display(); cout << endl; 34 35 cout << "T objects'current count: " << T::get_cnt() << endl; 36 37 func(); 38 }
运行测试结果截图:
问题1:
编译报错信息截图:
报错可能原因:当编译器编译这个项目时,会首先处理t.h头文件,然后处理t.cpp和task1.cpp源文件。在t.h中未对func()声明,即使在t,cpp中有func()函数,编译器并不会认为func()函数在主函数执行前便已对它进行声明。
问题2:
普通构造函数:
功能:在对象进行创建时对其初始化参数。
调用时机:创建对象时。
复制构造函数:
功能:使用一个已经存在的对象,去初始化同类的一个新对象。
调用时机:(1)用类的一个对象去初始化该类的另一个对象时;(2)函数的形参时类的对象,调用函数时,进行形参和实参结合时;(3)函数的返回值时类的对象,函数执行完成返回调用者时。
移动构造函数:
功能:将一个对象的资源移动到新的对象中,而非复制。
调用时机:利用右值引用,通过移动而不复制实参的高性能方式构建新对象。
析构函数:
功能:完成对象被删除前的一些清理工作。
调用时机:在对象的生存期即将结束的时刻被自动调用的。
问题3:
不能正确编译运行。
实验任务2:
Complex.h源码:
1 #pragma once 2 3 #include<string> 4 5 class Complex 6 { 7 public: 8 Complex(double r = 0, double i = 0); 9 Complex(const Complex& c); 10 ~Complex(); 11 12 double get_real() const; 13 double get_imag() const; 14 void add(const Complex& c); 15 16 friend Complex add(const Complex& c1, const Complex& c2); 17 friend bool is_equal(const Complex& c1, const Complex& c2); 18 friend bool is_not_equal(const Complex& c1, const Complex& c2); 19 friend double abs(const Complex& c); 20 friend void output(const Complex& c); 21 22 private: 23 double real, imag; 24 public: 25 static const std::string doc; 26 }; 27 28 Complex add(const Complex &c1, const Complex &c2); 29 bool is_equal(const Complex &c1, const Complex &c2); 30 bool is_not_equal(const Complex &c1, const Complex &c2); 31 double abs(const Complex &c); 32 void output(const Complex &c);
Complex.cpp源码:
1 #include <iostream> 2 #include "Complex.h" 3 4 using namespace std; 5 6 const string Complex::doc{ "a simplified complex class" }; 7 8 Complex::Complex(double r, double i) 9 { 10 real = r; 11 imag = i; 12 } 13 Complex::Complex(const Complex& c) 14 { 15 real = c.real; 16 imag = c.imag; 17 } 18 Complex::~Complex() = default; 19 20 double Complex::get_real() const 21 { 22 return real; 23 } 24 double Complex::get_imag() const 25 { 26 return imag; 27 } 28 void Complex::add(const Complex& c) 29 { 30 real = real + c.real; 31 imag = imag + c.imag; 32 } 33 34 Complex add(const Complex& c1, const Complex& c2) 35 { 36 return Complex(c1.real + c2.real, c1.imag + c2.imag); 37 } 38 bool is_equal(const Complex& c1, const Complex& c2) 39 { 40 if (c1.real == c2.real && c1.imag == c2.imag) 41 return true; 42 return false; 43 } 44 bool is_not_equal(const Complex& c1, const Complex& c2) 45 { 46 if (c1.real == c2.real && c1.imag == c2.imag) 47 return false; 48 return true; 49 } 50 double abs(const Complex& c) 51 { 52 return sqrt(c.real * c.real + c.imag * c.imag); 53 } 54 void output(const Complex& c) 55 { 56 if(c.imag >= 0) 57 cout << c.real << " + " << c.imag << "i"; 58 else 59 cout << c.real << " - " << -c.imag << "i"; 60 }
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:
task3.cpp源码:
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:
Fraction.h源码:
1 #pragma once 2 #include <string> 3 4 class Fraction 5 { 6 public: 7 Fraction(int uu, int dd = 1); 8 Fraction(const Fraction& f); 9 ~Fraction(); 10 11 int get_up() const; 12 int get_down() const; 13 Fraction negative(); 14 15 Fraction simplify(); 16 17 friend void output(const Fraction& f1); 18 friend Fraction add(const Fraction& f1, const Fraction& f2); 19 friend Fraction sub(const Fraction& f1, const Fraction& f2); 20 friend Fraction mul(const Fraction& f1, const Fraction& f2); 21 friend Fraction div(const Fraction& f1, const Fraction& f2); 22 private: 23 int up, down; 24 public: 25 static const std::string doc; 26 }; 27 28 void output(const Fraction& f1); 29 Fraction add(const Fraction& f1, const Fraction& f2); 30 Fraction sub(const Fraction& f1, const Fraction& f2); 31 Fraction mul(const Fraction& f1, const Fraction& f2); 32 Fraction div(const Fraction& f1, const Fraction& f2);
Fraction.cpp源码:
1 #include "Fraction.h" 2 #include <iostream> 3 4 using namespace std; 5 6 const std::string Fraction::doc{ "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算" }; 7 8 Fraction::Fraction(int uu, int dd) 9 { 10 up = uu; 11 down = dd; 12 } 13 Fraction::Fraction(const Fraction& f) 14 { 15 up = f.up; 16 down = f.down; 17 } 18 Fraction::~Fraction() = default; 19 20 int Fraction::get_up() const 21 { 22 return up; 23 } 24 int Fraction::get_down() const 25 { 26 return down; 27 } 28 Fraction Fraction::negative() 29 { 30 Fraction temp = simplify(); 31 return Fraction(-temp.up, temp.down); 32 } 33 34 Fraction Fraction::simplify() 35 { 36 int small, uu, dd, pos_or_neg; 37 if (up >= 0) 38 { 39 uu = up; 40 if (down > 0) 41 { 42 dd = down; 43 pos_or_neg = 1; 44 } 45 else 46 { 47 dd = -down; 48 pos_or_neg = -1; 49 } 50 } 51 else 52 { 53 uu = -up; 54 if (down > 0) 55 { 56 dd = down; 57 pos_or_neg = -1; 58 } 59 else 60 { 61 dd = -down; 62 pos_or_neg = 1; 63 } 64 } 65 if (uu > dd) 66 small = dd; 67 else 68 small = uu; 69 for (int i = small; i >= 1; i--) 70 if (uu % i == 0 && dd % i == 0) 71 { 72 uu = uu / i; 73 dd = dd / i; 74 break; 75 } 76 if (pos_or_neg == 1) 77 return Fraction(uu, dd); 78 return Fraction(-uu, dd); 79 } 80 81 void output(const Fraction& f) 82 { 83 Fraction temp = Fraction(f.up, f.down); 84 temp = temp.simplify(); 85 if (temp.down == 0) 86 cout << "分母不能为0"; 87 else 88 { 89 if (temp.up == 0) 90 cout << "0"; 91 else if (temp.up != 0 && temp.down == 1) 92 cout << temp.up; 93 else 94 cout << temp.up << "/" << temp.down; 95 } 96 } 97 98 Fraction add(const Fraction& f1, const Fraction& f2) 99 { 100 return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down); 101 } 102 Fraction sub(const Fraction& f1, const Fraction& f2) 103 { 104 return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down); 105 } 106 Fraction mul(const Fraction& f1, const Fraction& f2) 107 { 108 return Fraction(f1.up * f2.up, f1.down * f2.down); 109 } 110 Fraction div(const Fraction& f1, const Fraction& f2) 111 { 112 return Fraction(f1.up * f2.down, f1.down * f2.up); 113 }
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:
account.h源码:
1 #pragma once 2 #ifndef ACCOUNT_H 3 #define ACCOUNT_H 4 class SavingsAccount { 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() { 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__
account.cpp源码:
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 //SavingsAccount类相关成员函数的实现 8 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), 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" << balance << endl; 18 } 19 void SavingsAccount::deposit(int date, double amount) { 20 record(date, amount); 21 } 22 void SavingsAccount::withdraw(int date, double amount) { 23 if (amount > getBalance()) 24 cout << "Error:not enough money" << endl; 25 else 26 record(date, -amount); 27 } 28 void SavingsAccount::settle(int date) { 29 double interest = accumulate(date) * rate / 365; //计算年息 30 if (interest != 0) 31 record(date, interest); 32 accumulation = 0; 33 } 34 void SavingsAccount::show() const { 35 cout << "#" << id << "\tBalance:" << balance; 36 }
5_11.cpp源码:
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 }
运行测试结果截图:
分析:增加const关键字提高数据的安全性。使用静态数据成员total记录总金额,提高了使用性。
总结:
1.多文件结构的使用需要注意不同文件中操作的对象,如果对类的实现的文件修改,要注意是否在类的定义文件中同步。
2.const int返回类型 与 const成员函数 是两个不同的原型。
3.注意函数间相互调用,因为返回值类型不同导致的错误。