首页 > 编程语言 >实验2 类与对象 基础编程

实验2 类与对象 基础编程

时间:2024-10-24 14:44:17浏览次数:1  
标签:std const 对象 double 编程 int Complex 实验 Fraction

实验一:

t.h:

#pragma once

#include <string>

// 类T: 声明
class T {
// 对象属性、方法
public:
    T(int x = 0, int y = 0);   // 普通构造函数
    T(const T &t);  // 复制构造函数
    T(T &&t);       // 移动构造函数
    ~T();           // 析构函数

    void adjust(int ratio);      // 按系数成倍调整数据
    void display() const;           // 以(m1, m2)形式显示T类对象信息

private:
    int m1, m2;

// 类属性、方法
public:
    static int get_cnt();          // 显示当前T类对象总数

public:
    static const std::string doc;       // 类T的描述信息
    static const int max_cnt;           // 类T对象上限

private:
    static int cnt;         // 当前T类对象数目

// 类T友元函数声明
    friend void func();
};

// 普通函数声明
void func();

t.cpp:

// 类T: 实现
// 普通函数实现

#include "t.h"
#include <iostream>
#include <string>

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

// static成员数据类外初始化
const std::string T::doc{"a simple class sample"};
const int T::max_cnt = 999;
int T::cnt = 0;


// 对象方法
T::T(int x, int y): m1{x}, m2{y} { 
    ++cnt; 
    cout << "T constructor called.\n";
} 

T::T(const T &t): m1{t.m1}, m2{t.m2} {
    ++cnt;
    cout << "T copy constructor called.\n";
}

T::T(T &&t): m1{t.m1}, m2{t.m2} {
    ++cnt;
    cout << "T move constructor called.\n";
}    

T::~T() {
    --cnt;
    cout << "T destructor called.\n";
}           

void T::adjust(int ratio) {
    m1 *= ratio;
    m2 *= ratio;
}    

void T::display() const {
    cout << "(" << m1 << ", " << m2 << ")" ;
}     

// 类方法
int T::get_cnt() {
   return cnt;
}

// 友元
void func() {
    T t5(42);
    t5.m2 = 2049;
    cout << "t5 = "; t5.display(); cout << endl;
}

task1.cpp:

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

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

void test();

int main() {
    test();
    cout << "\nmain: \n";
    cout << "T objects'current count: " << T::get_cnt() << endl;
}

void test() {
    cout << "test class T: \n";
    cout << "T info: " << T::doc << endl;
    cout << "T objects'max count: " << T::max_cnt << endl;
    cout << "T objects'current count: " << T::get_cnt() << endl << endl;


    T t1;
    cout << "t1 = "; t1.display(); cout << endl;

    T t2(3, 4);
    cout << "t2 = "; t2.display(); cout << endl;

    T t3(t2);
    t3.adjust(2);
    cout << "t3 = "; t3.display(); cout << endl;

    T t4(std::move(t2));
    cout << "t3 = "; t4.display(); cout << endl;

    cout << "T objects'current count: " << T::get_cnt() << endl;

    func();
}

实验结果:

 问题一:

task.cpp调用了func函数,却没有在t.h中声明这个func函数

问题二:
普通构造函数是用给定的参数初始化,在创建对象时被调用;复制构造函数是用一个已存在的对象来初始化一个新对象;移动构造函数:用一个临时对象初始化新创立的对象;析构函数:在对象生命周期结束时,或者动态分配对象被delete调用时。

问题三:

不能正常运行。

实验二:

Complex.h

#pragma once
#include <string>

class Complex {
public:
    static const std::string doc;
    Complex(double r = 0, double i = 0);
    Complex(const Complex& c);
    double get_real() const;
    double get_imag() const;
    Complex add(const Complex& c) const;
    friend Complex add(const Complex& c1, const Complex& c2);
    friend bool is_equal(const Complex& c1, const Complex& c2);
    friend bool is_not_equal(const Complex& c1, const Complex& c2);
    friend double abs(const Complex& c);
    friend void output(const Complex& c);
private:
    double real;
    double imag;
};

Complex.cpp

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

const std::string Complex::doc{"a simplified complex class"};

Complex::Complex(double r, double i) : real{r}, imag{i} {}

Complex::Complex(const Complex& c) : real{c.real}, imag{c.imag} {}

double Complex::get_real() const {
    return real;
}

double Complex::get_imag() const {
    return imag;
}

Complex Complex::add(const Complex& c) const {
    return Complex(real + c.real, imag + c.imag);
}

Complex add(const Complex& c1, const Complex& c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

bool is_equal(const Complex& c1, const Complex& c2) {
    return c1.real == c2.real && c1.imag == c2.imag;
}

bool is_not_equal(const Complex& c1, const Complex& c2) {
    return!(c1.real == c2.real && c1.imag == c2.imag);
}

double abs(const Complex& c) {
    return std::sqrt(c.real * c.real + c.imag * c.imag);
}

void output(const Complex& c) {
    std::cout << c.real;
    if (c.imag >= 0) {
        std::cout << "+";
    }
    std::cout << c.imag << "i";
}

main.cpp

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

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

void test() {
    cout << "类成员测试: " << endl;
    cout << Complex::doc << endl;

    cout << endl;

    cout << "Complex对象测试: " << endl;
    Complex c1;
    Complex c2(3, -4);
    const Complex c3(3.5);
    Complex c4(c3);

    cout << "c1 = "; output(c1); cout << endl;
    cout << "c2 = "; output(c2); cout << endl;
    cout << "c3 = "; output(c3); cout << endl;
    cout << "c4 = "; output(c4); cout << endl;
    cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;

    cout << endl;

    cout << "复数运算测试: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1.add(c2);
    cout << "c1 += c2, c1 = "; output(c1); cout << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
    c4 = add(c2, c3);
    cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
}

int main() {
    test();
}

实验结果

实验三

#include <iostream>
#include <complex>

using std::cout;
using std::endl;
using std::boolalpha;
using std::complex;

void test() {
    cout << "标准库模板类comple测试: " << endl;
    complex<double> c1;
    complex<double> c2(3, -4);
    const complex<double> c3(3.5);
    complex<double> c4(c3);

    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c4 = " << c4 << endl;
    cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
    cout << endl;

    cout << "复数运算测试: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1 += c2;
    cout << "c1 += c2, c1 = " << c1 << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << (c1 == c2) << endl;
    cout << "c1 != c3 : " << (c1 != c3) << endl;
    c4 = c2 + c3;
    cout << "c4 = c2 + c3, c4 = " << c4 << endl;
}

int main() {
    test();
}

实验结果

启示

构造函数和操作符重载的使用使得代码直观而在使用标准库complex模板类时,可以直接使用+操作符标准库模板类的设计考虑了复数的常见需求,提供了简洁、高效的接口。在设计自定义类时,可以参考标准库的设计模式,更好地满足用户需求并提高代码的可读性和可维护性。

实验四

Fraction.cpp

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

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

const string Fraction::doc {"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."};

Fraction::Fraction(int u, int d): up{u}, down{d} {
}

Fraction::Fraction(const Fraction& other): up{other.up}, down{other.down} {}

Fraction::~Fraction() {}

int Fraction::get_up() const{
    int a = up, b = down;
    int u = up, d = down;
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    int gcd = a;
    u /= gcd;
    d /= gcd;
    if (d < 0) {
        d = -d;
        u = -u;
    }
    return u;
}

int Fraction::get_down() const{
    int a = up, b = down;
    int u = up, d = down;
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    int gcd = a;
    u /= gcd;
    d /= gcd;
    if (d < 0) {
        d = -d;
        u = -u;
    }
    return d;
}

Fraction Fraction::negative() const{
    return Fraction(-up, down);
}

void output(const Fraction &f) {
    if(f.down == 0) {
        cout << "分母不能为0";
        return;
    }

    int a = f.up, b = f.down;
    int u = f.up, d = f.down;
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    int gcd = a;
    u /= gcd;
    d /= gcd;
    if (d < 0) {
        d = -d;
        u = -u;
    }
    if(u == 0)
        cout << 0;
    else if(d == 1)
        cout << u;
    else
        cout << u << "/" << d ;
}

Fraction add(const Fraction& f1, const Fraction& f2) {
    return Fraction(f1.up * f2.down + f2.up * f1.down, f1.down * f2.down);
}

Fraction sub(const Fraction& f1, const Fraction& f2) {
    return Fraction(f1.up * f2.down - f2.up * f1.down, f1.down * f2.down);
}

Fraction mul(const Fraction& f1, const Fraction& f2) {
    return Fraction(f1.up * f2.up, f1.down * f2.down);
}

Fraction div(const Fraction& f1, const Fraction& f2) {
    return Fraction(f1.up * f2.down, f1.down * f2.up);
}

Fraction.h

#pragma once
#include <string>

using std::string;

class Fraction {
private:
    int up, down;

public:
    static const string doc;
    Fraction(int u, int d = 1);
    Fraction(const Fraction &other);
    ~Fraction();

    int get_up() const;
    int get_down() const;
    Fraction negative() const;

    friend void output(const Fraction &f);
    friend Fraction add(const Fraction &f1, const Fraction &f2);
    friend Fraction sub(const Fraction &f1, const Fraction &f2);
    friend Fraction mul(const Fraction &f1, const Fraction &f2);
    friend Fraction div(const Fraction &f1, const Fraction &f2);


  

};

test.cpp

#include "C:\Users\DELL\Desktop\新建文件夹\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();
}

实验结果

实验五

main.cpp

#include "C:\Users\DELL\Desktop\新建文件夹\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;
}

account.cpp

#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

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 << "Error: 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;
}

account.h

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount { 
private:
    int id;                
    double balance;        
    double rate;        
    int lastDate;        
    double accumulation;    
    static double total;    
  
    void record(int date, double amount);
  
    double accumulate(int date) const {
        return accumulation + balance * (date - lastDate);
    }
public:
   
    SavingsAccount(int date, int id, double rate);
    int getId() const { return id; }
    double getBalance() const { return balance; }
    double getRate() const { return rate; }
    static double getTotal() { return total; }
    void deposit(int date, double amount);         
    void withdraw(int date, double amount);     
    
    void settle(int date);
   
    void show() const;
};
#endif 

实验结果

 

数据的安全性和一些关键步骤,比如存取款剩余不足等

改进接口,减少用户的操作复杂度,加强对输入数据的合法性与安全性的验证

 

标签:std,const,对象,double,编程,int,Complex,实验,Fraction
From: https://www.cnblogs.com/hinaou/p/18494308

相关文章

  • python编程语言实现身份证实名认证?身份证查询接口
    互联网的便利性犹如一把双刃剑,在给人们带来便利的同时,也滋生了网络诈骗、网络水军等影响网络健康、安全的隐患。为了更好地监管网络安全,建设绿色、健康的网络环境,互联网平台软件均开始实行实名认证,下面以翔云身份证实名认证接口为例。翔云身份证实名认证接口,实时联网,通过......
  • 实验2 类和对象_基础编程1
    1.实验任务1t.h1#pragmaonce23#include<string>45//类T:声明6classT{7public:8T(intx=0,inty=0);//普通构造函数9T(constT&t);//复制构造函数10T(T&&t);//移动构造函数11~T();//析构函......
  • python编程语言实现身份证实名认证?身份证查询接口
    互联网的便利性犹如一把双刃剑,在给人们带来便利的同时,也滋生了网络诈骗、网络水军等影响网络健康、安全的隐患。为了更好地监管网络安全,建设绿色、健康的网络环境,互联网平台软件均开始实行实名认证,下面以翔云身份证实名认证接口为例。翔云身份证实名认证接口,实时联网,......
  • 实验3
    task1:源代码:1#include<stdio.h>2charscore_to_grade(intscore);//函数声明3intmain(){4intscore;5chargrade;6while(scanf_S("%d",&score)!=EOF){7grade=score_to_grade(score);//函数调用8......
  • 套接字socket编程预备知识
    套接字基础套接字定义套接字是一种用于实现网络通信的重要技术,在现代计算机网络中扮演着关键角色。它本质上是一个标准化的网络编程接口,为应用程序提供了访问底层网络协议的能力2。通过使用套接字,开发者能够创建能够在不同机器之间进行通信的应用程序,实现了跨设备、跨平台......
  • 20222327 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    一、实验内容1.正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧2.通过组合应用各种技术实现恶意代码免杀3.用另一电脑实测,在杀软开启的情况下,可运行并回连成功,注明电脑的杀软名称与版本4.问题回答(1)杀软是如何检测出恶意代码的?基于特征码检测:杀毒软件中......
  • [C++]在windows基于C++编程署yolov11-pose的openvino姿态估计模型cmake项目部署演示源
    【算法介绍】在Windows系统上,基于C++编程部署YOLOv11-Pose的OpenVINO姿态估计模型,可以通过CMake项目来实现。以下是简要介绍:首先,需要准备开发环境,包括安装OpenVINOToolkit、CMake、OpenCV和C++编译器(如GCC或MSVC)。OpenVINO是英特尔开发的一款用于优化和部署深度学习模型的工具套件......
  • [C++]在windows基于C++编程署yolov11-cls的openvino图像分类模型cmake项目部署演示源
    【算法介绍】在Windows系统上,基于C++编程部署YOLOv11-CLS的OpenVINO图像分类模型,可以通过CMake项目来实现。以下是简要介绍:首先,需要准备开发环境,包括安装OpenVINOToolkit、CMake、OpenCV和C++编译器(如GCC或MSVC)。OpenVINO是英特尔开发的一款用于优化和部署深度学习模型的工具套件,......
  • 20222317 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    一、实验内容本次实验目的为通过多次加密、文件格式欺骗、填充、加壳等技术手段实现恶意代码免杀,产生恶意程序,并尝试通过杀毒软件,不被杀毒软件检测出来。具体实验内容如下:1.正确使用msf编码器,使用msfvenom生成如jar之类的其他文件;2.能够使用veil,加壳工具;3.能够使用C+shellcode......
  • 第11章-Python网络编程
    网络编程是Python比较擅长的领域,Python不但内置了网络编程相关的库,而且与网络编程相关的第三方库也非常丰富,所以使用Python进行网络编程非常方便,Web应用程序、网络爬虫、网络游戏等常见的网络应用都可以使用Python进行开发。本章将介绍Python网络编程基础、内置的urllib库和......