实验二 类和对象

任务1

代码:

t.h:

 1 #pragma once 
 2 #include<string>
 3  
 4 class T {
 5 public:
 6     T(int x = 0, int y = 0);
 7     T(const T& t);
 8     T(T&& t);
 9     ~T();
10     void adjust(int ratio);
11     void display()const;
12 private:
13     int m1, m2;
14  
15 public:
16     static int get_cnt();
17  
18 public:
19     static const std::string doc;
20     static const int max_cnt;
21  
22 private:
23     static int cnt; 
24     friend void func();
25 };
26 
27 void func();
View Code

t.cpp:

// 类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;
}
View Code

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 }
View Code

运行截图:

问题1:不能, 原因可能是没有声明函数。

问题2:普通构造函数实现了对类的初始化,复制构造函数主要用于初始化一个新的对象,使其成为另一个同类型对象的副本,它会将引用的源对象的值复制到新对象中。移动构造函数的作用是提高程序的效率,其通过右值引用创建新对象,它避免了深拷贝,直接转移原始对象的资源到新对象中,从而减少了复制的时间。析构函数作用是在对象生命周期结束时进行资源清理。当前三种构造函数全部结束时析构函数会自动调用多遍。

问题3:不能正确编译。

 

任务2

代码:

Complex.h:

 1 class Complex{
 2 
 3 public:
 4     static string doc;
 5     Complex(double r=0,double i=0);
 6     Complex(const Complex &c);
 7     double get_real();
 8     double get_imag();
 9     void add(Complex &c);
10     friend Complex add(const Complex &c1,const Complex &c2);
11     friend bool is_equal(const Complex &c1,const Complex &c2);
12     friend bool is_not_equal(const Complex &c1,const Complex &c2);
13     friend void output(const Complex &c1);
14     friend double abs(const Complex &c1);
15 private:
16     double real;
17     double imag;
18     
19 };
20 
21 #endif
View Code

Complex.cpp:

 1 #include <bits/stdc++.h>
 2 #include "Complex.h"
 3 using namespace std;
 4 
 5 string Complex::doc="a simplified complex class";
 6 Complex::Complex(double r,double i):real{r},imag{i}{}
 7 Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{}
 8 double Complex::get_real(){return real;}
 9 double Complex::get_imag(){return imag;}
10 void Complex::add(Complex &c){
11     real+=c.real;
12     imag+=c.imag;
13 }
14 Complex add(const Complex &c1,const Complex &c2)
15 {
16     Complex c3;
17     c3.real=c1.real+c2.real;
18     c3.imag=c1.imag+c2.imag;
19     return c3;
20 }
21 
22 bool is_equal(const Complex &c1,const Complex &c2)
23 {
24     if(c1.real==c2.real&&c1.imag==c2.imag)
25         return true;
26     return false;
27 }
28 
29 bool is_not_equal(const Complex &c1,const Complex &c2)
30 {
31     if(c1.real==c2.real&&c1.imag==c2.imag)
32         return false;
33     return true;
34 }
35 
36 void output(const Complex &c1)
37 {
38     cout<<c1.real<<" + "<<c1.imag<<"i"<<endl;
39 }
40 
41 double abs(const Complex &c1)
42 {
43     return sqrt(c1.real*c1.real+c1.imag*c1.imag);
44 }
View Code

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 }
View Code

运行截图:

 

任务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 }
View Code

运行截图:

 

任务4

代码:

Fraction.h:
 1 #pragma once
 2 #include<string>
 3 
 4 class Fraction{
 5 private:
 6     int up;
 7     int down;
 8 
 9 public:
10     Fraction(int u,int d=1);
11     Fraction(const Fraction &f);
12     
13     static const std::string doc;
14     int get_up();
15     int get_down();
16     Fraction negative();
17     friend void output(const Fraction f);
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 };
23 void output(const Fraction f);
24 Fraction add(const Fraction f1,const Fraction f2);
25 Fraction sub(const Fraction f1,const Fraction f2);
26 Fraction mul(const Fraction f1,const Fraction f2);
27 Fraction div(const Fraction f1,const Fraction f2);
View Code
Fraction.cpp:
  1 #include <bits/stdc++.h>
  2 #include "Fraction.h"
  3 using namespace std;
  4 
  5 const string Fraction::doc="Fraction类 v 0.01版.\n目前仅支持分数对象的构造,输出,加/减/乘/除运算.\n";
  6 Fraction::Fraction(int u,int d):up{u},down{d}{}
  7 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{}
  8 int Fraction::get_up()
  9 {
 10     int a,b;
 11     a=abs(up);b=abs(down);
 12     
 13     while (b)
 14     {
 15         int tmp=a;
 16         a=b;
 17         b=tmp%b;
 18     }
 19     return up/a;
 20 }
 21 int Fraction::get_down()
 22 {
 23     int a,b;
 24     a=abs(up);b=abs(down);
 25     
 26     while (b)
 27     {
 28         int tmp=a;
 29         a=b;
 30         b=tmp%b;
 31     }
 32     return down/a;
 33 }
 34 Fraction Fraction::negative()
 35 {
 36     int new_up=-up;
 37     Fraction f(new_up,down);
 38     return f;
 39 }
 40 
 41 void output(const Fraction f)
 42 {
 43     if (f.down==0)
 44     {
 45         cout<<"分母不能为0";
 46         return ;
 47     }
 48     
 49     int a,b;
 50     a=abs(f.up);b=abs(f.down);
 51     
 52     while (b)
 53     {
 54         int tmp=a;
 55         a=b;
 56         b=tmp%b;
 57     }
 58     int new_down=f.down/a;
 59     if (new_down==1)
 60         cout<<f.up/a;
 61     else if (f.up*f.down>0)
 62         cout<<abs(f.up)/a<<"/"<<abs(f.down)/a;
 63     else 
 64         cout<<"-"<<abs(f.up)/a<<"/"<<abs(f.down)/a;
 65 }
 66 
 67 Fraction add(const Fraction f1,const Fraction f2)
 68 {
 69     int new_down=f1.down*f2.down;
 70     int new_up=f1.up*f2.down+f2.up*f1.down;
 71     int a,b;
 72     a=abs(new_up);b=abs(new_down);
 73     while (b)
 74     {
 75         int tmp=a;
 76         a=b;
 77         b=tmp%b;
 78     }
 79     new_up/=a;new_down/=a;
 80     Fraction f(new_up,new_down);
 81     return f;
 82 }
 83 
 84 Fraction sub(Fraction f1,Fraction f2)
 85 {
 86     int new_down=f1.down*f2.down;
 87     int new_up=f1.up*f2.down-f2.up*f1.down;
 88     int a,b;
 89     a=abs(new_up);b=abs(new_down);
 90     while (b)
 91     {
 92         int tmp=a;
 93         a=b;
 94         b=tmp%b;
 95     }
 96     new_up/=a;new_down/=a;
 97     Fraction f(new_up,new_down);
 98     return f;
 99 }
100 
101 Fraction mul(Fraction f1,Fraction f2)
102 {
103     int new_up=f1.up*f2.up;
104     int new_down=f1.down*f2.down;
105     int a,b;
106     a=abs(new_up);b=abs(new_down);
107     while (b)
108     {
109         int tmp=a;
110         a=b;
111         b=tmp%b;
112     }
113     new_up/=a;new_down/=a;
114     Fraction f(new_up,new_down);
115     return f;
116 }
117 
118 Fraction div(Fraction f1,Fraction f2)
119 {
120     
121     int new_up=f1.up*f2.down;
122     int new_down=f1.down*f2.up;
123     int a,b;
124     a=abs(new_up);b=abs(new_down);
125     while (b)
126     {
127         int tmp=a;
128         a=b;
129         b=tmp%b;
130     }
131     new_up/=a;new_down/=a;
132     Fraction f(new_up,new_down);
133     return f;
134 }
View Code

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 }
View Code

运行截图:

 

任务5

代码:

account.h:
 1 #ifndef __ACCOUNT_H__
 2 #define __ACCOUNT_H__
 3 
 4 class SavingAccount{
 5 private:
 6     int id;
 7     double balance;
 8     double rate;
 9     double lastData;
10     double accumulation;
11     
12     static double total;
13     
14     void record(int data,double amount);
15     double accumulate(int data) const
16     {
17         return accumulation+balance*(data-lastData);
18     }
19 public:
20     SavingAccount(int data,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 data,double amount);
26     void withdraw(int data,double amount);
27     void settle(int data);
28     void show() const;
29 }; 
30 #endif
View Code
account.cpp:
 1 #include <bits/stdc++.h>
 2 #include "account.h"
 3 using namespace std;
 4 
 5 double SavingAccount::total=0;
 6 SavingAccount::SavingAccount(int data,int id,double rate)
 7     :id(id),balance(0),rate(rate),lastData(data),accumulation(0)
 8     {
 9         cout<<data<<"\t#"<<id<<"is created"<<endl;
10     }
11     
12 void SavingAccount::record(int data,double amount)
13 {
14     accumulation=accumulate(data);
15     lastData=data;
16     amount=floor(amount*100+0.5)/100;
17     balance+=amount;
18     total+=amount;
19     cout<<data<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
20 }
21 
22 void SavingAccount::deposit(int data,double amount)
23 {
24     record(data,amount);
25 }
26 
27 void SavingAccount::withdraw(int data,double amount)
28 {
29     if (amount>getBalance())
30         cout<<"Error:not enough money"<<endl;
31     else 
32         record(data,-amount);
33 }
34 
35 void SavingAccount::settle(int data)
36 {
37     double interest=accumulate(data)*rate/365;
38     if (interest!=0)
39         record(data,interest);
40     accumulation=0;
41 }
42 void SavingAccount::show() const
43 {
44     cout<<"#"<<id<<"\tBalance:"<<balance;
45 }
View Code
5_11.cpp:
 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7     SavingAccount sa0(1,21325302,0.015);
 8     SavingAccount sa1(1,58320212,0.015);
 9     
10     sa0.deposit(5,5000);
11     sa1.deposit(25,10000);
12     sa0.deposit(45,5500);
13     sa1.deposit(60,4000);
14     
15     sa0.settle(90);
16     sa1.settle(90);
17     
18     sa0.show();cout<<endl;
19     sa1.show();cout<<endl;
20     cout<<"Total:"<<SavingAccount::getTotal()<<endl;
21     return 0;
22 }
View Code

运行截图:

好难

posted @ 2024-10-29 19:50  白鸽nm海鸥  阅读(1)  评论(0编辑  收藏  举报