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

1. 实验任务1
分别给出t.h, t.cpp, task1.cpp源码,以及,运行测试结果截图
t.h
 1 //t.h内容为类T的申明,友元函数的声明
 2 
 3 #pragma once
 4 
 5 #include <string>
 6 
 7 class T {
 8 
 9     public:
10         T(int x=0, int y=0);
11         T(const T &t);
12         T(T &&t);
13         ~T();
14 
15         void adjust(int ratio);
16         void display() const;
17 
18     private:
19         int m1,m2;
20 
21     public:
22         static int get_cnt();
23         
24     public:
25         static const std::string doc;
26         static const int max_cnt;
27     
28     private:
29         static int cnt;
30             
31         friend void func();
32 };
33 
34 void func(); 
View Code

t.cpp

 1 //类T实现
 2 
 3 #include"t.h"
 4 #include<iostream>
 5 #include <string>
 6 
 7 using std::cout;
 8 using std::endl;
 9 using std::string;
10 
11 //static成员数据类外初始化
12 const std::string T::doc {
13     "a simple class sample"
14 };
15 const int T::max_cnt=999;
16 int T::cnt =0;
17 
18 //对象方法
19 T::T(int x,int y):m1 {x},m2 {y} {
20     ++cnt;
21     cout << "T constructor called.\n";
22 }
23 T::T(const T&t):m1 {t.m1},m2 {t.m2} {
24     ++cnt;
25     cout << "T copy constructoor called. \n";
26 }
27 T::T(T &&t):m1 {t.m1},m2 {t.m2} {
28     ++cnt;
29     cout << "T move constructor called. \n";
30 }
31 T::~T(){
32     --cnt;
33     cout << "T destructor called. \n";
34 }
35 
36 void T::adjust(int ratio){
37     m1*=ratio;
38     m2*=ratio;
39 }
40 
41 void T::display() const{
42    cout << "(" << m1 << ", " << m2 << ")" ;
43 }
44 
45 //类方法
46 int T::get_cnt(){
47     return cnt;
48 } 
49 
50 //友元
51 void func(){
52     T t5(42);
53     t5.m2=2049;
54     cout<< "t5 = "; t5.display(); cout << endl;
55 
56 } 
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 T t1;
21 cout << "t1 = "; t1.display(); cout << endl;
22 T t2(3, 4);
23 cout << "t2 = "; t2.display(); cout << endl;
24 T t3(t2);
25 t3.adjust(2);
26 cout << "t3 = "; t3.display(); cout << endl;
27 T t4(std::move(t2));
28 cout << "t3 = "; t4.display(); cout << endl;
29 cout << "T objects'current count: " << T::get_cnt() << endl;
30 func();
31 }
View Code

问题1:
t.h中,普通函数 func 作为类X的友元,在类的内部声明了友元关系。在类外部,去掉
line36,重新编译,是否能正确运行。如果能,回答说明可以去掉line36。如果不能,以截图
形式给出编译报错信息,分析可能的原因。
不能去掉,因为只是在类中将func声明为友元函数,但是func函数本身还没有声明。
 
问题2:
t.h中,line9-12给出了各种构造函数、析构函数。总结各种构造函数的功能,以及它们与析
构函数的调用时机
默认构造函数:用于创建对象时进行基本初始化,通常将数据成员初始化为默认值,当使用无参数形式创建对象时被调用;

一般构造函数:接收参数以初始化对象的成员变量。允许创建对象时根据需要提供特定的初始值。当创建对象时传入参数,函数被调用;

复制构造函数:用于创建一个新对象,该对象是通过复制已有对象的状态来初始化的。当对象以值的形式传递给函数时,当对象从函数返回时,当需要从现有对象创建新的对象时,该函数被调用;

析构函数:析构函数用于释放对象所占用的资源,当对象的生命周期结束时,自动调用析构函数。

 

问题3:
t.cpp中,line13-15,调整到t.h,重新编译,程序能否正确编译运行。
可以。
 
2. 实验任务2
此部分书写内容:
分别给出Complex.h, Complex.cpp, task2.cpp源码,及,运行测试结果截图
Complex.h
 1 #pragma once
 2 
 3 #include<string>
 4 
 5 //类Complex声明
 6 class Complex{
 7     
 8 public:
 9     Complex(double x=0,double y=0);
10     Complex(const Complex&c);
11     ~Complex();
12     
13     void add(const Complex&c);
14     
15 public:
16     
17    static const std::string doc;
18    int get_real();
19    int get_imag();
20    
21 private:       
22     double real;
23     double imag;
24     
25     friend Complex add(const Complex&c1,const Complex&c2);
26     friend bool    is_equal(const Complex&c1,const Complex&c2);
27     friend bool    is_not_equal(const Complex&c1,const Complex&c2);
28     friend void    output(const Complex&c);
29     friend double abs(const Complex&c);
30 }; 
31 
32 Complex add(const Complex&c1,const Complex&c2);
33 bool    is_equal(const Complex&c1,const Complex&c2);
34 bool    is_not_equal(const Complex&c1,const Complex&c2);
35 void    output(const Complex&c);
36 double abs(const Complex&c);
View Code

Complex.cpp

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

task2.cpp 

 1 // 待补足头文件
 2 #include "Complex.h"
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 void test() {
 8 cout << "类成员测试: " << endl;
 9 cout << Complex::doc << endl;
10 cout << endl;
11 cout << "Complex对象测试: " << endl;
12 Complex c1;
13 Complex c2(3, -4);
14 const Complex c3(3.5);
15 Complex c4(c3);
16 cout << "c1 = "; output(c1); cout << endl;
17 cout << "c2 = "; output(c2); cout << endl;
18 cout << "c3 = "; output(c3); cout << endl;
19 cout << "c4 = "; output(c4); cout << endl;
20 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag()<< endl;
21 cout << endl;
22 cout << "复数运算测试: " << endl;
23 cout << "abs(c2) = " << abs(c2) << endl;
24 c1.add(c2);
25 cout << "c1 += c2, c1 = "; output(c1); cout << endl;
26 cout << boolalpha;
27 cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
28 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
29 c4 = add(c2, c3);
30 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
31 }
32 int main() {
33 test();
34 }
View Code

 

3. 实验任务3
此部分书写内容:
给出程序task3.cpp源码,及,运行测试结果截图
 1 #include <iostream>
 2 #include <complex>
 3 
 4 using namespace std;
 5 
 6 void test (){
 7     cout <<"标准库模版类complex测试:"<<endl;
 8     complex<double> c1;
 9     complex<double> c2(3,-4);
10     const complex<double> c3(3.5);
11     complex <double> c4(c3);
12     
13     cout << "c1 = " << c1 << endl;
14 cout << "c2 = " << c2 << endl;
15 cout << "c3 = " << c3 << endl;
16 cout << "c4 = " << c4 << endl;
17 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() <<
18 endl;
19 cout << endl;
20 cout << "复数运算测试: " << endl;
21 cout << "abs(c2) = " << abs(c2) << endl;
22 c1 += c2;
23 cout << "c1 += c2, c1 = " << c1 << endl;
24 cout << boolalpha;
25 cout << "c1 == c2 : " << (c1 == c2) << endl;
26 cout << "c1 != c3 : " << (c1 != c3) << endl;
27 c4 = c2 + c3;
28 cout << "c4 = c2 + c3, c4 = " << c4 << endl;
29 
30 }
31 
32 int main() {
33 test();
34 }
View Code

 

4. 实验任务4
此部分书写内容:
分别给出程序Fraction.h, Fraction.cpp, task4.cpp源码,及,运行测试结果截图

 Fraction.h

 1 #pragma once 
 2  
 3 #include<string>
 4 using namespace std; 
 5 class  Fraction{
 6 
 7 public:    
 8     Fraction(int x=0,int y=1);
 9     Fraction(const Fraction &f);
10     ~Fraction();
11     
12     int get_up();
13     int get_down() ;
14     Fraction negative() const; 
15 private:
16     int m1;    
17     int m2;
18     void simp(); 
19 public:    
20     static const string doc;     
21 private:    
22     friend void  output(const Fraction &f);
23     friend Fraction add(const Fraction &f1,const Fraction &f2);
24     friend Fraction sub(const Fraction &f1,const Fraction &f2);
25     friend Fraction mul(const Fraction &f1,const Fraction &f2);
26     friend Fraction div(const Fraction &f1,const Fraction &f2);
27     
28     
29 };
30 
31 void output(const Fraction &f);
32 Fraction add(const Fraction &f1,const Fraction &f2);
33 Fraction sub(const Fraction &f1,const Fraction &f2);
34 Fraction mul(const Fraction &f1,const Fraction &f2);
35 Fraction div(const Fraction &f1,const Fraction &f2);
View Code

Fraction.cpp

 1 #include "Fraction.h"
 2 #include <cmath>
 3 #include<string>
 4 #include<iostream>
 5 
 6 using namespace std;
 7 
 8 const string Fraction::doc {
 9     "目前仅支持分数对象的构造、输出、加/减/乘/除运算."
10 };
11 
12 Fraction::Fraction(int x,int y):m1 {x},m2 {y} {
13     simp();
14 }
15 Fraction::Fraction(const Fraction &f):m1 {f.m1},m2 {f.m2} {
16     simp();
17 }
18 Fraction::~Fraction() {}
19 
20 void Fraction::simp() {
21     if (m2 < 0) {
22         m1 = -m1;
23         m2 = -m2;
24     }
25     bool sign = false;
26     if (m1 < 0) sign = true;
27     int t_m1 = abs(m1);
28     for (int i = t_m1; i > 1; i--) {
29         if (m2 % i == 0 && t_m1 % i == 0) {
30             t_m1 /= i;
31             m2 /= i;
32             if (sign)
33                 m1 = -t_m1;
34             else
35                 m1 = t_m1;
36             return;
37         }
38     }
39 }
40 int Fraction::get_up() {
41     return m1;
42 }
43 
44 int Fraction::get_down() {
45     return m2;
46 }
47 
48 Fraction Fraction::negative() const {
49     return Fraction(-m1,m2);
50 }
51 
52 void output(const Fraction& f) {
53      if (f.m1 == 0)
54         cout << 0;
55     else if (f.m2 == 0)
56         cout << "分母不能为0";
57     else if (f.m2==1)
58         cout << f.m1;   
59     else
60         cout << f.m1 << "/" << f.m2;
61 }
62 
63 Fraction add(const Fraction &f1,const Fraction &f2) {
64     int num = f1.m1 * f2.m2 + f2.m1 * f1.m2;
65     int denom = f1.m2 * f2.m2;
66     Fraction ff(num, denom);
67     ff.simp();
68     return ff;
69 }
70 Fraction sub(const Fraction &f1,const Fraction &f2) {
71     int num = f1.m1 * f2.m2 - f2.m1 * f1.m2;
72     int denom = f1.m2 * f2.m2;
73     Fraction ff(num, denom);
74     ff.simp();
75     return ff;
76 }
77 Fraction mul(const Fraction &f1,const Fraction &f2) {
78     Fraction ff(f1.m1*f2.m1,f1.m2*f2.m2);
79     ff.simp();
80     return ff;
81 }
82 Fraction div(const Fraction &f1,const Fraction &f2) {
83     Fraction ff(f1.m1*f2.m2,f1.m2*f2.m1);
84     ff.simp();
85     return ff;
86 }
View Code

task4.cpp

 1 #include "Fraction.h"
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 void test1() {
 6 cout << "Fraction类测试: " << endl;
 7 cout << Fraction::doc << endl << endl;
 8 Fraction f1(5);
 9 Fraction f2(3, -4), f3(-18, 12);
10 Fraction f4(f3);
11  cout << "f1 = "; output(f1); cout << endl;
12 cout << "f2 = "; output(f2); cout << endl;
13 cout << "f3 = "; output(f3); cout << endl;
14 cout << "f4 = "; output(f4); cout << endl;
15 Fraction f5(f4.negative());
16 cout << "f5 = "; output(f5); cout << endl;
17 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
18 f5.get_down() << endl;
19 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
20 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
21 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
22 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
23 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
24 }
25 void test2() {
26 Fraction f6(42, 55), f7(0, 3);
27 cout << "f6 = "; output(f6); cout << endl;
28 cout << "f7 = "; output(f7); cout << endl;
29 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
30 }
31 int main() {
32 cout << "测试1: Fraction类基础功能测试\n";
33 test1();
34 cout << "\n测试2: 分母为0测试: \n";
35 test2();
36 }
View Code

 

5. 实验任务5
此部分书写内容:
分别给出程序account.h, account.cpp, 5_11.cpp源码,及,运行测试结果截图
对于教材上目前类的设计,包括接口和类内部的计算模块,是否合理?对于一些代码的具体实现,
你是否有疑问,有没有需要改进的部分?

 account.h

 1 //account.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 
13     void record(int data, double amount);
14     double accumulate(int data)const {
15         return accumulation + balance * (data - lastDate);
16     }
17 public:
18     SavingsAccount(int data, int id, double rate);
19     int getId()const { return id; }
20     double getBalance()const { return balance; }
21     static double getTotal() { return total; }
22     void deposit(int data, double amount);
23     void withdraw(int data, double amount);
24     void settle(int data);
25     void show()const;
26 };
27 #endif//__ACCOUNT_H__
View Code

 account.cpp

 1 #include "account.h"
 2 #include<iostream>
 3 #include<cmath>
 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 > 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 }
View Code

 5_11.cpp

 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     SavingsAccount sa0(1, 21325302, 0.015);  
 6     SavingsAccount sa1(1, 58320212, 0.015);
 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 }
View Code

 可以使用const修饰符,防止数据被修改,提高安全性。

提示语与标注也可以添加一些,使输出更直观,修改程序更方便。

 

 

posted @ 2024-10-24 19:40  Mikuuuu  阅读(9)  评论(0编辑  收藏  举报