首页 > 其他分享 >实验2 类和对象

实验2 类和对象

时间:2024-10-29 23:32:26浏览次数:1  
标签:std const 对象 int Complex 实验 Fraction include

实验任务1:

t.h代码:

 1 #pragma once
 2 #include <string>
 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 void adjust(int ratio); // 按系数成倍调整数据
12 void display() const; // 以(m1, m2)形式显示T类对象信息
13 private:
14 int m1, m2;
15 // 类属性、方法
16 public:
17 static int get_cnt(); // 显示当前T类对象总数
18 public:
19 static const std::string doc; // 类T的描述信息
20 static const int max_cnt; // 类T对象上限
21 private:
22 static int cnt; // 当前T类对象数目
23 // 类T友元函数声明
24 friend void func();
25 };
26 // 普通函数声明
27 void func();
28 
29 
30 // static成员数据类外初始化
31 const std::string T::doc{"a simple class sample"};
32 const int T::max_cnt = 999;
33 int T::cnt = 0;
View Code

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 }
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 
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 }
View Code

运行结果截图:

 问题1:

可以运行,因为在t.cpp中有func函数的定义,主程序运行时可以查找到func函数的定义,那么即使不在类T的定义中再次声明其为友元,编译器也能正确识别和处理func函数对类私有成员的访问。正常情况下,如果func函数在类外部没有任何声明或定义,且尝试在源文件中调用它,那么编译器将会报错,因为它不知道func函数的存在。

 

 

问题2:

普通构造函数 (T(int x = 0, int y = 0)):用于创建类T的对象,并初始化成员变量m1和m2为指定的值。在创建对象时自动调用。

复制构造函数 (T(const T& t)):用于创建一个新的对象,并将其初始化为另一个同类型对象的副本。当一个对象以值传递的方式传递给函数、或从函数返回对象、或进行对象赋值操作时。

移动构造函数 (T(T&& t)):用于获得另一个右值对象的资源,而不是复制它们。当右值对象被用作初始值来构造新对象时。

析构函数 (~T()):在对象生命周期结束时自动调用,用于执行清理操作,如释放对象所占用的资源。对象离开其作用域时。

 

问题3:

我使用的时DevC++中可以运行,调整的代码与t.cpp中其他代码未产生关联,故无影响。

 

 

实验任务2:

Complex.h代码:

 1 #pragma once
 2 #include<string>
 3 
 4 class Complex{
 5     public:
 6         Complex(double x=0,double y=0);
 7         Complex(const Complex &c);
 8     
 9         ~Complex();
10         void display() cosnt;
11         
12         double const get_real(){
13             return r;
14         };
15         double const get_imag(){
16             return i;
17         };
18         
19         private:
20             double r,i;
21 
22         friend Complex add(const Complex& c1,const Complex& c2);
23         friend bool is_equal(const Complex& c1,const Complex& c2);
24         friend bool is_not_equal(const Complex& c1,const Complex& c3);
25         friend double abs(const Complex& c);
26         friend void output(const Complex& c);
27         
28     public:
29         static const std::string doc;
30 };
31 
32 Complex add();
33 bool is_equal();
34 bool is_not_equal();
35 void output();
36 void abs();
View Code

Complex.cpp代码:

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

task2.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 }
View Code

运行结果截图:

 

实验任务3:

task3.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 << "标准库模板类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 }
View Code

运行结果截图:

 问题1:

接口有:

complex 模板类的默认构造函数、带实部和虚部的构造函数、带单个实部的构造函数、拷贝构造函数、实部和虚部的访问函数、模的计算 abs()

 

问题2:

类的定义与声明已经由C++标准库定义和实现,提供了丰富的运算接口,包括加减乘除、取模、相等比较等,且都是经过优化的实现,可以自动管理内存和资源。

成员函数的调用与运算可以直接使用运算符重载进行运算,如 c1 += c2,输出运算符 << 进行输出,如 cout << c1。

 

问题3:

适当的使用标准库complex模板类,使代码更简洁,因为只需要包含头文件和测试代码。可以直接使用标准库提供的接口,无需手动编写和调试运算函数和输出函数。

 

实验任务4:

 Fraction.h代码:

 1 #pragma once
 2 #include<string>
 3 
 4 using std::string;
 5 class  Fraction {
 6 
 7     public:
 8         Fraction(int x,int y=1);
 9         Fraction(const Fraction &f);
10         ~Fraction();
11 
12     public:
13         int get_up() const;
14         int get_down() const;
15         Fraction negative();
16 
17         Fraction simplify();
18 
19     private:
20         int up,down;
21         friend void  output(const Fraction &f);
22         friend Fraction add(const Fraction &f1,const Fraction &f2);
23         friend Fraction sub(const Fraction &f1,const Fraction &f2);
24         friend Fraction mul(const Fraction &f1,const Fraction &f2);
25         friend Fraction div(const Fraction &f1,const Fraction &f2);
26 
27     public:
28         static const string doc;
29 };
30 
31 void output(const Fraction& f1);
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 <iostream>
  2 #include <string>
  3 #include "Fraction.h"
  4 
  5 using std::cout;
  6 using std::endl;
  7 using std::string;
  8 
  9 const string Fraction::doc{
 10     "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算."
 11 };
 12 
 13 Fraction::Fraction(int x,int y):up{x},down{y}{}
 14 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{}
 15 Fraction::~Fraction(){}
 16 
 17 
 18 int Fraction::get_up() const{
 19     return up;
 20 }
 21 
 22 int Fraction::get_down() const{
 23     return down;
 24 }
 25 
 26 Fraction Fraction::negative() {
 27     Fraction t = simplify();
 28     return Fraction(-t.up,t.down);
 29 }
 30 
 31 Fraction Fraction::simplify()
 32 {
 33     int small, x, y, z;
 34     if (up >= 0)
 35     {
 36         x = up;
 37         if (down > 0)
 38         {
 39             y = down;
 40             z = 1;
 41         }
 42         else
 43         {
 44             y = -down;
 45             z = -1;
 46         }
 47     }
 48     else
 49     {
 50         x = -up;
 51         if (down > 0)
 52         {
 53             y = down;
 54             z = -1;
 55         }
 56         else
 57         {
 58            y = -down;
 59             z = 1;
 60         }
 61     }
 62     if (x > y)
 63         small = y;
 64     else
 65         small = x;
 66     int i;
 67     for (i = small; i >= 1; i--)
 68         if (x % i == 0 && y % i == 0)
 69         {
 70             x = x / i;
 71             y = y / i;
 72             break;
 73         }
 74     if (z == 1)
 75         return Fraction(x, y);
 76     return Fraction(-x, y);
 77 }
 78 
 79 void output(const Fraction& f){
 80     Fraction t = Fraction(f.up,f.down);
 81     t = t.simplify();
 82     if(t.down == 0) cout << "分母不能为0";
 83     else {
 84         if(t.up == 0)
 85         cout << "0";
 86     else if(t.up != 0 && t.down == 1)
 87         cout << t.up;
 88     else
 89          cout << t.up << "/" << t.down;
 90     cout << endl;
 91     }
 92 }
 93 Fraction add(const Fraction &f1,const Fraction &f2) {
 94     int num = f1.up * f2.down + f2.up * f1.down;
 95     int denom = f1.down * f2.down;
 96     return Fraction(num, denom);
 97 }
 98 Fraction sub(const Fraction &f1,const Fraction &f2) {
 99     int num = f1.up * f2.down - f2.up * f1.down;
100     int denom = f1.down * f2.down;
101     return Fraction(num, denom);
102 }
103 Fraction mul(const Fraction &f1,const Fraction &f2) {
104     return Fraction(f1.up*f2.up,f1.down*f2.down);
105 
106 }
107 Fraction div(const Fraction &f1,const Fraction &f2) {
108     return Fraction(f1.up*f2.down,f1.down*f2.up);
109 
110 }
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 = ";
12     output(f1);
13     cout << endl;
14     cout << "f2 = ";
15     output(f2);
16     cout << endl;
17     cout << "f3 = ";
18     output(f3);
19     cout << endl;
20     cout << "f4 = ";
21     output(f4);
22     cout << endl;
23     Fraction f5(f4.negative());
24     cout << "f5 = ";
25     output(f5);
26     cout << endl;
27     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
28          f5.get_down() << endl;
29     cout << "f1 + f2 = ";
30     output(add(f1, f2));
31     cout << endl;
32     cout << "f1 - f2 = ";
33     output(sub(f1, f2));
34     cout << endl;
35     cout << "f1 * f2 = ";
36     output(mul(f1, f2));
37     cout << endl;
38     cout << "f1 / f2 = ";
39     output(div(f1, f2));
40     cout << endl;
41     cout << "f4 + f5 = ";
42     output(add(f4, f5));
43     cout << endl;
44 }
45 void test2() {
46     Fraction f6(42, 55), f7(0, 3);
47     cout << "f6 = ";
48     output(f6);
49     cout << endl;
50     cout << "f7 = ";
51     output(f7);
52     cout << endl;
53     cout << "f6 / f7 = ";
54     output(div(f6, f7));
55     cout << endl;
56 }
57 int main() {
58     cout << "测试1: Fraction类基础功能测试\n";
59     test1();
60     cout << "\n测试2: 分母为0测试: \n";
61     test2();
62 }
View Code

运行结果截图:

 

实验任务5:

account.h代码:

 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 {
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 };
View Code

account.cpp代码:

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

运行结果截图:

 

理解和体会:

类通过私有成员变量和公有成员函数实现了封装。外界只能通过公有成员函数来访问和操作账户信息,保证了数据的安全性和完整性。程序中使用了静态成员变量来记录总账户数,这是数据共享。同时,通过类的封装和访问权限控制,保护了账户信息不被非法访问和修改。对银行账户进行了抽象,使得代码更加简洁、易于理解和维护。

优化:

可以使用接口或抽象类来定义更多基本的功能,并通过多态性来实现不同的银行账户类型。同时,加强代码的安全性,使用一些加密函数等等。

 

标签:std,const,对象,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/yr123/p/18514707

相关文章

  • 实验2 类和对象_基础编程1
    一、实验目的加深对OOP中类、对象的理解,能够解释类的封装、类的接口会使用C++语法规则正确定义、实现、测试类;会使用类创建对象,并基于对象编程针对具体问题场景,练习运用面向对象思维进行设计,合理利用C++语言特性(访问权限控制,static,friend,const),在数据共享和保护......
  • 实验3
    1:代码:#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,grade);}return0;}charscore_to_gra......
  • cpp实验二
     任务一://t.h#include"t.h"#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;conststd::stringT::doc{"asimpleclasssample"};constintT::max_cnt=999;intT::cnt=0;T::T(intx,int......
  • JS-数组、函数、类与对象
    JS进阶数组数组可以存放任意类型的元素letarr=['小胖',12,true,28.9];console.log(arr,arr.length);增arr[4]='newValue';改arr[4]='changedValue';删不会改变数组的长度,使用undefined赋值deletearr[4];查console.log(arr[4]);//undefined多......
  • 20222428 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容1.1本周学习内容1.1.1后门实践中的基本概念netcat(瑞士军刀):用于监听、探测端口、正向连接、反向连接、文件传输。socat:netcat的加强版(多了SSL连接等功能)。Meterpreter工具:Metadploit框架中的扩展模块,是生成后门的工具。VeilEvasion:生成免杀的工具。1.1.2......
  • 实验二
    实验内容一:t.h点击查看代码#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构......
  • 实验2 类和对象_基础编程1
    任务1:t.h#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数voidadjus......
  • 20222413 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容1.1学习总结本周的学习内容为恶意代码的概念、发展历史以及分析技术。我知晓了恶意代码的不同类型及其典型案例、攻击方式和危害。同时我了解了静态分析和动态分析所采用的技术方法。1.2实践内容(1)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或......
  • 实验3 C语言函数应用编程
    #include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,gr......
  • 实验2 类和对象_基础编程1
    任务1源代码t.h#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数void......