首页 > 编程语言 >实验2 类和对象_基础编程1

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

时间:2024-10-26 15:12:02浏览次数:4  
标签:std const 对象 编程 int Complex 实验 Fraction include

实验任务一

源码

 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;//以(m1,m2)形式显示对象信息
13 
14 private:
15     int m1, m2;
16 
17 public:
18     static int get_cnt();//显示对象总数
19 
20 public:
21     static const std::string doc;//描述信息
22     static const int max_cnt;//对象上限
23 
24 private:
25     static int cnt;//对象数目
26     friend void func();
27 };
28 
29 void func();
t.h
 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 // static成员数据类外初始化
10 const std::string T::doc{ "a simple class sample" };
11 const int T::max_cnt = 999;
12 int T::cnt = 0;
13 
14 
15 // 对象方法
16 T::T(int x, int y) : m1{ x }, m2{ y } {
17     ++cnt;
18     cout << "T constructor called.\n";
19 }
20 
21 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } {
22     ++cnt;
23     cout << "T copy constructor called.\n";
24 }
25 
26 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } {
27     ++cnt;
28     cout << "T move constructor called.\n";
29 }
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 }
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 << "t4 = "; t4.display(); cout << endl;
34 
35     cout << "T objects'current count: " << T::get_cnt() << endl;
36 
37     func();
38 }
task1.cpp

运行结果

问题一:

 如果func()没有被调用,并且没有其他地方依赖于它的实现,那么从项目中移除func()的定义或调用通常不会影响编译和运行。然而,如果func()的声明(作为友元)被保留,但实现被移除,并且其他代码(如测试代码或未来的开发)试图调用func(),那么这将导致链接错误,因为找不到func()的实现。

问题二:

1.T(int x = 0, int y = 0);为带有默认参数的默认构造函数。

    功能:当创建对象时没有提供任何初始化参数时,使用此构造函数。它允许对象以某种默认状态被创建。

    调用时机:在创建对象时,如果没有提供足够的参数来匹配其他构造函数,则调用默认构造函数。

2.T(const T& t);为拷贝构造函数。

    功能:用于创建一个新对象,作为另一个同类型对象的副本。它接受一个对同类型对象的常量引用作为参数。

    调用时机:当使用另一个同类型对象初始化新对象时(如T obj2 = obj1;);

                      当对象作为函数参数按值传递时;

                       当对象从函数按值返回时。

3.T(T&& t);为移动构造函数。

    功能:从t中窃取资源并初始化新对象,为避免不必要的资源复制

    调用时机:当使用std::move显式地将对象转换为右值,并用于初始化新对象时。

4.~T();为析构函数。

    功能:析构函数在对象生命周期结束时自动调用,用于执行清理操作,如释放对象占用的资源(如动态分配的内存、文件句柄等)。

    调用时机:当对象的生命周期结束时,如离开作用域(对于局部变量)、被删除(对于动态分配的对象)、或容器(如std::vector)被销毁且包含该类型的对象时。

问题三:

不行

 

 

实验任务二

 源码

 1 #pragma once
 2 
 3 #include<string>
 4 
 5 class Complex {
 6 public:
 7     static const std::string doc;
 8 private:
 9     double real; double imag;
10 public:
11     Complex(double r = 0, double i = 0);
12     Complex(const Complex& c);
13 
14     double get_real()const;
15     double get_imag()const;
16 
17     void add(const Complex& other);
18 
19     friend Complex add(const Complex& c1, const Complex& c2);
20     friend bool is_equal(const Complex& c1, const Complex& c2);
21     friend bool is_not_equal(const Complex& c1, const Complex& c2);
22     friend double abs(const Complex& c);
23     friend void output(const Complex& c);
24 };
Complex.h
 1 #include"Complex.h"
 2 #include<iostream>
 3 #include<string>
 4 
 5 using std::cout;
 6 using std::endl;
 7 using std::cin;
 8 using std::string;
 9 
10 const std::string Complex::doc{ "a simplified complex class" };
11 
12 Complex::Complex(double r,double i):real{r},imag{i}{}
13 Complex::Complex(const Complex& comp) :real{ comp.real }, imag{ comp.imag } {};
14 
15 double Complex::get_real()const { return real; }
16 double Complex::get_imag()const { return imag; }
17 
18 void Complex::add(const Complex& other) {
19     real += other.real;
20     imag += other.imag;
21 }
22 
23 Complex add(const Complex& c1, const Complex& c2) {
24     return Complex(c1.real + c2.real, c1.imag + c2.imag);
25 }
26 
27 bool is_equal(const Complex& c1, const Complex& c2) {
28     if (c1.real == c2.real)
29         if (c1.imag == c2.imag)
30             return true;
31     return false;
32 }
33 
34 bool is_not_equal(const Complex& c1, const Complex& c2) {
35     if (c1.real == c2.real)
36         if (c1.imag == c2.imag)
37             return false;
38     return true;
39 }
40 
41 double abs(const Complex& c) {
42     return sqrt(c.real * c.real + c.imag * c.imag);
43 }
44 
45 void output(const Complex& c) {
46     cout << c.real << "+" << c.imag;
47 }
Complex.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 }
task2.cpp

运行结果

 

 

实验任务三

源码

 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 << "标准库模板类complex测试:" << 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

运行结果

 思考:

1.提供了:构造函数(能自定义对象类型),取模,比较

2.可以提供接口自定义对象类型

 

 

实验任务四

源码

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<algorithm>
 5 
 6 class Fraction {
 7 private:
 8     int up;
 9     int down;
10 public:
11     static const std::string doc;
12 
13     Fraction(int u = 0, int d = 1);
14     Fraction(const Fraction& other);
15 
16     int get_up() const;
17     int get_down() const;
18 
19     Fraction negative() const;
20 
21     friend void simplify(Fraction&f);
22 
23     friend void output(const Fraction& f);
24     friend Fraction add (const Fraction& f1, const Fraction& f2);
25     friend Fraction sub(const Fraction& f1, const Fraction& f2);
26     friend Fraction mul(const Fraction& f1, const Fraction& f2);
27     friend Fraction div(const Fraction& f1, const Fraction& f2);
28 
29 };
Fraction.h
 1 #include"Fraction.h"
 2 #include<iostream>
 3 
 4 
 5 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
 6 
 7 Fraction::Fraction(int u, int d) :up(u), down(d) { simplify(*this); };
 8 //用this指针
 9 Fraction::Fraction(const Fraction& other) :up(other.up), down(other.down) {};
10 
11 int Fraction::get_up() const{
12     return up;
13 }
14 int Fraction::get_down() const{
15     return down;
16 }
17 
18 //求负分数
19 Fraction Fraction:: negative() const{
20     return Fraction(-up, -down);
21 }
22 
23 void simplify(Fraction& f) {
24     //化简,先求最大公约数
25     int a = f.up, b = f.down;
26     int temp;
27     while (b != 0) {
28         temp = b;
29         b = a % b;
30         a = temp;
31     }
32     int gcd = a;//最大公约数
33     f.up /= gcd;
34     f.down /= gcd;
35 
36     //确保分母为正
37     if (f.down < 0) {
38         f.up = -f.up;
39         f.down = -f.down;
40     }
41 }
42 
43 //输出分数
44 void output(const Fraction& f) {
45     if (f.down == 0)return;
46     if (f.down == 1) {
47         std::cout << f.up << std::endl;
48         return;
49     }
50     std::cout << (f.up<0?"-" :"") <<std::abs(f.up)<< "/" << f.down << std::endl;
51 }
52 
53 //分数加法
54 Fraction add(const Fraction& f1, const Fraction& f2) {
55     Fraction result;
56     result.up = f1.up * f2.down + f2.up * f1.down;
57     result.down = f1.down * f2.down;
58     simplify(result);
59     return result;
60 }
61 
62 //分数减法
63 Fraction sub(const Fraction& f1, const Fraction& f2) {
64     Fraction result(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down);
65     simplify(result);
66     return result;
67 }
68 
69 //分数乘法
70 Fraction mul(const Fraction& f1, const Fraction& f2) {
71     Fraction result(f1.up * f2.up, f1.down * f2.down);
72     simplify(result);
73     return result;
74 }
75 
76 //分数除法
77 Fraction div(const Fraction& f1, const Fraction& f2) {
78     if (f2.up == 0) {
79         std::cout<<"分母不能为0"<<std::endl;
80         return Fraction(0,0);
81     }
82     Fraction result;
83     result.up = f1.up * f2.down; // 分子相乘(f1的分子乘以f2的分母)
84     result.down = f1.down * f2.up; // 分母相乘(f1的分母乘以f2的“分子”,但f2的分子在这里视为其倒数的分母部分)
85     simplify(result); // 化简
86     return result;
87 }
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

运行结果

 

 

实验任务五

源码

 1 #pragma once
 2 class SavingsAccount {
 3 private:
 4     int id;
 5     double balance;
 6     double rate;
 7     int lastDate;
 8     double accumulation;
 9     static double total;
10     void record(int date, double amount);
11     double accumulate(int date) const {
12         return accumulation + balance * (date - lastDate);
13     }
14 public:
15     SavingsAccount(int date, int id, double rate);
16     int getId() const { return id; }
17     double getBalance() const { return balance; }
18     double getRate() const { return rate; }
19     static double getTotal() { return total; }
20     void deposit(int date, double amount);
21     void withdraw(int date, double amount);
22     void settle(int date);
23     void show() const;
24 };
accout.h
 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 SavingsAccount::SavingsAccount(int date, int id, double rate) :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 
19 void SavingsAccount::deposit(int date, double amount) {
20     record(date, amount);
21 }
22 
23 
24 void SavingsAccount::withdraw(int date, double amount) {
25     if (amount > getBalance())
26         cout << "Error:not enough money" << endl;
27     else
28         record(date, -amount);
29 }
30 void SavingsAccount::settle(int date) {
31     double interest = accumulate(date) * rate / 365;
32     if (interest != 0)
33         record(date, interest);
34     accumulation = 0;
35 }
36 void SavingsAccount::show() const {
37     cout << "#" << id << "\tBalance:" << balance;
38 }
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 
 9     sa0.deposit(5, 5000);
10     sa1.deposit(25, 10000);
11     sa0.deposit(45, 5500);
12     sa1.withdraw(60, 4000);
13 
14     sa0.settle(90);
15     sa1.settle(90);
16 
17     sa0.show(); cout << endl;
18     sa1.show(); cout << endl;
19     cout << "Total:" << SavingsAccount::getTotal() << endl;
20     return 0;
21 }
5.11.cpp

运行结果

 

标签:std,const,对象,编程,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/yrx0415/p/18494453

相关文章

  • java的无锁编程和锁机制
    Java的并发编程中,为了保证线程安全和高性能,采用了两种主要的同步手段:锁机制和无锁编程。以下是对锁机制、无锁编程、死锁及其避免的详细讲解。一、无锁编程无锁编程通过原子操作来避免传统锁,从而减少线程的上下文切换,提升性能。在Java中,通常使用java.util.concurrent.......
  • 实验文档3
    关于第三次实践课作业实验结论task1.c1#define_CRT_SECURE_NO_WARNINGS2#include<stdio.h>3charscore_to_grade(intscore);4intmain(){5intscore;6chargrade;78while(scanf("%d",&score)!=EOF)9{10......
  • 实验5-3 使用函数求Fibonacci数
    本题要求实现求Fabonacci数列项的函数。所谓Fibonacci数列就是满足任一项数字是前两项的和(最开始两项均定义为1)的数列。函数接口定义:intfib(intn);函数fib应返回第n项Fibonacci数。题目保证输入输出在长整型范围内。裁判测试程序样例:#include<stdio.h>intfib(int......
  • 浙大版《C语言程序设计实验与习题指导(第4版)实验6-6 使用函数验证哥德巴赫猜想
    本题要求实现一个判断素数的简单函数,并利用该函数验证哥德巴赫猜想:任何一个不小于6的偶数均可表示为两个奇素数之和。素数就是只能被1和自身整除的正整数。注意:1不是素数,2是素数。函数接口给定如下intprime(intp);voidGoldbach(intn);其中函数prime当用户传入参数p......
  • 实验三
    实验任务1源代码1#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf......
  • 实验2 类和对象_基础编程1
    任务1:t.h#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数voidadjus......
  • Qt编程技巧小知识点(5)GPIB缓存区数据读取(升级版)
    文章目录Qt编程技巧小知识点(5)GPIB缓存区数据读取(升级版)小结Qt编程技巧小知识点(5)GPIB缓存区数据读取(升级版)  大端小端的问题,GPIB返回的数据经常是小端数据,而我们转化需要大端数据,看代码,Qt的这个函数很好用哦!代码输入//添加库文件#include<QtDebug>#include<Q......
  • CUDA编程学习 (1)——CUDA C介绍
    1.内存分配和数据移动API函数CUDA编程模型是一个异构模型,需要CPU和GPU协同工作。在CUDA中,host和device是两个重要的概念,我们用host指代CPU及其内存,而用device指代GPU及其内存。CUDA程序中既包含host程序,又包含device程序,它们分别在CPU和GPU上运行。同时,host与device之间......
  • CUDA编程学习 (2)——CUDA并行性模型
    1.基于kernel的SPMD并行编程1.1向量加法kernel(device代码)//DeviceCode//ComputevectorsumC=A+B//每个thread执行一次成对加法__global__voidvecAddKernel(float*A,float*B,float*C,intn){inti=threadIdx.x+blockDim.x*blockIdx.x......
  • 鸿蒙编程江湖:ArkTS 容器与原生容器在行为上的差异
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。ArkTS提供了一套容器集,包括Array、Map......