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

news/2024/10/23 18:57:23/文章来源:https://www.cnblogs.com/sqmylc/p/18493603

实验任务1

代码:

t.h:

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

t.cpp:

 1 #include"t.h"
 2 #include<iostream>
 3 #include<string>
 4 
 5 using std::cout;
 6 using std::endl;
 7 using std::string;
 8 
 9 const std::string T::doc{ "a simple class sample" };
10 const int T::max_cnt = 999;
11 int T::cnt = 0;
12 
13 T::T(int x, int y) :m1{ x }, m2{ y } {
14     ++cnt;
15     cout << "T constructor called.\n";
16 }
17 
18 T::T(const T& t) :m1{ t.m1 }, m2{ t.m2 } {
19     ++cnt;
20     cout << "T copy constructor called.\n";
21 }
22 
23 T::T(T&& t) :m1{ t.m1 }, m2{ t.m2 } {
24     ++cnt;
25     cout << "T move constructor called.\n";
26 }
27 
28 T::~T() {
29     --cnt;
30     cout << "T destructor called.\n";
31 }
32 
33 void T::adjust(int ratio) {
34     m1 *= ratio;
35     m2 *= ratio;
36 }
37 
38 void T::display()const {
39     cout << "(" << m1 << "," << m2 << ")";
40 }
41 
42 int T::get_cnt() {
43     return cnt;
44 }
45 
46 void func() {
47     T t5(42);
48     t5.m2 = 2049;
49     cout << "t5="; t5.display(); cout << endl;
50 }

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;
20 
21     T t1;
22     cout << "t1="; t1.display(); cout << endl;
23 
24     T t2(3, 4);
25     cout << "t2="; t2.display(); cout << endl;
26 
27     T t3(t2);
28     t3.adjust(2);
29     cout << "t3="; t3.display(); cout << endl;
30 
31     T t4(std::move(t2));
32     cout << "t3="; t4.display(); cout << endl;
33 
34     cout << "T objects'current count:" << T::get_cnt() << endl;
35 
36     func();
37 }

运行截图:

问题1:

 不能去掉,因为func在类内声明了友元函数,但没有声明为普通函数,删掉会显示找不到此函数。

问题2:line9是普通构造函数,给定了两个默认的参数初始值。在系统定义类的对象时,如果定义形式是T t(括号内可以有0到两个参数),就调用该函数。

line10是复制构造函数,将t指向调用时输入的对象,但不改变其的值。

line11是移动构造函数,通过移动而不复制实参的方式提高运行效率。

line12是析构函数,之前的各类构造函数调用完成后,调用该函数,释放他们的空间。一般在主函数结束后调用。

问题3:不能

实验任务2

代码:

complex.h:

 1 #pragma once
 2 #include<string>
 3 class Complex {
 4 public:
 5     static std::string doc;
 6     Complex(double x1 = 0, double  y1 = 0);
 7     Complex(const Complex& c);
 8     ~Complex() {}
 9     double get_real() ;
10     double get_imag() ;
11     void add(const Complex &d) ;
12     friend Complex add(const Complex &e, const Complex &f);
13     friend bool is_equal(const Complex &e,const Complex &f);
14     friend bool is_not_equal(const Complex &e,const Complex &f);
15     friend void output(const Complex& e);
16     friend double abs(const Complex &g);
17 
18 private:
19     double x, y;
20 };
21 
22 bool is_equal(const Complex &e,const Complex&f);
23 bool is_not_equal(const Complex &e,const Complex&f);
24 void output(const Complex& e);

complex.cpp:

 1 #include"complex.h"
 2 #include<iostream>
 3 #include<string>
 4 Complex::Complex(double x1, double y1):x{ x1 }, y{ y1 } {}
 5 
 6 Complex::Complex(const Complex& c) :x{ c.x }, y{ c.y } {}
 7 double Complex::get_real() {
 8     return x;
 9 }
10 double Complex::get_imag() {
11     return y;
12 }
13 void Complex::add(const Complex& d) {
14     x += d.x;
15     y += d.y;
16 }
17 Complex add(const Complex& e,const Complex& f) {
18     Complex h;
19     h.x = e.x + f.x;
20     h.y = e.y + f.y;
21     return h;
22 }
23 bool is_equal(const Complex &e,const Complex &f) {
24     return e.x == f.x && e.y == f.y;
25 }
26 bool is_not_equal(const Complex& e, const Complex& f) {
27     return e.x != f.x || e.y != f.y;
28 }
29 
30 void output(const Complex& e) {
31     std::cout << e.x << e.y << "i";
32 }
33 double abs(const Complex& g) {
34     return sqrt(g.x * g.x + g.y * g.y);
35 }
36  std::string Complex::doc = "a simplified complex class";

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

代码:

 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

代码:

Fraction.h:

 1 #pragma once
 2 #include<string>
 3 
 4 class Fraction {
 5 public:
 6     static const std::string doc;
 7     Fraction(int x=1, int y = 1);
 8     Fraction(const Fraction& p);
 9     int get_up();
10     int get_down();
11     Fraction negative();
12 
13 public:
14     friend void output(Fraction p);
15     friend Fraction add(const Fraction& a, const Fraction& b);
16     friend Fraction sub(const Fraction& a, const Fraction& b);
17     friend Fraction mul(const Fraction& a, const Fraction& b);
18     friend Fraction div(const Fraction& a, const Fraction& b);
19 
20 private:
21     int up, down;
22 
23 };
24 
25 void output( Fraction p);
26 
27 
28 int computeGCD(int a, int b);

Fraction.cpp:

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

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 }

运行截图:

实验任务5

代码:

account.h:

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

account.cpp:

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

5_11.cpp:

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

运行截图:

实验总结:在类的定义中有许多小细节,要十分注意,不然报错一大堆就会很头疼。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/820790.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第一次团队作业——“行趣”智能旅游软件

作业所属的课程 软件工程2024作业要求 2024秋软工实践团队作业-第一次作业的目标 开发一款基于LLM大模型接口的软件,为这个软件做需求分析团队名称 十光年团队成员学号-姓名 施靖杰-102201327邓才慧-102201102陈宇尧-102201119陆旭东-102201118黄宇舟-102201331邱予-102202121…

24.10.23

出题人可能原神玩多了。 A 乘法少说要乘个 \(2\) 才有意义,所以答案是 \(\log\) 量级,直接 dp 设 \(f_{i,j}\) 表示算出 \(i\) 用了 \(j\) 个乘号最少用几个加号。 一个性质是,如果相加那么较小数不超过 \(4\),然后直接枚举加多少乘多少刷表。 f[1][0] = 0; rep(i, 1, n) r…

七月在线公开课笔记-二十三-

七月在线公开课笔记(二十三) 人工智能—机器学习中的数学(七月在线出品) - P1:Taylor展式与拟牛顿 - 七月在线-julyedu - BV1Vo4y1o7t1 这次我们探讨它的展示与它的相关应用,如米牛顿。我们首先给出塔的展示的本身的,它的定义,它的展示的公式的本身。然后我们利用它来计…

七月在线公开课笔记-二十七-

七月在线公开课笔记(二十七) 人工智能—机器学习公开课(七月在线出品) - P25:【公开课】数据挖掘与机器学习基础 - 七月在线-julyedu - BV1W5411n7fg 可以是吧?好,那么我们稍等一下啊,稍等一下我们。在8点钟我们就准时开始我们的一个直播的内容。对。那么各位同学之前有…

七月在线公开课笔记-二十六-

七月在线公开课笔记(二十六) 人工智能—机器学习公开课(七月在线出品) - P18:世界杯数据分析案例 - 七月在线-julyedu - BV1W5411n7fg 然后我们来做个分析吧,所以大家喜欢做一些比赛的分析,对吧?然后大家最关注的当然是决赛半决赛啊,可能也多多分析一下,分析到4分之1…

10.22-10.23

A.异或和 CF1261F 做过类似的题的话,\(O(n^2\log^2v\log(n^2\log^2v))\) 应该算是暴力分了。 显然这过不了,不然就不是 *3100 了。 主要的瓶颈在于异或完后产生了大量的线段,而且里面大多数是没用的。 于是赛时写出了一个绝唐的优化点击查看代码 for (int i = 0;i < seg[…

window下安装并使用nvm(含卸载node、卸载nvm、全局安装npm),解决老代码使用的node.js 比较旧

一、卸载node如果你已经安装了node,那么你需要先卸载node(不然安装nvm可能会失败),如果你没有安装那直接跳过这一步到下一步。 打开控制面板 -> 打开程序和功能 -> 右上角搜索输入node -> 右键卸载 为了确保彻底删除node在看看你的node安装目录中还有没有node文件…

1024 程序员节,我做了个闯关小游戏!

1024 程序员节到了,首先祝各位程序员们节日快乐,代码零 Bug!大家好,我是程序员鱼皮。1024 程序员节到了,首先祝各位程序员们节日快乐,代码零 Bug! 在这个特殊的日子,为了帮助大家轻松了解计算机编程相关的实用知识,帮助程序员朋友们巩固基础、检验自己的技术水平,我带…

ResNet50

1、查找最优的ResNet50预训练版本 从具体的预训练模型目录 <no title> — Torchvision 0.20 documentation 中,可以知道表现最好的ResNet50版本2、加载ResNet50预训练模型 加载预训练模型使用TorchVision方式,torch.hub.load方式本文不再研究。from torchvision import…

如何将MySQL数据集成到金蝶云星空以实现生产领料单新增

MySQL数据集成到金蝶云星空:SLD生产领料单新增深圳天一-单工序-好 在企业信息化系统中,数据的高效流转和准确对接是业务运作的关键。本文将分享一个具体的技术案例,展示如何通过轻易云数据集成平台,将MySQL中的数据无缝集成到金蝶云星空,实现SLD生产领料单新增深圳天一-单…

免费地图资源发布、下载、压缩

Google等无法正常访问的资源需要代理,免费、不限速、无需安装、无需注册。压缩包210MB,包含离线地图。 地图资源管理系统提供高效的地图瓦图资源管理,集成了资源发布、下载和压缩三大功能。通过WMS,用户可以发布瓦图、高程数据等资源,支持灵活的黑白名单管理,确保数据的安…

PbootCMS 测试发送邮件提示“发送失败: 503 Error: need EHLO and AUTH first!”的解决办法

问题表现在 PbootCMS 中测试发送邮件时,提示“发送失败: 503 Error: need EHLO and AUTH first!”。原因邮箱登录需要设置安全码,而不是使用邮箱密码。解决方法获取邮箱的安全码,并在 PbootCMS 的邮件配置中使用安全码代替邮箱密码。扫码添加技术【解决问题】专注中小企业网…