首页 > 其他分享 >实验4

实验4

时间:2022-11-06 20:36:00浏览次数:37  
标签:m1 int hpp 实验 output vectorInt include

vectorInt.hpp

#pragma once

#include<iostream>
#include<cassert>

using namespace std;

class vectorInt{
    public:
        vectorInt(int n);
        vectorInt(int n,int value);
        vectorInt(const vectorInt &vp);
        ~vectorInt();
        
        int &at(int index);
        int get_size();
        friend void output(vectorInt &T);
        
        private:
            int size;
            int *p;
};

vectorInt::vectorInt(int n) :size{n} {
cout<<"constructor 1 called.\n";
p = new int[n];
}

vectorInt::vectorInt(const vectorInt &vp) :size{vp.size} {
cout<<"copy constructor called.\n";
p=new int[size];
for(int i =0;i<size ;i++)
p[i]=vp.p[i];
} 

vectorInt::vectorInt(int n,int value) :size{n} {
cout<<"cosntructor 2 called.\n";
p = new int[size];
}

vectorInt::~vectorInt() {
    cout<<"destructor called.\n";
    delete[] p;
}

int &vectorInt::at(int index) {
    assert(index >=0 && index <size);
    return p[index];
}

int vectorInt::get_size() {
    return size;
}

void output(vectorInt &T){
    for(int i=0;i<T.get_size();i++)
    cout<<T.p[i]<<" ";
    cout<<endl;
}

main.cpp

#include <iostream>
#include "vectorInt.hpp"

void test() {
    using namespace std;

    int n;
    cin >> n;
    
    vectorInt x1(n);
    for(auto i = 0; i < n; ++i)
    {
        x1.at(i) = i*i;
    }

    output(x1);
    
    vectorInt x2(n, 42);

    vectorInt x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;

    output(x2);
    output(x3);
}

int main() {
    test();
}

 

 

 

Matrix.hpp

#include <iostream>
#include "matrix.hpp"

using namespace std;

void test() {
    double x[] = {1,2,3,4,5,6};
    
    Matrix m1(3,2);
    m1.set(x);
    m1.print();
    cout<<"the first line is:"<<endl;
    cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl;
    cout<<endl;
    
    Matrix m2(2,3);
    m2.set(x);
    m2.print();
    cout<<"the first line is: "<<endl;
    cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl;
    cout<<endl;
    
    Matrix m3(m2);
    m3.set(0,0,999);
    m3.print();
}

int main(){
    test();
}
 

 

main.cpp

#include <iostream>
#include "matrix.hpp"

using namespace std;

void test() {
    double x[] = {1,2,3,4,5,6};
    
    Matrix m1(3,2);
    m1.set(x);
    m1.print();
    cout<<"the first line is:"<<endl;
    cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl;
    cout<<endl;
    
    Matrix m2(2,3);
    m2.set(x);
    m2.print();
    cout<<"the first line is: "<<endl;
    cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl;
    couy<<endl;
    
    Matix m3(m2);
    m3.set(0,0,999);
    m3.print();
}

int main(){
    test();
}
 

 

 

 

标签:m1,int,hpp,实验,output,vectorInt,include
From: https://www.cnblogs.com/wzw252/p/16861724.html

相关文章

  • 实验四 类与数组、指针
    point1.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point():x{0},y{0}{}10......
  • 实验二:逻辑回归算法实验
    【实验目的】理解逻辑回归算法原理,掌握逻辑回归算法框架;理解逻辑回归的sigmoid函数;理解逻辑回归的损失函数;针对特定应用场景及数据,能应用逻辑回归算法解决实际分类问题。......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的1.能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;2.能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境1.下载虚拟机软件OracleVisua......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的1.能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;2.能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境1.下载虚拟机软件OracleVisua......
  • 实验6:开源控制器实践——RYU
    一、实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。查看网络拓扑2.阅读Ryu文档的TheFirstApplic......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubu......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBox或......
  • 实验4 类与数组、指针
    实验5:#include<iostream>usingstd::cout;usingstd::endl;usingstd::cin;classvectorInt{public:vectorInt(intn);vectorInt(intn,intm);~......