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

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

时间:2024-10-29 16:02:09浏览次数:5  
标签:std 对象 double 编程 int Complex 实验 Fraction include

实验任务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();
}

运行测试截图:

问题1:不能;编译报错信息截图:

问题2:普通构造函数:默认构造函数
复制构造函数:用别的对象来初始化新对象的内存
移动构造函数:更节约时间的类似复制构造函数的函数
析构函数:当对象生命周期结束时,释放对象内存空间

问题3:不能

实验任务2:
源代码Complex.h:

点击查看代码
#pragma once
   #include<iostream>
   using namespace std;
   
   class Complex
   {
   public:
       Complex(double x = 0, double y = 0) :real(x), imag(y) {}
       Complex(const Complex& L);
       double get_real();
       double get_imag();
       void add(Complex L2);
       
       friend Complex add(Complex L1, Complex L2);
       friend bool is_equal(Complex L1, Complex L2);
       friend bool is_not_equal(Complex L1, Complex L2);
       friend void output(Complex L);
       friend double abs(Complex L);

       static string doc; 
   private:
   double real;
   double imag;
};

源代码Complex.cpp:

点击查看代码
#include<iostream>
   #include"Complex.h"
   #include<cmath>
   using namespace std;
   
   string Complex::doc = "a simplified complex class";
   Complex::Complex(const Complex& L)
   {
       real = L.real;
       imag = L.imag;
   }
   
   double Complex::get_real()
   {
       return real;
   }
   double Complex::get_imag()
   {
       return imag;
   }
   
   void Complex::add(Complex L2)
   {
       real += L2.real;
       imag += L2.imag;
   }
   Complex add(Complex L1, Complex L2)
   {
       Complex L3;
       L3.real = L1.real + L2.real;
       L3.imag = L1.imag + L2.imag;
       return L3;
   }
   bool is_equal(Complex L1, Complex L2)
   {
       if (L1.real == L2.real && L1.imag == L2.imag)
       {
           return true;
       }
       else
       {
           return false;
       }
   }
   
    bool is_not_equal(Complex L1, Complex L2)
   {
       if (L1.real == L2.real && L1.imag == L2.imag)
       {
           return false;
       }
       else
       {
           return true;
       }
   
   }
   void output(Complex L)
   {
       if (L.imag >= 0)
           cout << L.real << "+" << L.imag << "i";
       else
       {
           cout << L.real << L.imag << "i";
       }
   }
   
    double abs(Complex L)
   {
       double c;
       c = sqrt(L.real * L.real + L.imag * L.imag);
       return c;
   }

源代码task2.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();
}

运行测试截图:

实验任务3:
源代码task3.cpp:

点击查看代码
#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();
}

运行测试截图:

实验任务4:
源代码Fraction.h:

点击查看代码
#pragma once
#include<iostream>
using namespace std;

class Fraction
{
private:
    int up, down;


public:
    static const string doc;
    Fraction(int x = 0, int y = 1);
    Fraction(const Fraction& L);
    int get_up();
    int get_down();
    Fraction negative();

    void yuefen();
    friend void output(Fraction L);
    friend Fraction add(Fraction L1, Fraction L2);
    friend Fraction sub(Fraction L1, Fraction L2);
    friend Fraction mul(Fraction L1, Fraction L2);
    friend Fraction div(Fraction L1, Fraction L2);


};

源代码Fraction.cpp:

点击查看代码
#include "Fraction.h"
#include <iostream>
#include <cmath>


using namespace std;

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


int gongyueshu(int a, int b)
{
    int r;
    while (b > 0)
    {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

Fraction::Fraction(int x, int y):up(x),down(y)
{
    if (down == 0)
    {
        cout << "分母不能为0";
    }
    yuefen();
}

Fraction::Fraction(const Fraction& L)
{
    up = L.up;
    down = L.down;
}

void Fraction::yuefen()
{
    int shu = gongyueshu(abs(up), abs(down));
    up = up / shu;
    down = down / shu;

}



int Fraction::get_up()
{
    return up;
}
int Fraction::get_down()
{
    return down;
}
Fraction Fraction::negative()
{
    return Fraction(-up, down);
}
void output(Fraction L)
{


        if (L.up * L.down < 0)
        {
            if (L.up % L.down == 0)
            {
                cout << "-" << abs(L.up) /abs( L.down);
            }
            else
            {
                cout << "-" << abs(L.up) << "/" << abs(L.down);
            }
        }
        else
        {
            if (L.up % L.down == 0)
            {
                cout << abs(L.up) / abs(L.down);
            }
            else
            {
                cout << abs(L.up) << "/" << abs(L.down);
            }
        }

}

Fraction add(Fraction L1, Fraction L2)
{
    int a, b;
    b = L1.down * L2.down;
    a = L1.up * L2.down + L2.up * L1.down;
    return Fraction(a, b);
}
Fraction sub(Fraction L1, Fraction L2)
{
    int a, b;
    b = L1.down * L2.down;
    a = L1.up * L2.down - L2.up * L1.down;
    return Fraction(a, b);
}
Fraction mul(Fraction L1, Fraction L2)
{
    int a, b;
    a = L1.up * L2.up;
    b = L1.down * L2.down;
    return Fraction(a, b);
}
Fraction div(Fraction L1, Fraction L2)
{
    int a, b;
    a = L1.up * L2.down;
    b= L1.down * L2.up;
    return Fraction(a, b);
}

源代码task4.cpp:

点击查看代码
#include "Fraction.h"
#include <iostream>
#include <cmath>


using namespace std;

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


int gongyueshu(int a, int b)
{
    int r;
    while (b > 0)
    {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

Fraction::Fraction(int x, int y):up(x),down(y)
{
    if (down == 0)
    {
        cout << "分母不能为0";
    }
    yuefen();
}

Fraction::Fraction(const Fraction& L)
{
    up = L.up;
    down = L.down;
}

void Fraction::yuefen()
{
    int shu = gongyueshu(abs(up), abs(down));
    up = up / shu;
    down = down / shu;

}



int Fraction::get_up()
{
    return up;
}
int Fraction::get_down()
{
    return down;
}
Fraction Fraction::negative()
{
    return Fraction(-up, down);
}
void output(Fraction L)
{


        if (L.up * L.down < 0)
        {
            if (L.up % L.down == 0)
            {
                cout << "-" << abs(L.up) /abs( L.down);
            }
            else
            {
                cout << "-" << abs(L.up) << "/" << abs(L.down);
            }
        }
        else
        {
            if (L.up % L.down == 0)
            {
                cout << abs(L.up) / abs(L.down);
            }
            else
            {
                cout << abs(L.up) << "/" << abs(L.down);
            }
        }

}

Fraction add(Fraction L1, Fraction L2)
{
    int a, b;
    b = L1.down * L2.down;
    a = L1.up * L2.down + L2.up * L1.down;
    return Fraction(a, b);
}
Fraction sub(Fraction L1, Fraction L2)
{
    int a, b;
    b = L1.down * L2.down;
    a = L1.up * L2.down - L2.up * L1.down;
    return Fraction(a, b);
}
Fraction mul(Fraction L1, Fraction L2)
{
    int a, b;
    a = L1.up * L2.up;
    b = L1.down * L2.down;
    return Fraction(a, b);
}
Fraction div(Fraction L1, Fraction L2)
{
    int a, b;
    a = L1.up * L2.down;
    b= L1.down * L2.up;
    return Fraction(a, b);
}

运行测试截图:

实验任务5:
源代码acccount.h:

点击查看代码
#pragma once
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;

};

源代码acccount.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;

}

运行测试截图:

标签:std,对象,double,编程,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/ykbgxlb/p/18494273

相关文章

  • 实验3
    实验任务1:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf("分数:%d,......
  • C++ 网络编程 IO多路复用、select、poll、epoll知识点总结
    1.什么是I/O多路复用?I/O多路复用(I/OMultiplexing)是一种编程技术,允许一个线程或进程同时管理多个I/O通道(如文件描述符、套接字等)。它使得单个进程能够在不使用多个线程或进程的情况下,同时处理多个I/O操作。这在网络编程和高性能服务器中尤为重要,因为它可以有效地利用系......
  • JUC并发编程1
    JUC并发编程1.常见的加锁方式1.1synchronized关键字要求:多个线程并行执行,依次实现对数字的+1、-1操作。即一次+1、一次-1,依次执行。Share类classShare{privateintnumber=0;publicsynchronizedvoidincr()throwsInterruptedException{//......
  • 实验三
    实验一:源代码:1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;67while(scanf("%d",&score)!=EOF){8grade=score_to_grade(score);9printf(&quo......
  • 《DNK210使用指南 -CanMV版 V1.0》第三十四章 image图像滤波实验
    第三十四章image图像滤波实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)......
  • 实验3(2.0)
    任务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,g......
  • 实验3
    任务一#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//函数调用11......
  • 在C语言中进行网络编程时,有哪些辅助工具可用
    标题:在C语言中进行网络编程时,有哪些辅助工具可用?在C语言中进行网络编程时,可用的辅助工具包括套接字库(如Winsock、BSDSockets)、协议库(如OpenSSL)、网络调试工具(如Wireshark)、以及集成开发环境(如Eclipse、VisualStudio)。这些工具为开发者提供了强大的支持,使得在C语言中进行网络编......
  • HNU-操作系统实验lab6-2022级
    实验目的任务调度是操作系统的核心功能之一。UniProton实现的是一个单进程支持多线程的操作系统。在UniProton中,一个任务表示一个线程。UniProton中的任务为抢占式调度机制,而非时间片轮转调度方式。高优先级的任务可打断低优先级任务,低优先级任务必须在高优先级任务挂起或......