首页 > 其他分享 >实验四类和对象数组及指针

实验四类和对象数组及指针

时间:2022-11-08 18:01:33浏览次数:30  
标签:const Matrix 四类 lines cols int 数组 vectorInt 指针

 1 1 #pragma once
 2  2 #include<iostream>
 3  3 
 4  4 using std::cout;
 5  5 using std::endl;
 6  6 
 7  7 class vectorInt {
 8  8 public:
 9  9     //构造函数和析构函数
10 10     vectorInt(int n);
11 11     vectorInt(int n, int value);
12 12     vectorInt(const vectorInt& X);
13 13     ~vectorInt();  //~vectorInt()=default;  默认合成的析构函数 
14 14 
15 15     //其它成员函数
16 16     int& at(int i);
17 17     int get_size();
18 18 
19 19     //友元函数
20 20     friend void output(const vectorInt& X);
21 21 
22 22 private:
23 23     int size;
24 24     int* p;
25 25 };
26 26 
27 27 vectorInt::vectorInt(int n) :size{ n } {
28 28     cout << "constructor 1 called.\n";
29 29     p = new int[size];
30 30 }
31 31 
32 32 vectorInt::vectorInt(int n, int value) :size{ n }
33 33 {
34 34     cout << "constuctot 2 called" << endl;
35 35     p = new int[size];
36 36     for (int i = 0; i < n; ++i)
37 37         p[i] = value;
38 38 }
39 39 
40 40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size }
41 41 {
42 42     cout << "copy constuctot called" << endl;
43 43     p = new int[X.size];
44 44     for (auto i = 0; i < X.size; ++i)
45 45         p[i] = X.p[i];
46 46 }
47 47 
48 48 vectorInt::~vectorInt() {
49 49     cout << "destructor called.\n";
50 50     delete[]p;
51 51 }
52 52 
53 53 int& vectorInt::at(int i) {
54 54     return p[i];
55 55 }
56 56 
57 57 int vectorInt::get_size() {
58 58     return size;
59 59 }
60 60 
61 61 void output(const vectorInt& X) {
62 62     for (auto i = 0; i < X.size; i++)
63 63         cout << X.p[i] << " ";
64 64     cout << endl;
65 65 }
66 
67 vectorInt.hpp
 1 1 #pragma once
 2  2 #include<iostream>
 3  3 
 4  4 using std::cout;
 5  5 using std::endl;
 6  6 
 7  7 class vectorInt {
 8  8 public:
 9  9     //构造函数和析构函数
10 10     vectorInt(int n);
11 11     vectorInt(int n, int value);
12 12     vectorInt(const vectorInt& X);
13 13     ~vectorInt();  //~vectorInt()=default;  默认合成的析构函数 
14 14 
15 15     //其它成员函数
16 16     int& at(int i);
17 17     int get_size();
18 18 
19 19     //友元函数
20 20     friend void output(const vectorInt& X);
21 21 
22 22 private:
23 23     int size;
24 24     int* p;
25 25 };
26 26 
27 27 vectorInt::vectorInt(int n) :size{ n } {
28 28     cout << "constructor 1 called.\n";
29 29     p = new int[size];
30 30 }
31 31 
32 32 vectorInt::vectorInt(int n, int value) :size{ n }
33 33 {
34 34     cout << "constuctot 2 called" << endl;
35 35     p = new int[size];
36 36     for (int i = 0; i < n; ++i)
37 37         p[i] = value;
38 38 }
39 39 
40 40 vectorInt::vectorInt(const vectorInt& X) :size{ X.size }
41 41 {
42 42     cout << "copy constuctot called" << endl;
43 43     p = new int[X.size];
44 44     for (auto i = 0; i < X.size; ++i)
45 45         p[i] = X.p[i];
46 46 }
47 47 
48 48 vectorInt::~vectorInt() {
49 49     cout << "destructor called.\n";
50 50     delete[]p;
51 51 }
52 52 
53 53 int& vectorInt::at(int i) {
54 54     return p[i];
55 55 }
56 56 
57 57 int vectorInt::get_size() {
58 58     return size;
59 59 }
60 60 
61 61 void output(const vectorInt& X) {
62 62     for (auto i = 0; i < X.size; i++)
63 63         cout << X.p[i] << " ";
64 64     cout << endl;
65 65 }
66 
67 vectorInt.hpp

测试截图:

 

 Task 6

#pragma once

#include <iostream>


using std::cout;
using std::endl;

class Matrix {
public:
    Matrix(int n);                    
    Matrix(int n, int m);              
    Matrix(const Matrix &X);           
    ~Matrix();                         

    void set(const double *pvalue);     
    void set(int i, int j, int value);
    double &at(int i, int j);          
    double at(int i, int j) const;     
    int get_lines() const;             
    int get_cols() const;            
    void print() const;             

private:
    int lines; 
    int cols;  
    double *p; 
};
Matrix::Matrix(int n) : lines{n},cols{n} 
{
    p=new double[lines*cols];
};
Matrix::Matrix(int n, int m) : lines{n},cols{m}
{
    p=new double[lines*cols];
};
Matrix::Matrix(const Matrix &X)
{
    lines=X.lines;
    cols=X.cols;
    p=new double[lines*cols];
    for(int i=0;i<lines*cols;i++)
    p[i]=X.p[i];
}
Matrix::~Matrix()  {delete[] p;}
void Matrix::set(const double *pvalue)
{
    int i=0,j=0;
    while(i<lines*cols)
    {
        p[i++]=pvalue[j++];
    }
}
void Matrix::set(int i, int j, int value)
{
    p[i*cols+j]=value;
}
double &Matrix::at(int i, int j)
{
    return p[i*cols+j];
}
int Matrix::get_lines() const
{
    return lines;
}
int Matrix::get_cols() const
{
    return cols;
}
void Matrix::print() const
{
    int i=0;
    while(i<lines)
    {
        int j=0;
        while(j<cols)
        {
            cout<<p[i*cols+j]<<" ";
            j++;
        }   
        cout<<endl;
        i++;
    }
}
#include<iostream>
#include"matrix.hpp"
void test(){
    using namespace std;
    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();
}

测试截图:

 

 

标签:const,Matrix,四类,lines,cols,int,数组,vectorInt,指针
From: https://www.cnblogs.com/yy-L886/p/16870624.html

相关文章

  • mysql根据json字段内容作为查询条件(包括json数组)检索数据
    最近用到了mysql5.7的json字段的检索查询,发现挺好用的,记录一下笔记我们有一个日志表,里面的data字段是保存不同对象的json数据,遇到想根据里面的json的字段内容作为条件查询......
  • 8种现代数组方法,每个开发人员都应该知道
    英文| https://javascript.plainenglish.io/8-modern-array-methods-that-every-developer-should-know-416855e01757翻译|小爱在用代码执行数组操作时,你是否经想过,关于......
  • javascript中数组的22种方法
    数组总共有22种方法,本文将其分为对象继承方法、数组转换方法、栈和队列方法、数组排序方法、数组拼接方法、创建子数组方法、数组删改方法、数组位置方法、数组归并方法和数......
  • 湍流数组 双指针+数学 奇偶分数组 字符串
    978.最长湍流子数组dp[0][0]=dp[0][1]=1;初始化一个数for(inti=1;i<n;i++){dp[i][0]=dp[i][1]=1;if(arr[i]>arr[i-1]){dp[i][0]=dp[i-1][1......
  • C语言指针
    在C语言中,有两种方式得到一个变量方式一:直接找到变量方式二:间接找到变量。也就是先找到变量的地址,然后再根据地址解析得到该变量。......
  • elementUI table 数组中各项加逗号隔开
    js处理//this.newTableData数组this.newTableData.map((item,index)=>{//item.label数组if(item.label){......
  • AcWing 3583 整数分组(01背包 + 双指针)
    原题链接本题是比较明显的01背包,选或者不选,中间可以用双指针找到最后可以选到的区间长度,那么如果选当前最后一个区间的话最后就要求这个区间前面的长度要最大状态表示:f[......
  • 使用前缀和数组解决"区间和查询"问题
    本文已收录到 GitHub·AndroidFamily,有Android进阶知识体系,欢迎Star。技术和职场问题,请关注公众号[彭旭锐]进Android面试交流群。前言大家好,我是小彭。今......
  • 4 种将字符串转换为字符数组的方法
    英文|https://javascript.plainenglish.io/4-ways-of-transforming-a-string-into-an-array-of-characters-8649e3abfd8d翻译|杨小二在某些情况下,我们希望将字符串转换......
  • 6种JavaScript判断数组是否包含某个值的方法
    我们在项目开发过程中,经常会要检查一个数组(无序)是否包含一个特定的值?这是一个在JavaScript中经常用到的并且非常有用的操作。下面给出几种实现方式。方式一:利用循环这种方......