实验2 类和对象_基础编程1
实验任务1
代码:
t.h:
1 #pragma once 2 3 #include<string> 4 5 class T { 6 public: 7 T(int x = 0, int y = 0); 8 T(const T& t); 9 T(T&& t); 10 ~T(); 11 void adjust(int ratio); 12 void display()const; 13 private: 14 int m1, m2; 15 16 public: 17 static int get_cnt(); 18 19 public: 20 static const std::string doc; 21 static const int max_cnt; 22 23 private: 24 static int cnt; 25 26 friend void func(); 27 }; 28 29 void func();
t.cpp:
1 #include"t.h" 2 #include<iostream> 3 #include<string> 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 9 const std::string T::doc{ "a simple class sample" }; 10 const int T::max_cnt = 999; 11 int T::cnt = 0; 12 13 T::T(int x, int y) :m1{ x }, m2{ y } { 14 ++cnt; 15 cout << "T constructor called.\n"; 16 } 17 18 T::T(const T& t) :m1{ t.m1 }, m2{ t.m2 } { 19 ++cnt; 20 cout << "T copy constructor called.\n"; 21 } 22 23 T::T(T&& t) :m1{ t.m1 }, m2{ t.m2 } { 24 ++cnt; 25 cout << "T move constructor called.\n"; 26 } 27 28 T::~T() { 29 --cnt; 30 cout << "T destructor called.\n"; 31 } 32 33 void T::adjust(int ratio) { 34 m1 *= ratio; 35 m2 *= ratio; 36 } 37 38 void T::display()const { 39 cout << "(" << m1 << "," << m2 << ")"; 40 } 41 42 int T::get_cnt() { 43 return cnt; 44 } 45 46 void func() { 47 T t5(42); 48 t5.m2 = 2049; 49 cout << "t5="; t5.display(); cout << endl; 50 }
task.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; 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 objects'current count:" << T::get_cnt() << endl; 35 36 func(); 37 }
运行截图:
问题1:
不能去掉,因为func在类内声明了友元函数,但没有声明为普通函数,删掉会显示找不到此函数。
问题2:line9是普通构造函数,给定了两个默认的参数初始值。在系统定义类的对象时,如果定义形式是T t(括号内可以有0到两个参数),就调用该函数。
line10是复制构造函数,将t指向调用时输入的对象,但不改变其的值。
line11是移动构造函数,通过移动而不复制实参的方式提高运行效率。
line12是析构函数,之前的各类构造函数调用完成后,调用该函数,释放他们的空间。一般在主函数结束后调用。
问题3:不能
实验任务2
代码:
complex.h:
1 #pragma once 2 #include<string> 3 class Complex { 4 public: 5 static std::string doc; 6 Complex(double x1 = 0, double y1 = 0); 7 Complex(const Complex& c); 8 ~Complex() {} 9 double get_real() ; 10 double get_imag() ; 11 void add(const Complex &d) ; 12 friend Complex add(const Complex &e, const Complex &f); 13 friend bool is_equal(const Complex &e,const Complex &f); 14 friend bool is_not_equal(const Complex &e,const Complex &f); 15 friend void output(const Complex& e); 16 friend double abs(const Complex &g); 17 18 private: 19 double x, y; 20 }; 21 22 bool is_equal(const Complex &e,const Complex&f); 23 bool is_not_equal(const Complex &e,const Complex&f); 24 void output(const Complex& e);
complex.cpp:
1 #include"complex.h" 2 #include<iostream> 3 #include<string> 4 Complex::Complex(double x1, double y1):x{ x1 }, y{ y1 } {} 5 6 Complex::Complex(const Complex& c) :x{ c.x }, y{ c.y } {} 7 double Complex::get_real() { 8 return x; 9 } 10 double Complex::get_imag() { 11 return y; 12 } 13 void Complex::add(const Complex& d) { 14 x += d.x; 15 y += d.y; 16 } 17 Complex add(const Complex& e,const Complex& f) { 18 Complex h; 19 h.x = e.x + f.x; 20 h.y = e.y + f.y; 21 return h; 22 } 23 bool is_equal(const Complex &e,const Complex &f) { 24 return e.x == f.x && e.y == f.y; 25 } 26 bool is_not_equal(const Complex& e, const Complex& f) { 27 return e.x != f.x || e.y != f.y; 28 } 29 30 void output(const Complex& e) { 31 std::cout << e.x << e.y << "i"; 32 } 33 double abs(const Complex& g) { 34 return sqrt(g.x * g.x + g.y * g.y); 35 } 36 std::string Complex::doc = "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
代码:
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 public: 6 static const std::string doc; 7 Fraction(int x=1, int y = 1); 8 Fraction(const Fraction& p); 9 int get_up(); 10 int get_down(); 11 Fraction negative(); 12 13 public: 14 friend void output(Fraction p); 15 friend Fraction add(const Fraction& a, const Fraction& b); 16 friend Fraction sub(const Fraction& a, const Fraction& b); 17 friend Fraction mul(const Fraction& a, const Fraction& b); 18 friend Fraction div(const Fraction& a, const Fraction& b); 19 20 private: 21 int up, down; 22 23 }; 24 25 void output( Fraction p); 26 27 28 int computeGCD(int a, int b);
Fraction.cpp:
1 #include"Fraction.h" 2 #include<iostream> 3 #include<string> 4 5 const std::string Fraction::doc = "Fraction类v0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 6 7 Fraction::Fraction(int x,int y):up{x},down{y}{} 8 Fraction::Fraction(const Fraction &p):up{p.up},down{p.down}{} 9 int Fraction::get_up(){ 10 int gcd = computeGCD(up, down); 11 up /= gcd; 12 down /= gcd; 13 return up; 14 } 15 int Fraction::get_down(){ 16 int gcd = computeGCD(up,down); 17 up /= gcd; 18 down /= gcd; 19 return down; 20 } 21 Fraction Fraction::negative() { 22 return Fraction(-up, down); 23 } 24 void output(Fraction p) { 25 int gcd = computeGCD(p.up, p.down); 26 p.up /= gcd; 27 p.down /= gcd; 28 if (p.down == 1) { 29 std::cout << p.up; 30 } 31 else if ( p.down > 0&&p.down!=1) { 32 std::cout << p.up << "/" << p.down; 33 } 34 else if (p.down == 0) { 35 std::cout << "分母不能为0"; 36 } 37 else std::cout << -p.up << "/" << -p.down; 38 } 39 Fraction add(const Fraction& a, const Fraction& b) { 40 Fraction c; 41 c.up = a.up*b.down+ b.up*a.down; 42 c.down = a.down * b.down; 43 return c; 44 } 45 Fraction sub(const Fraction& a, const Fraction& b) { 46 Fraction c; 47 c.down = a.down * b.down; 48 c.up = a.up * b.down - a.down * b.up; 49 int gcd = computeGCD(c.up, c.down); 50 c.up /= gcd; 51 c.down /= gcd; 52 return c; 53 } 54 Fraction mul(const Fraction& a, const Fraction& b) { 55 Fraction c; 56 c.down = a.down * b.down; 57 c.up = a.up * b.up; 58 int gcd = computeGCD(c.up, c.down); 59 c.up /= gcd; 60 c.down /= gcd; 61 return c; 62 } 63 Fraction div(const Fraction& a, const Fraction& b) { 64 Fraction c; 65 if (b.up != 0 && b.down != 0) { 66 c.down = a.down * b.up; 67 c.up = a.up * b.down; 68 int gcd = computeGCD(c.up, c.down); 69 c.up /= gcd; 70 c.down /= gcd; 71 return c; 72 } 73 else { 74 return Fraction(1, 0); 75 } 76 } 77 78 int computeGCD(int a, int b) { 79 while (b != 0) { 80 int temp = b; 81 b = a % b; 82 a = temp; 83 } 84 return a < 0 ? -a : a; 85 }
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 13 void record(int date, double amount); 14 double accumulate(int date)const { 15 return accumulation + balance * (date - lastDate); 16 } 17 public: 18 SavingsAccount(int date, int id, double rate); 19 int getId()const { return id; } 20 double getBanlance()const { return balance; } 21 double getRate()const { return rate; } 22 static double getTotal() { return total; } 23 void deposit(int date, double amcount); 24 void withdraw(int date, double amount); 25 void settle(int date); 26 void show()const; 27 }; 28 #endif
account.cpp:
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 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 > getBanlance()) 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 SavingsAccount sa0(1, 21325302, 0.0015); 6 SavingsAccount sa1(1, 58320212, 0.0015); 7 8 sa0.deposit(5, 5000); 9 sa1.deposit(25, 10000); 10 sa0.deposit(45, 5500); 11 sa1.withdraw(60, 4000); 12 13 sa0.settle(90); 14 sa1.settle(90); 15 16 sa0.show(); cout << endl; 17 sa1.show(); cout << endl; 18 cout << "Total:" << SavingsAccount::getTotal() << endl; 19 return 0; 20 }
运行截图:
实验总结:在类的定义中有许多小细节,要十分注意,不然报错一大堆就会很头疼。