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

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

时间:2024-10-27 22:47:26浏览次数:5  
标签:std const 对象 imag 编程 int Complex 实验 include

任务1: 

t.h

#pragma once
#include <string>

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;   
   
private:
  int m1, m2;

public:
  static int get_cnt();  
   
public:
  static const std::string doc;   
  static const int max_cnt;   
  
private:
  static int cnt;     
  friend void func();
};

void func();

t.cpp

#include "t.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

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:

#include <iostream>
#include <string>

class Complex {
public:
    static const std::string doc; 
    Complex(double real = 0.0, double imag = 0.0);
    Complex(const Complex& other);
    double get_real() const;
    double get_imag() const;
    void add(const Complex& other);
    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);
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 real, double imag) : real(real), imag(imag) {}

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

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

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

void Complex::add(const Complex& other) {
    real += other.real;
    imag += other.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 !is_equal(c1, c2);
}

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

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

main.cpp:

#include "Complex.h"
#include <iostream>
using std::cout;
using std::endl;
using std::boolalpha;

void output(const Complex& c);

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

任务3:

#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

#include <iostream>
#include string

class Fraction{
    public:
        static const std::string doc;
    private:
        Fraction(int numerator = 0, int denominator = 1);
        int up;
        int down;
        void simplify();
    public:
        int get_up() const;
        int get_down() const;
        Fraction negative() const;
        
        friend output(const Fraction &f1,const Fraction &f2);
        friend add(const Fraction &f1,const Fraction &f2);
        friend sub(const Fraction &f1,const Fraction &f2);
        friend mul(const Fraction &f1,const Fraction &f2);
        friend div(const Fraction &f2,const Fraction &f2);
};

 

标签:std,const,对象,imag,编程,int,Complex,实验,include
From: https://www.cnblogs.com/luyiming/p/18494449

相关文章

  • 2024HIT哈工大计算机网络实验(详细注释、Java代码实现)
    点此去往代码仓库,持续更新实验内容HIT计算机网络实验大部分好像都是用的C/C++代码,网上找很少看到Java的代码,或者不是很详细,所以我自己总结了一下发在了Github上,有详细注释和内容解释,还有一些写代码时的错误以及找错误的心路历程。如果能够对你有所帮助,麻烦点一点star谢谢啦......
  • CUDA编程学习 (3)——内存和数据定位
    1.CUDAMemories1.1GPU性能如何所有thread都会访问globalmemory,以获取输入的矩阵元素在执行一次浮点加法时,需要进行一次内存访问,每次访问传输4字节(即32位浮点数)1FLOP(浮点运算)对应4字节的内存带宽假设的GPU性能:该GPU的峰值浮点计算能力为1,600GFL......
  • CUDA编程学习 (4)——thread执行效率
    1.Warp和SIMD硬件1.1作为调度单位的Warp每个block分为32-threadwarp在CUDA编程模型中,虽然warp不是显式编程的一部分,但在硬件实现上,每个block会被自动划分成若干个包含32个线程的warp。warp作为SM中的调度单元:SM(StreamingMultiprocessor)会以warp......
  • java计算机毕业设计基于web的青少年编程课程评价系统(开题+程序+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容一、研究背景随着信息技术的迅猛发展,编程教育在青少年教育中的地位日益凸显。如今,编程被视为一项关键技能,对青少年的逻辑思维、创造力和问题解决能力有着积极......
  • 实验二
    任务一代码1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数......
  • 实验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;}ch......
  • JAVA 面向对象编程
    随着软件系统的复杂性不断增加,传统的过程式编程方法已经难以满足需求。面向对象编程提供了一种更自然的方式来映射现实世界的问题域到计算机程序中,使得代码更加易于理解、维护和扩展。面向对象编程(Object-OrientedProgramming,OOP)是一种编程范式,它使用“对象”来设计软件和实......
  • 实验三
    实验任务1:task1.c源代码:1#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11......
  • 实验3
    task1#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:......
  • 鸿蒙跨设备协同开发08——使用分布式数据对象接续应用
    如果你也对鸿蒙开发感兴趣,加入“Harmony自习室”吧!扫描下方名片,关注公众号,公众号更新更快,同时也有更多学习资料和技术讨论群。1、前言本文是基于鸿蒙跨设备协同开发07——动态控制应用接续的进一步讨论。我们在鸿蒙跨设备协同开发06——应用接续中有提到:为了接续体验,在o......