实验2 类和对象_基础编程1

实验任务1:

实验代码:

 1 #include <string>
 2 
 3 // 类T: 声明
 4 class T {
 5 // 对象属性、方法
 6 public:
 7     T(int x = 0, int y = 0);   // 普通构造函数
 8     T(const T &t);  // 复制构造函数
 9     T(T &&t);       // 移动构造函数
10     ~T();           // 析构函数
11 
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.h
 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 }
t.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 }
task1.cpp

运行结果截图:

问题1:

不能运行。

报错截图:

可能原因:

友元声明在类内,定义在类外。如果不在类外进行普通函数声明的话,那么在task1.cpp进行编译的时候,由于在类外无法访问类内的私有成员,将找不到t.h中关于func 声明。

问题2:

功能:

普通构造函数 T(int x = 0, int y = 0):这是类的默认构造函数,用于初始化对象的成员变量 m1 和 m2。当创建类 T 的对象时,如果没有提供特定的构造函数参数,将调用此构造函数。

复制构造函数 T(const T &t):这个构造函数用于创建一个新对象,它是另一个同类型对象的副本。它通过复制已有对象的成员变量来初始化新对象。

移动构造函数 T(T &&t):移动构造函数用于在不进行复制的情况下,将资源从一个对象转移到另一个对象。它接受一个右值引用作为参数。

析构函数 ~T():析构函数用于执行清理工作,释放资源。

调用时机:

普通构造函数在对象创建时调用。

复制构造函数在对象通过另一个对象初始化时调用。

移动构造函数在对象通过另一个对象的移动初始化时调用。

析构函数在对象被销毁时调用,例如当对象离开作用域或被显式删除时。

问题3:

不能。

因为静态成员变量的定义和初始化应该在类定义之外进行,而不是在类定义内部。

实验任务2:

实验代码:

 1 #pragma once
 2 #include<iostream>
 3 class Complex {
 4 public:
 5     static const std::string doc;
 6     Complex();
 7     Complex(double x);
 8     Complex(double x, double y);
 9     Complex(const Complex& c);
10     double get_real()const;
11     double get_imag()const;
12     void add(const Complex& c);
13     friend Complex add(const Complex& c1, const Complex& c2);
14 
15     friend bool is_equal(const Complex& c1, const Complex& c2);
16 
17     friend bool is_not_equal(const Complex& c1, const Complex& c2);
18 
19     friend void output(const Complex& c);
20 
21     friend double abs(const Complex& c);
22 
23 
24 private:
25     double real;
26     double imag;
27 };
complex.h
 1 #include"complex.h"
 2 #include<iostream>
 3 #include<math.h>
 4 const std::string Complex::doc = "a simplified complex class";
 5 Complex::Complex():real(0), imag(0) {}
 6 Complex::Complex(double x) : real(x), imag(0) {}
 7 Complex::Complex(double x, double y) : real(x), imag(y) {}
 8 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {}
 9 double Complex::get_real() const {
10 
11     return real;
12 
13 }
14 double Complex::get_imag() const {
15 
16     return imag;
17 
18 }
19 void Complex::add(const Complex& other) {
20 
21     real += other.real;
22 
23     imag += other.imag;
24 
25 }
26 Complex add(const Complex& c1, const Complex& c2) {
27 
28     return Complex(c1.real + c2.real, c1.imag + c2.imag);
29 
30 }
31 
32 
33 
34 bool is_equal(const Complex& c1, const Complex& c2) {
35 
36     return (c1.real == c2.real) && (c1.imag == c2.imag);
37 
38 }
39 bool is_not_equal(const Complex& c1, const Complex& c2)
40 {
41     return !is_equal(c1, c2);
42 }
43 double abs(const Complex& c) {
44 
45     return std::sqrt(c.real * c.real + c.imag * c.imag);
46 
47 }
48 void output(const Complex& c) {
49     if (c.imag >= 0) {
50         std::cout << c.real << " + " << std::abs(c.imag) << "i";
51     }
52     else
53     {
54         std::cout << c.real << " - " << std::abs(c.imag) << "i";
55     }
56 
57 }
complex.cpp
#include"complex.h"
#include <iostream>

using std::cout;
using std::endl;
using std::boolalpha;

void test() {
    cout << "类成员测试: " << endl;
    cout << Complex::doc << endl;

    cout << endl;

    cout << "Complex对象测试: " << endl;
    Complex c1;
    Complex c2(3, -4);
    const Complex c3(3.5);
    Complex c4(c3);

    cout << "c1 = "; output(c1); cout << endl;
    cout << "c2 = "; output(c2); cout << endl;
    cout << "c3 = "; output(c3); cout << endl;
    cout << "c4 = "; output(c4); cout << endl;
    cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;

    cout << endl;

    cout << "复数运算测试: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1.add(c2);
    cout << "c1 += c2, c1 = "; output(c1); cout << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
    c4 = add(c2, c3);
    cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
}

int main() {
    test();
}
main.cpp

运行结果截图:

实验任务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 }
task3.cpp

运行结果截图:

问题:

接口:

complex<double> c1使用默认构造函数创建了一个复数对象c1
complex<double> c2(3, -4) 使用初始化列表构造复数对象c2
const complex<double> c3(3.5) 使用构造函数创建了一个常量复数对象c3
complex<double> c4(c3) 使用复制构造函数创建了复数对象c4

使用std::cout和<<操作符直接输出复数对象

c4.real()和 c4.imag() 分别获取复数对象的实部和虚部。

c1 += c2 使用+=运算符将c2加到c1上

(c1 == c2)使用==运算符比较两个复数对象是否相等
(c1 != c3)使用!=运算符比较两个复数对象是否不相等

abs(c2)计算复数对象c2的模;

c4 = c2 + c3使用+运算符将两个复数对象相加

写法更简洁之处:

1.构造函数:

任务2:需要手动定义各种构造函数来初始化复数对象。
任务3:可以直接使用列表初始化或者提供特定的构造函数来创建复数对象,代码更为简洁。
2.运算:

任务2:需要手动实现加法、减法、乘法和除法等运算符的重载。
任务3:标准库complex类已经重载了基本的算术运算符,可以直接使用这些运算符进行复数运算。
3.访问:

任务2:需要通过成员函数(如get_real()和get_imag())来获取实部和虚部。
任务3:可以直接使用.real()和.imag()成员函数来访问复数的实部和虚部,代码更为直观和简洁。
4.输出:

任务2:需要自定义一个output()函数来格式化输出复数。
任务3:可以直接使用std::cout和<<运算符来输出复数
5.模运算:

任务2:需要手动实现复数的模运算。
任务3:可以直接使用标准库提供的abs()函数来计算复数的模。

 启发:标准库complex类的接口设计简洁直观,易于理解和使用,可以使代码更加简洁和直观。

实验任务4:

实验代码:

 1  #pragma once
 2  #include <string>
 3 
 4  class Fraction {
 5  public:
 6          Fraction(int up, int down = 1);
 7         Fraction(const Fraction&);
 8     
 9              int get_up() const;
10          int get_down() const;
11          Fraction negative() const;
12          int gcd()const;
13          friend Fraction add(const Fraction&, const Fraction&);
14         friend Fraction sub(const Fraction&, const Fraction&);
15          friend Fraction mul(const Fraction&, const Fraction&);
16          friend Fraction div(const Fraction&, const Fraction&);
17          friend void output(const Fraction&);
18     
19         
20      static const std::string doc;
21     
22  private:
23         int up;
24          int down;
25     
26 };
Fraction.h
 1  #include "Fraction.h"
 2 #include <iostream>
 3  #include<cmath>
 4  const std::string Fraction::doc = "Fraction类v0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算";
 5 
 6 Fraction::Fraction(int up, int down) : up(up), down(down) {}
 7 
 8 Fraction::Fraction(const Fraction & other) : up(other.up), down(other.down) {}
 9 
10  int Fraction::get_up() const {
11      int a = gcd();
12          return up/a;
13     
14 }
15 
16  int Fraction::get_down() const {
17      int a = gcd();
18      return down/a;
19     
20 }
21 
22  Fraction Fraction::negative() const {
23          return Fraction(-up, down);
24     
25 }
26 
27  Fraction add(const Fraction & f1, const Fraction & f2) {
28         return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down);
29     
30 }
31 
32  Fraction sub(const Fraction & f1, const Fraction & f2) {
33          return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down);
34     
35 }
36 
37  Fraction mul(const Fraction & f1, const Fraction & f2) {
38         return Fraction(f1.up * f2.up, f1.down * f2.down);
39     
40 }
41 
42  Fraction div(const Fraction & f1, const Fraction & f2) {
43          return Fraction(f1.up * f2.down, f1.down * f2.up);
44     
45 }
46 
47  void output(const Fraction & f) {
48          int a = f.down, b = f.up;
49          int c = a, d = b;
50          int temp;
51          while (d != 0)
52              {
53                  temp = c % d;
54                  c = d;
55                  d = temp;
56              }
57          int gcd = c;
58          b /= gcd;
59          a /= gcd;
60          if (a == 0)
61          {
62              std::cout << "分母不能为0";
63              return;
64          }
65          if (a < 0) {
66                  b = -b;
67                  a = -a;
68         
69     }
70          if (b == 0) {
71                  std::cout << "0";
72         
73     }
74     else if (a == 1) {
75                  std::cout << b;
76         
77     }
78     else {
79                  std::cout << b << "/" << a;
80         
81     }
82     
83 }
84  int Fraction::gcd()const 
85  {
86      int c = up;
87      int d = down;
88      int temp;
89      while (d != 0)
90      {
91          temp = c % d;
92          c = d;
93          d = temp;
94      }
95      return c;
96  }
Fraction.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 }
task4.cpp

运行结果截图:

 

实验任务5:

实验代码:

 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);
17     int getId()const{return id;}
18     double getBalance()const{return balance;}
19     double getRate()const{return rate;}
20     static double getTotal(){return total;}
21     void deposit(int date,double amount);
22     void withdraw(int date,double amount);
23     void settle(int date);
24     void show()const;
25     
26 };
27 #endif//_ _ACCOUNT_H_ _
account.h
 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),accumulation(0){
 8     cout<<date<<"\t#"<<id<<" is created"<<endl;
 9 } 
10 void  SavingsAccount::record(int date,double amount){
11     accumulation=accumulate(date);
12     lastDate=date;
13     amount=floor(amount*100+0.5)/100;
14     balance+=amount;
15     total+=amount;
16     cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
17 }
18 void  SavingsAccount::deposit(int date,double amount){
19     record(date,amount);
20 }
21 void  SavingsAccount::withdraw(int date,double amount){
22     if(amount>getBalance())
23         cout<<"Error:not enough money"<<endl;
24     else
25         record(date,-amount);
26 }
27 void  SavingsAccount::settle(int date){
28     double interest=accumulate(date)*rate/365;
29     if(interest!=0)
30         record(date,interest);
31         accumulation=0;
32 }
33 void SavingsAccount::show()const{
34     cout<<"#"<<id<<"\tBalance:"<<balance;
35 }
account.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      sa0.deposit(5,5000);
 9      sa1.deposit(25,10000);
10      sa0.deposit(45,5500);
11      sa1.withdraw(60,4000);
12      sa0.settle(90);
13      sa1.settle(90);
14      sa0.show();cout<<endl;
15      sa1.show();cout<<endl;
16      cout<<"Total:"<< SavingsAccount::getTotal()<<endl;
17      return 0;
18 }
main.cpp
可以在 accumulate() 和 getBalance() 加上 const 修饰符,可以防止数据被更改。
可以在输出时加一些提示语,可以让输出更直观。

总结:

通过本次实验,我明白了使用标准库所带来的简洁,以及知道了怎么通过多文件的方式来编写代码。

 

posted @ 2024-10-24 18:48  宿雨如秋  阅读(7)  评论(0编辑  收藏  举报