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

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

时间:2024-10-23 18:43:42浏览次数:5  
标签:const 对象 编程 down int Complex 实验 Fraction include

task 1:

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

运行结果:

问题一:

不能,可能是因为friend只是声明名为func的函数为友元,并没有声明func函数,去掉36行的func的声明后程序中就没有对func函数的声明了。

问题二:

1.复制构造函数
1.功能
1.用于创建一个新对象,该新对象是另一个同类型对象的副本。它会将源对象的每个数据成员的值复制到新创建的对象中。
2.调用时机
1.当用一个已存在的对象初始化一个新对象时调用;当对象作为函数参数按值传递时调用;当函数返回一个对象时调用(返回值优化可能会避免这种调用,但在某些情况下仍然会发生)。
2.移动构造函数
1.功能
1.移动构造函数用于将资源(如动态分配的内存)从一个对象转移到另一个对象,而不是进行昂贵的复制操作。它通常会“窃取”源对象的资源,然后将源对象置于一种可析构的安全状态(例如将指针成员设为nullptr)。
2.调用时机
1.当用一个临时对象(右值)初始化一个新对象时调用;在使用std::move将左值转换为右值后,用这个转换后的右值初始化新对象时调用。
3.参数带默认值的普通构造函数
1.功能
1.用于创建对象并初始化对象的成员变量。如果构造函数的参数有默认值,那么在创建对象时,可以根据需要选择提供部分或全部参数,未提供的参数将使用默认值进行初始化。
2.调用时机
1.当创建对象时调用。
4.析构函数
调用时机
1.当对象超出其作用域时调用;当使用delete操作符显式删除动态分配的对象时调用。

问题三:

 

不能运行

task 2:

Complex.h

#include<iostream>
using namespace std;

class Complex
{
public:
    static string doc;
    Complex(double x=0,double y=0):r{x},i{y}{}
    Complex(const Complex& p) { r = p.r; i = p.i; }
    double get_real() { return r; }
    double get_imag() { return i; }
    void add(const Complex& p) { r += p.r; i += p.i; }
    friend Complex add(const Complex& p, const Complex& q);
    friend bool is_equal(const Complex& p, const Complex& q);
    friend bool is_not_equal(const Complex& p, const Complex& q);
    friend void output(const Complex& p);
    friend double abs(const Complex& p);
private:
    double r, i;
};

Complex.cpp:

#include<iostream>
#include<cmath>
#include"1.hpp"
string Complex::doc = " a simplified complex class";
Complex add(const Complex& p, const Complex& q)
{
    Complex c;
    c.i = p.i + q.i;
    c.r = p.r + q.r;
    return c;
}
bool is_equal(const Complex& p, const Complex& q)
{
    return (p.i == q.i && p.r == q.r);
}
bool is_not_equal(const Complex& p, const Complex& q)
{
    return (p.i != q.i || p.r != q.r);
}
void output(const Complex& p)
{
    if(p.i<0)cout << p.r << p.i << "i" << endl;
    else cout << p.r << "+" << p.i << "i" << endl;
}
double abs(const Complex& p)
{
    double t;
    t = p.i * p.i + p.r * p.r;
    return std::sqrt(t);
}

task2.cpp:

 

#include <iostream>
#include"1.hpp"
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();
}

运行结果:

task3:

代码如下:

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

运行结果:

 

task4:

Fraction.h:

#pragma once
#include<iostream>
using namespace std;
class Fraction
{
public:
    static string Doc;
    Fraction(int u, int d = 1) :up{ u }, down{ d } {}
    Fraction(const Fraction& f) { up = f.up; down = f.down; }
    int get_up() { return up; }
    int get_down() { return down; }
    Fraction negative() {
        Fraction f(1);
        if (down < 0) { f.up = up; f.down = 0 - down; return f; }
        else { f.up = 0 - up; f.down = down; return f; }
    }
    friend void output(const Fraction& f);
    friend Fraction add(const Fraction& a, const Fraction& b);
    friend Fraction sub(const Fraction& a, const Fraction& b);
    friend Fraction mul(const Fraction& a, const Fraction& b);
    friend Fraction div(const Fraction& a, const Fraction& b);
private:
    int up, down;

};
void output(const Fraction& f);
Fraction add(const Fraction& a, const Fraction& b);
Fraction sub(const Fraction& a, const Fraction& b);
Fraction mul(const Fraction& a, const Fraction& b);
Fraction div(const Fraction& a, const Fraction& b);

Fraction.cpp:

#include<iostream>
#include"Fraction.h"
using namespace std;
string Fraction::Doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加 / 减 / 乘 / 除运算.";
void output(const Fraction& f)
{
    int x, y,c,d, m = 0;
    c = f.up;
    d = f.down;
    if (c <= 0)x = -1 * c;
    else x = c;
    if (d <= 0)y = -1 * d;
    else y = d;
    for (int i = x; (i * i) >= x && x != 0&&y != 0; i--)
    {
        if (x % i == 0)
        {
            if (y % i == 0 && i > m)m = i;
            else if ((x / i) > m && y % (x / i) == 0)m = x / i;
        }
    }
    if(m)c = c / m, d = d / m;
    if (d)
    {
        if (c)cout << c << "/" << d << endl;
        else cout <<"0" << endl;
    }
    else cout << "分母不能为0" << endl;
}
Fraction add(const Fraction& a, const Fraction& b)
{
    int x, y, c, d, m = 0;
    Fraction f(1);
    c = a.up * b.down+ b.up * a.down;
    d = a.down * b.down;
    if (c <= 0)x = -1 * c;
    else x = c;
    if (d <= 0)y = -1 * d;
    else y = d;
    for (int i = x; (i * i) >= x && x != 0 && y != 0; i--)
    {
        if (x % i == 0)
        {
            if (y % i == 0 && i > m)m = i;
            else if ((x / i) > m && y % (x / i) == 0)m = x / i;
        }
    }
    if (m)c = c / m, d = d / m;
    f.up = c;
    f.down = d;
    return f;
}
Fraction sub(const Fraction& a, const Fraction& b)
{
    int x, y, c, d, m = 0;
    Fraction f(1);
    c = a.up * b.down - b.up * a.down;
    d = a.down * b.down;
    if (c <= 0)x = -1 * c;
    else x = c;
    if (d <= 0)y = -1 * d;
    else y = d;
    for (int i = x; (i * i) >= x && x != 0 && y != 0; i--)
    {
        if (x % i == 0)
        {
            if (y % i == 0 && i > m)m = i;
            else if ((x / i) > m && y % (x / i) == 0)m = x / i;
        }
    }
    if (m)c = c / m, d = d / m;
    f.up = c;
    f.down = d;
    return f;
}
Fraction mul(const Fraction& a, const Fraction& b)
{
    int x, y, c, d, m = 0;
    Fraction f(1);
    c = a.up * b.up;
    d = a.down * b.down;
    if (c <= 0)x = -1 * c;
    else x = c;
    if (d <= 0)y = -1 * d;
    else y = d;
    for (int i = x; (i * i) >= x && x != 0 && y != 0; i--)
    {
        if (x % i == 0)
        {
            if (y % i == 0 && i > m)m = i;
            else if ((x / i) > m && y % (x / i) == 0)m = x / i;
        }
    }
    if (m)c = c / m, d = d / m;
    f.up = c;
    f.down = d;
    return f;
}
Fraction div(const Fraction& a, const Fraction& b)
{
    int x, y, c, d, m = 0;
    Fraction f(1);
    c = a.up * b.down;
    d = a.down * b.up;
    if (c <= 0)x = -1 * c;
    else x = c;
    if (d <= 0)y = -1 * d;
    else y = d;
    for (int i = x; (i * i) >= x && x != 0 && y != 0; i--)
    {
        if (x % i == 0)
        {
            if (y % i == 0 && i > m)m = i;
            else if ((x / i) > m && y % (x / i) == 0)m = x / i;
        }
    }
    if (m)c = c / m, d = d / m;
    f.up = c;
    f.down = d;
    return f;
}

task4.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();
}

运行结果:

 

 

 

task5:

 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//_ACCOUNT_H

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;
        }

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;
}

运行结果:

 

标签:const,对象,编程,down,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/the-existent/p/18497900

相关文章

  • C语言趣味编程100例源码分享
    C语言趣味编程100例是学习路上必不可缺的示例,话不多说直接看代码1,百钱百鸡问题include<stdio.h>main(){intcock,hen,chicken;for(cock=0;cock<=20;cock++) /外层循环控制公鸡数量取值范围0~20/for(hen=0;hen<=33;hen++) /内层循环控制母鸡数量取值范围0~30/for(chic......
  • 实验三
    任务一验证性实验源码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:......
  • 实验3
    TASK11#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf(......
  • 实验2
    实验任务1:源码及截图:1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数1//类T:实现2//普通函数实现34......
  • 《Python游戏编程入门》注-第3章2
    《Python游戏编程入门》的“3.2.2获取用户输入”部分介绍了input()函数的用法;“3.2.3异常处理”部分介绍了try...except语句的用法。1input()函数的用法input()函数用于接受用户的输入,该函数的参数可以在等待用户输入之前显示文本。该函数主要有两种用法:第一个是将当前程......
  • 《Python游戏编程入门》注-第3章1
    《Python游戏编程入门》的第三章是“I/O、数据和字体:Trivia游戏”,介绍了print()函数、input()函数、异常处理以及文件的输入输出,最后根据以上内容完成了Trivia游戏。本章的“3.1了解Trivia游戏”介绍了Trivia游戏的界面和玩法。“3.2Python数据类型”中讲解了print()函数、i......
  • 20222316 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    一、实验内容1.学习总结——后门与免杀1)后门基本概念后门就是不经过正常认证流程而访问系统的通道。狭义后门:特指潜伏于操作系统中专门做后门的一个程序,“坏人”可以连接这个程序,远程执行各种指令。后面类型有编译器后门、操作系统后门、应用程序后门、潜伏于操作系统中或......
  • 20222324 石国力 《网络与系统攻防技术》 实验二
    1.实验内容(1)使用netcat获取主机操作Shell,cron启动某项任务(2)使用socat获取主机操作Shell,任务计划启动(3)使用MSFmeterpreter(或其他软件)生成可执行文件(后门),利用ncat或socat传送到主机并运行获取主机Shell(4)使用MSFmeterpreter(或其他软件)生成获取目标主机音频、摄像头、......
  • 第四章 面向对象(2)
    一.this关键字this关键字代表当前对象(哪一个对象在访问,this就表示哪个)使用this关键字引用成员变量使用this关键字引用成员方法或构造方法。在一个类的方法或构造方法内部,可以使用“this.成员变量名”这样的格式来引用成员变量名,常常用来区分同名的成员变量和局部变量。......
  • 实验2
    x.h#include<iostream>#include<math.h>#include<string>usingnamespacestd;usingstd::string;classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();sta......