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

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

时间:2024-10-23 20:31:19浏览次数:6  
标签:const 对象 double 编程 int Complex 实验 Fraction include

实验任务一:

#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: 实现
// 普通函数实现

#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;
}
#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、在返回对象的函数调用中,返回一个对象时创建的临时对象时调用

移动构造函数:避免拷贝对象,而是改变资源指向,提高效率  ; 在显示调用移动构造函数时调用

 析构函数:用来对象被删除前的清理工作,调用后,对象将不存在; 在对象生存期快要结束时调用

问题三:不可以

 

实验任务二:

#pragma once
#include<string>
using namespace std;
class Complex {
private:
    double real;
    double imag;
    
public:
    static string doc;
    Complex(double x = 0, double y = 0);
    Complex(const Complex& c);
    double get_real() const;
    double get_imag() const ;
    void add(const Complex &c);
    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 void output(const Complex &c);
    friend double abs(const Complex &c);
};
//普通函数声明
Complex add(const Complex& c1, const  Complex& c2);
bool is_equal(const Complex& c1, const  Complex& c2);
bool is_not_equal(const Complex& c1, const  Complex& c2);
void output(const Complex& c);
double abs(const Complex& c);
#include "C:/Users/29114/source/repos/MAJORCPP/Complex.h"
#include<iostream>
#include<math.h>
using namespace std;

 string Complex ::doc = "a simplified complex class";


 Complex ::Complex(double x, double y ):real(x),imag(y){}
 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;
 }
 void Complex::add(const Complex& c) {
     real = c.real + 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) {
    if (c1.real == c2.real &&c2.imag == c1.imag)
        return true;
    else
        return false;
}
bool is_not_equal(const Complex& c1, const  Complex& c2) {
    if (is_equal(c1,c2))
        return false;
    else
        return true;
}
 void output(const Complex& c) {
     if(c.imag>=0)
    cout << c.real << " + " << c.imag << "i";
     else
         cout << c.real << c.imag << "i";
}
 double abs(const Complex& c) {
    return sqrt(c.real * c.real + c.imag * c.imag);
}
#include <iostream>
#include "C:/Users/29114/source/repos/MAJORCPP/Complex.h"

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

使用了相应的构造函数,使用了加、取模等操作

实验任务4:

#pragma once
#include<iostream>
using namespace std;
class Fraction {
public:
    const static  string doc;
private:
    int up;
    int down;
public:
    //先声明,在.cpp中实现
    Fraction(int n);
    Fraction(int a, int b);
    Fraction(const Fraction& p);
    int get_up();
    int get_down();
    Fraction negative();//返回求负对象

    friend void output(Fraction a);
    friend Fraction add(Fraction a, Fraction b);
    friend Fraction sub(Fraction a, Fraction b);
    friend Fraction mul(Fraction a, Fraction b);
    friend Fraction div(Fraction a, Fraction b);

};
//普通函数声明
void output(Fraction a);
Fraction add(Fraction a, Fraction b);
Fraction sub(Fraction a, Fraction b);
Fraction mul(Fraction a, Fraction b);
Fraction div(Fraction a, Fraction b);
int gcd(int a, int b);
#include"Fraction.h"
#include<iostream>
#include<cstdlib>


using namespace std;
/*
Fraction(int n):up=n,down=1{}
    Fraction(int a,int b):up=a,down=b{}
    Fraction(const Fraction &p);
    int get_up(Fraction a);
    int get_down(Fraction a);
    Fraction negative();//返回求负对象

    friend void output(Fraction a);
    friend Fraction add(Fraction a,Fraction b);
    friend Fraction sub(Fraction a,Fraction b);
    friend Fraction mul(Fraction a,Fraction b);
    friend Fraction div(Fraction a,Fraction b);
*/
const string Fraction::doc = "Fraction类 v 0.01版.目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
Fraction::Fraction(int n) :up(n), down(1){}
Fraction::Fraction(int x,int y) :up(x), down(y) {}
Fraction::Fraction(const Fraction& p) :up(p.up), down(p.down){}
int Fraction::get_up() {
    return up/ gcd(up, down);
}
int Fraction::get_down() {
    return down / gcd(up, down);
}
Fraction Fraction::negative() {
    Fraction a(-1*up,down);
    return a;
}
void output(Fraction a) {
    int b = gcd(a.up, a.down);
    if ((a.up < 0 && a.down>0) || (a.up > 0 && a.down < 0))
        cout << "-" << abs(a.up / b) << "/" << abs(a.down / b);
    else if (a.down == 1)
        cout << a.up;
    else if (a.down == 0)
        cout << "分子不能为0";
    else if (a.up == 0)
        cout << "0";
    else
        cout << a.up / b << "/" << a.down / b;

}
Fraction add(Fraction a, Fraction b) {
    int down1 = abs(a.down * b.down) / gcd(a.down, b.down);
    int up1 = down1 / a.down * a.up+ down1 / b.down * b.up;
    Fraction c(up1, down1);
    return c;
}
Fraction sub(Fraction a, Fraction b) {
    int down = abs(a.down * b.down) / gcd(a.down, b.down);
    int up1 = down / a.down * a.up;
    int up2 = down / b.down * b.up;
    Fraction c(up1 - up2, down);
    return  c;
}
Fraction mul(Fraction a, Fraction b) {
    Fraction c(a.up * b.up, a.down * b.down);
    return c;
}
Fraction div(Fraction a, Fraction b) {
    Fraction c(a.up * b.down, a.down * b.up);
    return c;
}
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
#include <iostream>
#include "C:/Users/29114/source/repos/MAJORCPP/Fraction.h"

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

实验任务5

 

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include<iostream>
class SavingAccount{
    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:
        SavingAccount(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);//取出现金
        //结算利息,每年1月1日调用一次下函数
        void settle(int date);
        void show()const; 
};
#endif
#include"account.h"
#include<iostream>
#include<cmath>
using namespace std;
double SavingAccount::total=0;
SavingAccount::SavingAccount(int date,int id,double rate):id{id},balance(0),rate(rate),lastDate(date),accumulation(0){
   cout<<date<<"\t#"<<id<<"is created"<<endl;    
}
void SavingAccount::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 SavingAccount::deposit(int date,double amount)
{
    record(date,amount);
}
void SavingAccount::withdraw(int date,double amount)
{
    if(amount>getBalance())
    cout<<"Error:not enough money"<<endl;
    else
    record(date,-amount);
}
void SavingAccount::settle(int date)
{
    double interest=accumulate(date)*rate/365;
    if(interest!=0)
       record(date,interest);
    accumulation=0;
}
void SavingAccount::show()const
{
    cout<<"#"<<id<<"\tBalance:"<<balance;
}
#include"account.h"
#include<iostream>
using namespace std;
int main()
{
    SavingAccount sa0(1,21325302,0.015);
    SavingAccount 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:"<<SavingAccount::getTotal()<<endl;
    return 0;
}

 

标签:const,对象,double,编程,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/0448wyz/p/18494468

相关文章

  • 实验2
    task11#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12~T();......
  • # 20222402 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    1.实验内容本周学习内容①Shellcode技术②后门概念:后门就是不经过正常认证流程而访问系统的通道。③后门案例:XcodeGhost等。④后门技术:狭义后门:特指潜伏于操作系统中专门做后门的一个程序,“坏人”可以连接这个程序,远程执行各种指令。管控功能实现技术自启动技术进程隐藏技......
  • javascript对象介绍
    1.什么是对象?在JavaScript中,对象是一个无序的键值对集合,可以用来存储数据和功能。对象可以包含原始值、函数(方法)以及其他对象,是构建复杂数据结构和实现面向对象编程的基础。2.创建对象2.1字面量方式最常见的创建对象的方法是使用对象字面量:constperson={n......
  • 实验2:简单工厂模式
    [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。实验要求:1.画出对应的类图;2.提交源代码;3.注意编程规范。类图:  2、代......
  • 实验3
    task1点击查看代码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%......
  • 实验2:简单工厂模式
    [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。  1. 类图   2.源代码//抽象产品类:Person接口publicinterfaceP......
  • UML与面向对象程序设计原则
    UML与面向对象程序设计原则本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;2、理解面向对象程序设计原则。 [实验任务一]:UML复习阅读教材第一章复习UML,回答下述问题:面向对象程序设计中类与类的关系都......
  • 20222404 2024-2025-1《网络与系统攻防》 实验二
    1.实验内容(一)本周课程内容了解后门概念,了解后门案例,后门会对系统安全造成的影响。对后门技术进行普及,包括各种进程隐藏技术。了解netcat、meterpreter,veil等常见工具。进一步学习shellcode注入的逻辑和多种情况。(二)问题回答(1)例举你能想到的一个后门进入到你系统中的可能......
  • 实验2 类和对象_基础编程1
    实验任务1代码:t.h:1#pragmaonce23#include<string>45classT{6public:7T(intx=0,inty=0);8T(constT&t);9T(T&&t);10~T();11voidadjust(intratio);12voiddisplay()const;13private......
  • 实验2 类和对象_基础编程1
    task1: t.h:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数void......