OOP实验二

任务1

源码:

 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: 实现
// 普通函数实现

#include "t.h"
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

// static成员数据类外初始化
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;
}
 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 }

 

结果:

问:t.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉

line36,重新编译,是否能正确运行。如果能,回答说明可以去掉line36。如果不能,以截图
形式给出编译报错信息,分析可能的原因。

答:不能

 在类内部的声明不会提醒main函数普通函数func的存在。

问:t.h中,line9-12给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析

构函数的调用时机。

答:

普通构造函数:通过数值创建对象实例。

复制构造函数:利用一个已知的对象实例给新实例赋值。

移动构造函数:在处理临时对象是时提高效率。

析构函数:释放实例占用的内存。

问:t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。

答:不能。

任务2

源码:

 

  1 //.h:
  2 #pragma once
  3 
  4 #include<iostream>
  5 #include<cmath>
  6 
  7 using std::cout;
  8 using std::endl;
  9 using std::boolalpha;
 10 
 11 void test();
 12 
 13 class Complex {
 14 private:
 15     double real, imag;
 16 public:
 17     static std::string doc;
 18     Complex(double r = 0, double i = 0) :real{ r }, imag{ i } {}
 19     Complex(const Complex& c) :real{ c.get_real() }, imag{ c.get_imag() } {}
 20     double get_real() const {
 21         return real;
 22     }
 23     double get_imag() const {
 24         return imag;
 25     }
 26     void add(const Complex c) {
 27         real += c.get_real();
 28         imag += c.get_imag();
 29     }
 30     friend Complex add(const Complex c1, const Complex c2);
 31     friend bool is_equal(const Complex& c1, const Complex& c2);
 32     friend bool is_not_equal(const Complex& c1, const Complex& c2);
 33     friend void output(const Complex& c);
 34     friend double abs(const Complex& c);
 35 };
 36 
 37 Complex add(const Complex c1, const Complex c2);
 38 bool is_equal(const Complex& c1, const Complex& c2);
 39 bool is_not_equal(const Complex& c1, const Complex& c2);
 40 void output(const Complex& c);
 41 double abs(const Complex& c);
 42 
 43 //.cpp:
 44 #include"Complex.h"
 45 
 46 std::string Complex::doc = "a simple complex class";
 47 
 48 void test() {
 49     cout << "类成员测试: " << endl;
 50     cout << Complex::doc << endl;
 51 
 52     cout << endl;
 53 
 54     cout << "Complex对象测试: " << endl;
 55     Complex c1;
 56     Complex c2(3, -4);
 57     const Complex c3(3.5);
 58     Complex c4(c3);
 59 
 60     cout << "c1 = "; output(c1); cout << endl;
 61     cout << "c2 = "; output(c2); cout << endl;
 62     cout << "c3 = "; output(c3); cout << endl;
 63     cout << "c4 = "; output(c4); cout << endl;
 64     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
 65 
 66     cout << endl;
 67 
 68     cout << "复数运算测试: " << endl;
 69     cout << "abs(c2) = " << abs(c2) << endl;
 70     c1.add(c2);
 71     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
 72     cout << boolalpha;
 73     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
 74     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
 75     c4 = add(c2, c3);
 76     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
 77 }
 78 Complex add(const Complex c1,const Complex c2) {
 79     Complex c3;
 80     c3.add(c2);
 81     c3.add(c1);
 82     return c3;
 83 }
 84 bool is_equal(const Complex& c1, const Complex& c2) {
 85     if (c1.get_imag() == c2.get_imag() && c1.get_real() == c2.get_real())
 86     {
 87         return true;
 88     }
 89     else
 90     {
 91         return false;
 92     }
 93 }
 94 
 95 bool is_not_equal(const Complex& c1, const Complex& c2) {
 96     if (c1.get_imag() == c2.get_imag() && c1.get_real() == c2.get_real())
 97     {
 98         return false;
 99     }
100     else
101     {
102         return true;
103     }
104 }
105 
106 void output(const Complex& c) {
107     if (c.get_imag() >= 0) {
108         cout << c.get_real() << '+' << c.get_imag() << 'i' << endl;
109     }
110     else {
111         cout << c.get_real() << '-' << abs(c.get_imag()) << 'i' << endl;
112     }
113 }
114 double abs(const Complex& c) {
115     return sqrt(c.get_imag() * c.get_imag() + c.get_real() * c.get_real());
116 
117 }
118 
119 //.main:
120 #include"Complex.h"
121 int main() {
122     test();
123 }

 

结果:

 

 

任务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

源码:

 

  1 //.h
  2 #pragma once
  3 #include <iostream>
  4 
  5 using std::cout;
  6 using std::endl;
  7 
  8 int measure(int x, int y);
  9 class Fraction {
 10 private:
 11     int up;
 12     int down;
 13 public:
 14     static std::string doc;
 15     Fraction(int u, int d = 1){
 16         int mea = measure(u, d);
 17         up = u / mea;
 18         down = d / mea;
 19         if (down < 0)
 20         {
 21             down = -down;
 22             up = -up;
 23         }
 24     }
 25     int get_up() const {
 26         return up;
 27     }
 28     int get_down() const {
 29         return down;
 30     }
 31     Fraction negative() {
 32         Fraction f(-up, down);
 33         return f;
 34     }
 35     friend void output(const Fraction& f);
 36     friend Fraction add(const Fraction f1, const Fraction f2);
 37     friend Fraction sub(const Fraction f1, const Fraction f2);
 38     friend Fraction mul(const Fraction f1, const Fraction f2);
 39     friend Fraction div(const Fraction f1, const Fraction f2);
 40 };
 41 
 42 Fraction add(const Fraction f1, const Fraction f2);
 43 Fraction sub(const Fraction f1, const Fraction f2);
 44 Fraction mul(const Fraction f1, const Fraction f2);
 45 Fraction div(const Fraction f1, const Fraction f2);
 46 void output(const Fraction& f);
 47 
 48 //.cpp
 49 #include "Fraction.h"
 50 
 51 std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算\n";
 52 Fraction add(const Fraction f1, const Fraction f2) {
 53     int up1, up2, down1, down2, up, down;
 54     up1 = f1.up;
 55     up2 = f2.up;
 56     down1 = f1.down;
 57     down2 = f2.down;
 58     up = up1 * down2 + up2 * down1;
 59     down = down1 + down2;
 60     int mea = measure(up, down);
 61     Fraction f(up / mea, down / mea);
 62     return f;
 63 }
 64 Fraction sub(const Fraction f1, const Fraction f2) {
 65     int up1, up2, down1, down2, up, down;
 66     up1 = f1.up;
 67     up2 = f2.up;
 68     down1 = f1.down;
 69     down2 = f2.down;
 70     up = up1 * down2 - up2 * down1;
 71     down = down1 + down2;
 72     int mea = measure(up, down);
 73     Fraction f(up / mea, down / mea);
 74     return f;
 75 }
 76 Fraction mul(const Fraction f1, const Fraction f2) {
 77     int up1, up2, down1, down2, up, down;
 78     up1 = f1.up;
 79     up2 = f2.up;
 80     down1 = f1.down;
 81     down2 = f2.down;
 82     up = up1 * up2;
 83     down = down1 * down2;
 84     int mea = measure(up, down);
 85     Fraction f(up / mea, down / mea);
 86     return f;
 87 }
 88 Fraction div(const Fraction f1, const Fraction f2) {
 89     if (f2.up == 0)
 90     {
 91         Fraction error(0, 0);
 92         return error;
 93     }
 94     int up1, up2, down1, down2, up, down;
 95     up1 = f1.up;
 96     up2 = f2.up;
 97     down1 = f1.down;
 98     down2 = f2.down;
 99     up = up1 * down2;
100     down = up2 * down1;
101     int mea = measure(up, down);
102     Fraction f(up / mea, down / mea);
103     return f;
104 }
105 void output(const Fraction& f) {
106     if (f.down != 0)
107     {
108         if (f.up == 0 || f.down == 1)
109         {
110             cout << f.up;
111         }
112         else
113         {
114             cout << f.up << "/" << f.down;
115         }
116     }
117     else 
118     {
119         cout << "分母不能为0";
120     }
121 }
122 int measure(int x, int y)
123 {
124     if (y == 0)
125     {
126         return 1;
127     }
128     int z = y;
129     while (x % y != 0)
130     {
131         z = x % y;
132         x = y;
133         y = z;
134     }
135     return z;
136 }
137 
138 //.main
139 #include"Fraction.h"
140 
141 void test1() {
142     cout << "Fraction类测试: " << endl;
143     cout << Fraction::doc << endl << endl;
144 
145     Fraction f1(5);
146     Fraction f2(3, -4), f3(-18, 12);
147     Fraction f4(f3);
148     cout << "f1 = "; output(f1); cout << endl;
149     cout << "f2 = "; output(f2); cout << endl;
150     cout << "f3 = "; output(f3); cout << endl;
151     cout << "f4 = "; output(f4); cout << endl;
152 
153     Fraction f5(f4.negative());
154     cout << "f5 = "; output(f5); cout << endl;
155     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
156 
157     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
158     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
159     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
160     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
161     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
162 }
163 
164 void test2() {
165     Fraction f6(42, 55), f7(0, 3);
166     cout << "f6 = "; output(f6); cout << endl;
167     cout << "f7 = "; output(f7); cout << endl;
168     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
169 }
170 
171 int main() {
172     cout << "测试1: Fraction类基础功能测试\n";
173     test1();
174 
175     cout << "\n测试2: 分母为0测试: \n";
176     test2();
177 }

 

结果:

 

任务5

源码:

 1 //.h
 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     void record(int date, double amount);
13     double accumulate(int date)const {
14         return accumulation + balance * (date - lastDate);
15     }
16 public:
17     SavingsAccount(int date, int id, double rate);
18     int getId()const { return id; }
19     double getBalance()const { return balance; }
20     double getRate()const { return rate; }
21     static double getTotal() { return total; }
22     void deposit(int date, double amount);
23     void withdraw(int date, double amount);
24     void settle(int date);
25     void show()const;
26 };
27 #endif // !__ACCOUNT__
28 
29 //.cpp
30 #include"account.h"
31 #include<cmath>
32 #include<iostream>
33 using namespace std;
34 double SavingsAccount::total = 0;
35 SavingsAccount::SavingsAccount(int date, int id, double rate)
36     :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
37     cout << date << "\t#" << id << " is created" << endl;
38 }
39 void SavingsAccount::record(int date, double amount) {
40     accumulation = accumulate(date);
41     lastDate = date;
42     amount = floor(amount * 100 + 0.5) / 100;
43     balance += amount;
44     total += amount;
45     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
46 }
47 void SavingsAccount::deposit(int date, double amount) {
48     record(date, amount);
49 }
50 void SavingsAccount::withdraw(int date, double amount) {
51     if (amount > getBalance())
52         cout << "Error:not enough money" << endl;
53     else
54         record(date, -amount);
55 }
56 void SavingsAccount::settle(int date) {
57     double interest = accumulate(date) * rate / 365;
58     if (interest != 0)
59         record(date, interest);
60     accumulation = 0;
61 }
62 void SavingsAccount::show() const  {
63     cout << "#" << id << "\tBalance:" << balance;
64 }
65 
66 //.main
67 #include"account.h"
68 #include<iostream>
69 using namespace std;
70 int main() {
71     SavingsAccount sa0(1, 2132502, 0.015);
72     SavingsAccount sa1(1, 58320212, 0.015);
73     sa0.deposit(5, 5000);
74     sa1.deposit(25, 10000);
75     sa0.deposit(45, 5500);
76     sa1.withdraw(60, 4000);
77     sa0.settle(90);
78     sa1.settle(90);
79     sa0.show(); cout << endl;
80     sa1.show(); cout << endl;
81     cout << "Total:" << SavingsAccount::getTotal() << endl;
82     return 0;
83 }

 

结果:

 

posted @ 2024-10-25 22:05  孙一发  阅读(5)  评论(0编辑  收藏  举报