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

1,实验任务1

t.cpp

 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.h

 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();

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 << 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中的test函数以及main函数中调用了func函数,如果类外部不声明func函数,编译器无法识别该函数的定义位置

 

普通构造函数:T(int x=0,int y=0)

2.实验任务2

Complex.h

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

Complex.cpp

 1 #include "Complex.h"
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 const std::string Complex::doc = "a simplified complex class";
 6 
 7 Complex::Complex(double r, double i) : real{r}, imag{i} {}
 8 Complex::Complex(const Complex& c) : real{c.real}, imag{c.imag} {}
 9 double Complex::get_real() const { return real; }
10 double Complex::get_imag() const { return imag; }
11 Complex Complex::add(const Complex& c) const {
12     return Complex(real + c.real, imag + c.imag);
13 }
14 Complex add(const Complex& c1, const Complex& c2) {
15     return Complex(c1.real + c2.real, c1.imag + c2.imag);
16 }
17 bool is_equal(const Complex& c1, const Complex& c2) {
18     return c1.real == c2.real && c1.imag == c2.imag;
19 }
20 bool is_not_equal(const Complex& c1, const Complex& c2) {
21     return!(c1.real == c2.real && c1.imag == c2.imag);
22 }
23 double abs(const Complex& c) {
24     return std::sqrt(c.real * c.real + c.imag * c.imag);
25 }
26 void output(const Complex& c) {
27     std::cout << c.real;
28     if (c.imag >= 0) std::cout << "+";
29     std::cout << c.imag << "i";
30 }

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

 

 

构造函数:使用了带参数的构造函数来创建对象

访问实部和虚部的接口:c4.real()和c4.imag()用于获取复数对象的实部和虚部

加法运算接口  自加运算接口 相等判断接口 取模运算接口

对比任务2 代码写法不同更加简洁

实验任务4

Fraction.h

 1 #pragma once
 2 #include <string>
 3 
 4 class Fraction {
 5 public:
 6     // 类的描述信息
 7     static const std::string doc;
 8     // 构造函数
 9     Fraction(int up = 0, int down = 1);
10     Fraction(const Fraction& f);
11     // 接口函数
12     int get_up() const;
13     int get_down() const;
14     Fraction negative() const;
15     // 友元函数声明
16     friend void output(const Fraction& f);
17     friend Fraction add(const Fraction& f1, const Fraction& f2);
18     friend Fraction sub(const Fraction& f1, const Fraction& f2);
19     friend Fraction mul(const Fraction& f1, const Fraction& f2);
20     friend Fraction div(const Fraction& f1, const Fraction& f2);
21 private:
22     int up;
23     int down;
24 };

Fraction.cpp

#include "Fraction.h"
#include <iostream>

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

void test1() {
    cout << "Fraction类测试: " << endl;
    cout << Fraction::doc << endl << endl;
    Fraction f1(5);
    Fraction f2(3, -4), f3(-18, 12);
    Fraction f4(f3);
    cout << "f1 = "; output(f1); cout << endl;
    cout << "f2 = "; output(f2); cout << endl;
    cout << "f3 = "; output(f3); cout << endl;
    cout << "f4 = "; output(f4); cout << endl;
    Fraction f5(f4.negative());
    cout << "f5 = "; output(f5); cout << endl;
    cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
        f5.get_down() << endl;
    cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
    cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
    cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
    cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
    cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
}
void test2() {
    Fraction f6(42, 55), f7(0, 3);
    cout << "f6 = "; output(f6); cout << endl;
    cout << "f7 = "; output(f7); cout << endl;
    cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
}
int main() {
    cout << "测试1: Fraction类基础功能测试\n";
    test1();
    cout << "\n测试2: 分母为0测试: \n";
    test2();
    return 0;
}

实验任务5

 account.h

 1 #include<iostream>
 2 
 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     
12     void record(int date,double amount);
13     double accumulate(int date) const{
14         return accumulation+balance*(date-lastDate);
15     }
16 
17 public:
18     SavingsAccount(int date,int id,double rate);
19     int getId() const {return id;}
20     double getBalance() const{return balance;}
21     double getRate() const{return rate;}
22     static double getTotal(){return total;}
23     void deposit(int rate,double amount);
24     void withdraw(int rate,double amount);
25     void settle(int date);
26     void show() const;
27     
28 };

 

account.cpp

#include "account.h"
#include<cmath>
#include<iostream>

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

double SavingsAccount::total=0;

SavingsAccount::SavingsAccount(int date,int id,double rate)
    :id(id),balance{0},rate{rate},lastDate{date},accumulation{0}{
    cout<<date<<"\t#"<<id<<"is created"<<endl;    
}

void SavingsAccount::record(int date,double amount){
    accumulation=accumulate(date);
    lastDate=date;
    amount=floor(amount*100+0.5)/100;
    balance+=amount;
    total+=amount;
    cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}

void SavingsAccount::deposit(int date,double amount){
    record(date,amount);
}

void SavingsAccount::withdraw(int date,double amount){
    if(amount>getBalance()){
        cout<<"Erroe:not enough money"<<endl;
    }
    else{
        record(date,-amount);
    }
}

void SavingsAccount::settle(int date){
    double interest=accumulate(date)*rate/365;
    if(interest!=0){
        record(date,interest);
    }
    accumulation=0;
}

void SavingsAccount::show() const{
    cout<<"#"<<id<<"\tBalance:"<<balance;
}

 

 

5_11.cpp

#include "account.h"
#include<iostream>

using namespace std;

int main(){
    SavingsAccount sa0(1,21325302,0.015);
    SavingsAccount sa1(1,58320212,0.015);
    
    sa0.deposit(5,5000);
    sa1.deposit(25,10000);
    sa0.deposit(45,5500);
    sa1.withdraw(60,4000);
    
    sa0.settle(90);
    sa1.settle(90);
    
    sa0.show();cout<<endl;
    sa1.show();cout<<endl;
    
    cout<<"Total:"<<SavingsAccount::getTotal()<<endl;
    
    return 0;
}

 

posted @ 2024-10-23 15:11  qc798w3  阅读(14)  评论(0编辑  收藏  举报