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

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

时间:2023-10-19 19:14:02浏览次数:41  
标签:float const 对象 void 编程 int Complex 实验 Rect

1.实验任务1

task.1 程序代码:

# include<iostream>
# include<string>
# include<vector>
# include <array>

template <typename T>
void output1(const T &obj){
    for(auto i : obj)
        std::cout << i << ",";
    std::cout << "\b \b \n";
} 

template <typename T>
void output2(const T &obj){
    for(auto p = obj.begin(); p != obj.end(); ++p)
        std :: cout << *p << ",";
    std::cout << "\b \b \n";
}

void test_array(){
    using namespace std;
    
    array<int,5> x1;
    cout << "x1.size()= " << x1.size() << endl;
    x1.fill(42);
    x1.at(0) = 999;
    x1[4] = -999;
    cout << "x1: ";
    output1(x1);
    cout << "x1: ";
    output2(x1);
    
    array<int,5> x2(x1);
    cout << boolalpha << (x1 == x2) << endl;
    x2.fill(22);
    cout << "x2: ";
    output1(x2);
    
    swap(x1,x2);
    cout << "x1: ";
    output1(x1);
    cout << "x2: ";
    output1(x2);
}


void test_vector(){
    using namespace std;
    
    vector<int> v1;
    cout << v1.size() << endl;
    cout << v1.max_size() << endl;
    v1.push_back(55);
    cout << "v1: ";
    output1(v1);
    
    vector<int> v2 {1,0,5,2};
    v2.pop_back();
    v2.erase(v2.begin());
    v2.insert(v2.begin(),999);
    v2.insert(v2.end(),-999);
    cout << v2.size() << endl;
    cout << "v2: ";
    output2(v2);
    
    vector<int> v3(5,42);
    cout << "v3: ";
    output1(v3);
    
    vector<int> v4(v3.begin(),v3.end()-2);
    cout << "v4: ";
    output1(v4);    
}



void test_string(){
    using namespace std;
    
    string s1{"opp"};
    cout << s1.size() << endl;
    for(auto &i: s1)
        i -= 32;
    s1 += "2023";
    s1.append(", hello");
    cout << s1 <<endl;        
}


int main(){
    using namespace std;
    
    cout << "==============测试1:array模板类基础用法==============" << endl;
    test_array();
    
    cout << "\n==============测试2;vector模板类基础用法===============" << endl;
    test_vector();
    
    cout << "\n===============测试3:spring模板类基础用法===============" << endl;
    test_string();
    
}

task.1 运行测试:

 

 

2.实验任务2

task.2 程序代码:

# include<iostream>
# include<complex>

void test_std_complex(){
    using namespace std;
    complex<double> c1{3,4},c2{4.5};
    const complex<double> c3{c2};
    
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c3.real = " <<  c3.real() << "," << "c3.imag = " << c3.imag() << endl;
    
    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    cout << "abs(c1) = " << abs(c1) << endl;
    
    cout << boolalpha;
    cout << "c1 == c2: " << (c1 == c2) << endl;
    cout << "c3 == c2: " << (c3 == c2) << endl;    
    
    complex<double> c4 = 2;
    cout << "c4 = " << c4 << endl;
    c4 += c1;
    cout << "c4 = " << c4 << endl;
    
}

int main(){
    test_std_complex();    
}

task.2 运行测试:

 

 

 3.实验任务3

task.3 程序代码:

# include<iostream>
# include<string>

using namespace std;

class T{
public:
    T(int x = 0,int y = 0);
    T(const T &t);
    T(T &&t);
    ~T();
    
    void set_m1(int x);
    int get_m1() const;
    int get_m2() const;
    void display() const;
    
    friend void func();
    
private:
    int m1,m2;
    
public:
    static void display_count();
    
public:
    static const string doc;
    static const int max_count;
    
private:
    static int count;
}; 


const string T::doc{"a simple class"};
const int T::max_count = 99;
int T::count = 0;

T::T(int x,int y):m1{x},m2{y}{
     ++count;
     cout << "constructor called.\n";
}

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

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

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


void T::set_m1(int x){
    m1 = x;
}

int T::get_m1() const { return m1;}

int T::get_m2() const { return m2;}

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

void T::display_count(){ cout << "T objects: " << count<< endl;}

void func(){
    T t1;
    t1.set_m1(55);
    t1.m2 = 77;
    t1.display();
}

void test(){
    cout << "T class info: " << T::doc << endl;
    cout << "T objects max_count: " << T::max_count << endl;
    T::display_count();
    
    T t1;
    t1.display();
    t1.set_m1(42);
    
    T t2{t1};
    t2.display();
    
    T t3{std::move(t1)};
    t3.display();
    t1.display();
    
    T::display_count();
}

int main(){
    cout << "=============测试类T=============" << endl;
    test();
    cout << endl;
    
    cout << "==============测试友元函数func()=============" <<endl;
    func();
}

task.3 运行测试:

 

 

 4.实验任务4

task.4 程序代码:

# include<iostream>
# include<string>
# include<iomanip>

using namespace std;

class Rect{
public:
    Rect(float l = 2.0 , float w = 1.0);
    Rect(const Rect &r);
    ~Rect();
    
    float len() const;
    float wide() const;
    float area() const;
    float circumference() const;
    void resize(float times);
    void resize(float l_times,float w_times);
     
private:
    float length , width;
    
public:
    static void size_info();
    
public:
    static const string doc;
    
private:
    static int size;
    
}; 

const string Rect::doc{"a simple Rect class"};
int Rect::size = 0;

Rect::Rect(float l,float w):length{l},width{w}{++size;}
Rect::Rect(const Rect &r):length{r.length},width{r.width}{++size;}
Rect::~Rect(){--size;}

float Rect::len() const {return length;}
float Rect::wide() const{return width;}
float Rect::area() const {return length*width;}
float Rect::circumference() const {return 2*(length+width);}

void Rect::resize(float times){
    length = length*times;
    width = width*times;    
}

void Rect::resize(float l_times,float w_times){
    length = length*l_times;
    width = width*w_times;
}

void Rect::size_info(){
    cout << size;
}


void output(const Rect &r){
    cout << "矩形信息:" << endl;
    cout << fixed << setprecision(2);
    cout << left << setw(10) << "长:" << r.len() << endl;
    cout << left << setw(10) << "宽:" << r.wide() << endl;
    cout << left << setw(10) << "面积:" << r.area() << endl;
    cout << left << setw(10) << "周长:" << r.circumference() << endl; 
     
}

void test(){
    cout << "矩形类信息:" << Rect::doc << endl;
    cout << "当前矩形对象数目:";
    Rect::size_info() ;
    cout << endl;
    Rect r1;
    output(r1);
    
    Rect r2(4,3);
    output(r2);
    
    Rect r3(r2);
    r3.resize(2);
    output(r3);
    r3.resize(5,2); 
    output(r3);
    cout << "当前矩形对象数目:";
    Rect::size_info();    
    cout << endl;
}

int main(){
    test();
    cout << "当前矩形对象数目:";
    Rect::size_info();
    cout << endl;
}

task.4 运行测试:

 

 

5.实验任务5

task.5 程序代码:

# include<iostream> 
# include<cmath>

class Complex{
public:
    Complex(double r = 0, double i = 0);
    Complex(const Complex &c);
    ~Complex(){};
    
    double get_real() const;
    double get_imag() const;
    
    void add(const Complex &c);
    void show() const;
    
    friend Complex add(const Complex &c1 , const Complex &c2);
    friend bool is_equal(const Complex &c1 , const Complex &c2);
    friend double abs(const Complex &c1);
    
private:
    double real;
    double imag;
    
};

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

void Complex::add(const Complex &c){
    real += c.real;
    imag += c.imag;
}

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

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

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

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

void test(){
    using namespace std;
    
    Complex c1(3,-4);
   const Complex c2(4.5);
    Complex c3(c1);
    
    cout << "c1 = ";
    c1.show();
    cout << endl;
    
    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;
    
    cout << "c3 = ";
    c3.show();
    cout << endl;
    
    cout << "abs(c1) = ";
    cout << abs(c1) << endl;
    
    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1,c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1,c2) << endl;
    
    Complex c4;
    c4 = add(c1,c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;
    
    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

int main(){
    test();
}

task.5 运行测试:

 

 实验总结:通过相关代码编写与运行,自己对于类的定义与实践运用有了更为直观具体的体验与感受。虽然编写代码和调试代码的过程耗费了相当大的精力与耐心,也很煎熬,但是还是对自己编写程序的能力具有一定的提升,推动个人综合素养进步。在平时还需要对一些比较套路化的程序实现代码进行积累与学习,相信对于以后的编写能有比较大的帮助。

 

标签:float,const,对象,void,编程,int,Complex,实验,Rect
From: https://www.cnblogs.com/Rainbow-forest/p/17775394.html

相关文章

  • 电子级PFA试剂瓶50ml 100ml 250ml 500ml高纯实验级PFA取样瓶
    PFA试剂瓶(PFAreagentbottle)一、产品简介PFA试剂瓶又叫PFA样品瓶、PFA取样瓶、PFA广口瓶。我司PFA试剂瓶分为GL45的广口瓶和GL32的细口瓶。PFA塑料的耐化学腐蚀性,对所有化学品都耐腐蚀,摩擦系数在塑料中低,还有很好的电性能,其电绝缘性不受温度影响。因其未添加回料具有低的本底,金......
  • 实验2
    Task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13746#defineN24657intmain()8{9intnumber;10inti;11srand(time(0));//以当前系统时间作为随机种子12for(i=0;i<N;++i){......
  • 实验二 OpenSSL API使用
    SM3测试代码#include<stdio.h>#include<string.h>#include"openssl/evp.h"#include"err.h"voidtDigest(){unsignedcharmd_value[EVP_MAX_MD_SIZE];intmd_len,i;EVP_MD_CTX*mdctx;charmsg1[......
  • Day18_有参装饰器_迭代器_可迭代对象___iter__()方法__next__()方法_for循环原理_自定
    1.Day17复习无参装饰器模版: 2.Day17复习装饰器的补充: 3.有参函数的知识储备: 4.有参装饰器不用语法糖,使用套用的方式从数据源取数据: 5.有参装饰器不用语法糖,使用套用的方式二从数据源取数据: 6.有参装饰器语法糖: 7.有参装饰器模板: 8.迭代器的介绍和为何存在迭......
  • Lab4-事务与并发编程实现
              实验三存储过程与触发器实验目的:学习SQL语言进行编程的基本方法与技术,能够编写存储过程、触发器解决数据库需要处理的复杂问题。实验内容:1、 设计一个存储过程或者自定义函数,练习存储过程的设计方法。2、 设计触发器,理解触发器的工作原理与设计方法......
  • 使用单例模式进行多线程编程
    title:aliases:tags:-工程技术-cpp/并发编程category:-方法stars:url:creation-time:2023-10-1914:32modification-time:2023-10-1915:27:06[[单例模式]]简而言之就是程序中的某个类只能实例化一个对象。因为对象只有一个,在不同线程中实例化的时候,实......
  • 抽象工厂模式:创建高效强大的对象家族
    大家好,欢迎来到程序视点!今天要分享的是工厂模式的最后一种模式:抽象工厂模式。​前言在上一篇文章:设计模式之工厂方法模式最后,我们通过示例展示了两个问题。归纳起来就是:每次增加一个产品时,都需要增加一个具体类和一个对象实现工厂。随着产品类的增多,使得系统中类的个数成......
  • 实验二测试1—— OpenSSL命令测试1
       ......
  • 20211102尹子扬 实验二 openssl命令测试
    点击查看代码openssldgst-sm3-outsn.sm3sn.txt3.(用od打印时发现有\n换行符,所以在第4步时不加-n,否则会生成错误的hash值)点击查看代码od-tc-Ansn.sm34.(正确的是不带-n的hash值)点击查看代码echo"20211102"|openssldgst-sm3代码截图:......
  • Linux shell编程学习笔记8:使用字符串
    一、前言字符串是大多数编程语言中最常用最有用的数据类型,这在Linuxshell编程中也不例外。本文讨论了LinuxShell编程中的字符串的三种定义方式的差别,以及字符串拼接、取字符串长度、提取字符串、查找子字符串等常用字符串操作,,以及反引号在echo和expr命令联合使用时的作用。二......