zoukankan      html  css  js  c++  java
  • 实验三 类和对象Ⅱ

    任务四:

    //vector_int.hpp

     1 #ifndef BALL_H
     2 #define BALL_H
     3 
     4 #include<iostream>
     5 #include<cstdlib>
     6 
     7 using namespace std;
     8 
     9 class Vector_int{
    10 private:
    11     int n, m;
    12     int * p;
    13 public:
    14     Vector_int(int n0);
    15     Vector_int(int n0, int m0);
    16     Vector_int(Vector_int & x);
    17     ~Vector_int(); 
    18     int & at(int i);
    19     void print(){
    20         for(int i=0; i<n; i++)
    21             cout << p[i] << " ";
    22         cout << endl;
    23     }
    24 }; 
    25 
    26 Vector_int::Vector_int(int n0){
    27     p = new int [n0];
    28     n = n0;
    29     for(int i=0; i<n0; i++)
    30         p[i] = 0;
    31     cout << "构造函数1被调用"  << endl;    
    32 }
    33 Vector_int::Vector_int(int n0, int m0){
    34     p = new int [n0];
    35     n = n0;
    36     for(int i=0; i<n0; i++)
    37         p[i] = m0;
    38     cout << "构造函数2被调用"  << endl;
    39 }
    40 Vector_int::Vector_int(Vector_int & x){
    41     n = x.n;
    42     p = new int [n];
    43     for(int i=0; i<n; i++)
    44         p[i] = x.p[i];
    45     cout << "复制构造函数被调用"  << endl;
    46 }
    47 Vector_int::~Vector_int(){
    48     delete[] p;
    49     cout << "析构函数被调用" << endl; 
    50 }
    51 int & Vector_int::at(int i){
    52     return p[i];
    53 }
    54 
    55 #endif

    //task4.cpp

     1 #include<iostream>
     2 #include"vector_int.hpp"
     3 
     4 using namespace std;
     5 
     6 int main(){
     7     int n;
     8     cin >> n;
     9     Vector_int x(n);
    10     x.print();
    11     Vector_int y(n, 6);
    12     y.print();
    13     Vector_int z(x);
    14     z.at(0) = 999; 
    15     z.print();
    16 }

    运行结果截图:

    任务五:

    //Matrix.hpp

     1 #ifndef MATRIX_H
     2 #define MATRIX_H 
     3 
     4 #include<iostream>
     5 using std::cout;
     6 using std::endl;
     7 class Matrix{
     8 public:
     9     Matrix(int n);
    10     Matrix(int n, int m);
    11     Matrix(const Matrix &X);
    12     ~Matrix();
    13     void set(const double * pvalue);
    14     void set(int i, int j, int value);
    15     double & at(int i, int j);
    16     double at(int i, int j)const;
    17     int get_lines() const;
    18     int get_cols() const;
    19     void print() const;
    20 private:
    21     int lines;
    22     int cols;
    23     double *p;
    24 }; 
    25 
    26 Matrix::Matrix(int n){
    27     lines = n;
    28     cols = n;
    29     p = new double [n*n];
    30 }
    31 
    32 Matrix::Matrix(int n, int m){
    33     lines = n;
    34     cols = m;
    35     p = new double [n*m];
    36 }
    37 Matrix::Matrix(const Matrix &X){
    38     lines = X.lines;
    39     cols = X.cols;
    40     p = new double [X.lines*X.cols];
    41     for(int i=0; i<lines*cols; i++)
    42         p[i] = X.p[i];
    43 }
    44 Matrix::~Matrix(){
    45     delete[] p;
    46 }
    47 void Matrix::set(const double * pvalue){
    48     for(int i=0; i<lines*cols; i++)
    49         p[i] = pvalue[i];
    50 }
    51 void Matrix::set(int i, int j, int value){
    52     p[cols * i + j] = value; 
    53 }
    54 double & Matrix::at(int i, int j){
    55     return p[cols * i + j];
    56 }
    57 double Matrix::at(int i, int j)const{
    58     return p[cols * i + j];
    59 }
    60 int Matrix::get_lines() const{
    61     return lines;
    62 }
    63 int Matrix::get_cols() const{
    64     return cols;
    65 }
    66 void Matrix::print() const{
    67     for(int i=0; i<cols*lines; i++){
    68         cout << p[i] << ", ";
    69         if(!((i+1)%cols))
    70             cout << " 
    ";
    71     }
    72 }
    73 #endif

    //task5.cpp

     1 #include<iostream>
     2 #include"Matrix.hpp"
     3 
     4 int main(){
     5     using namespace std;
     6     
     7     double x[] = {1, 2, 3, 4, 5, 6};
     8     
     9     Matrix m1(3, 2);
    10     m1.set(x);
    11     m1.print();
    12     cout << "the first line is: " << endl;
    13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
    14     cout << endl;
    15     
    16     Matrix m2(2, 3);
    17     m2.set(x);
    18     m2.print();
    19     cout << "the first line is: " << endl;
    20     cout << m2.at(0, 0) << " " << m2.at(0 , 1) << " " << m2.at(0, 2) << endl;
    21     cout << endl;
    22     
    23     Matrix m3(m2);
    24     m3.set(0, 0, 999);
    25     m3.print();
    26 }

    运行结果截图:

  • 相关阅读:
    快速修改elementUI组件样式
    设置占位符颜色!
    缩放屏幕的时候banner被压缩变形!
    less一些用法整理
    CentOS 7 的目录结构介绍
    为什么需要VMware的 快照、克隆?
    虚拟机概述
    Linux的简单介绍
    VMware克隆已安装的虚拟机
    【vim使用】分屏命令、操作分屏
  • 原文地址:https://www.cnblogs.com/wjxing/p/15525730.html
Copyright © 2011-2022 走看看