C + + code learning 01

Keywords: C++

Article catalog

preface

This article is mainly about the creation of two-dimensional matrix in C + + and the application of various operators to two-dimensional matrix.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Experimental content

         ① Constructor
         1.CMatrix():   Constructor without parameters;
         2.CMatrix(int   nRow,   int   nCol,   double  * pData=NULL)  :  Constructor with parameters such as row, column and data pointer,   And the parameter has a default value;
         3.CMatrix(const   char  *  strPath):   Constructor with file path parameter;
         4.CMatrix(const   CMatrix&   m):   copy constructor
         5. In addition, the member variable will be initialized with the list: CMatrix():   m_nRow(0),   m_nCol(0),   m_pData(NULL);
         6.bool   Create(int   nRow,   int   nCol,   double  * pData=NULL):   First delete the original space and create a space according to the incoming rows and columns. If pdata is not empty, copy the content of pdata to m_pData.


        ② Destructor
         1.~CMatrix():   Call Release();
         2.Release():   Release the memory and set the row and column to 0;


        ③ Operator overloading
         1. Arithmetic operator overload: +,  -, +=, -=
         2. Overloading of relational operators: >,  <, ==
         3. Subscript operator: [],   ()
         4 cast type:   double
         5. Assignment operator: =, especially when m1=m1


       ④ Friend function
         Input and output transport symbols: < <,  >>

2, Code

1.CMatrix.h

#ifndef CMATRIX_H
 #define CMATRIX_H
 #include <iostream>
 using namespace std;
class CMatrix
{
public:
     CMatrix();
       CMatrix(int nRow,int nCol,double *pData=NULL);
     CMatrix(const CMatrix& m);
     CMatrix(const char * strPath);
     ~CMatrix();
     bool Create(int nRow,int nCol,double *pData=NULL);
     void Set(int nRow,int nCol,double dVale);
     void Release();
     friend istream & operator>>(istream& is,CMatrix & m);
     friend ostream & operator<<(ostream& os,const CMatrix &m);

    CMatrix& operator=(const CMatrix& m);
    CMatrix& operator+=(const CMatrix& m);
     double & operator[](int nIndex);
     double & operator()(int nRow,int nCol);
     bool operator ==(const CMatrix& m);
     bool operator !=(const CMatrix& m);

     operator double();

 private:
     int m_nRow;
     int m_nCol;
     double *m_pData;
 };
 CMatrix operator+(const CMatrix& m1,const CMatrix& m2);

 inline void CMatrix::Set(int nRow,int nCol,double dVal)
 {
     m_pData[nRow*m_nCol+nCol]=dVal;
 }
 #endif

2.CComplex.h

 #include "CComplex.h"
 #include <math.h>
 #include <stdio.h>
 double Modulus(const SComplex & sc)
 {
     return sqrt(sc.m_dReal*sc.m_dReal+sc.m_dImag*sc.m_dImag);
 }
 void Output(const SComplex & sc)
 {
     printf("%f %f\n",sc.m_dReal,sc.m_dImag);
 }
 void Input(SComplex & sc)
 {
     scanf("%lf%lf",&sc.m_dReal,&sc.m_dImag);
 }
 double CComplex::Modulus()
 {
     return sqrt(m_dReal*m_dReal+m_dImag*m_dImag);
 }
 void CComplex::Output()
 {
     printf("%f %f\n",m_dReal,m_dImag);
 }
 void CComplex::Input()
 {
     scanf("%lf%lf",&m_dReal,&m_dImag);
 }

 istream& operator>>(istream& is, CComplex& cc)
 {
     is>>cc.m_dReal>>cc.m_dImag;
     return is;
 }
 ostream& operator<<(ostream& cout, const CComplex& cc)
 {
     cout<<cc.m_dReal<<" "<<cc.m_dImag<<endl;
     return cout;
 }

3.CMatrix.cpp

#include "CMatrix.h"
 #include <fstream>
 #include <assert.h>
 CMatrix::CMatrix():m_nRow(0),m_nCol(0),m_pData(0)
 {

 }
 CMatrix::CMatrix(int nRow,int nCol,double *pData):m_pData(0)
 {
     Create(nRow,nCol,pData);
 }
 CMatrix::CMatrix(const CMatrix& m):m_pData(0)
 {
     *this = m;
 }
 CMatrix::CMatrix(const char * strPath)
 {
     m_pData = 0;
     m_nRow = m_nCol = 0;
     ifstream cin(strPath);
     cin>>*this;
 }
 CMatrix::~CMatrix()
 {
     Release();
 }
 bool CMatrix::Create(int nRow,int nCol,double *pData)
 {
     Release();
     m_pData = new double[nRow*nCol];
     m_nRow = nRow;
     m_nCol = nCol;
     if(pData)
     {
         memcpy(m_pData,pData,nRow*nCol*sizeof(double));
     }
 }
 void CMatrix::Release()
 {
     if(m_pData)
     {
         delete []m_pData;
         m_pData = NULL;
     }
     m_nRow = m_nCol = 0;
 }
 CMatrix& CMatrix::operator=(const CMatrix& m)
 {
     if(this!=&m){
         Create(m.m_nRow,m.m_nCol,m.m_pData);
     }
     return *this;
 }
 CMatrix& CMatrix::operator+=(const CMatrix& m)
 {
     assert(m_nRow==m.m_nRow && m_nCol==m.m_nCol);
     for(int i=0;i<m_nRow*m_nCol;i++)
     {
         m_pData[i]+=m.m_pData[i];
     }
     return *this;
 }

 CMatrix operator+(const CMatrix& m1,const CMatrix& m2)
 {
     CMatrix m3(m1);
     m3 += m2;
     return m3;
 }
 double & CMatrix::operator[](int nIndex)
 {
     assert(nIndex<m_nRow*m_nCol);
     return m_pData[nIndex];
 }
 double & CMatrix::operator()(int nRow,int nCol)
 {
     assert(nRow*m_nCol+nCol<m_nRow*m_nCol);
     return m_pData[nRow*m_nCol+nCol];
 }
 bool CMatrix::operator == (const CMatrix& m)
 {
     if(!(m_nRow==m.m_nRow && m_nCol==m.m_nCol))
     {
         return false;
     }
     for(int i=0;i<m_nRow*m_nCol;i++)
     {
         if(m_pData[i]!=m.m_pData[i])
         {
             return false;
         }
     }
     return true;
 }
 bool CMatrix::operator !=(const CMatrix& m)
 {
     return !((*this)==m);
 }
 CMatrix::operator double()
 {
     double dS=0;
     for(int i=0;i<m_nRow*m_nCol;i++)
     {
    dS+=m_pData[i];
    }
     return dS;
 }

 istream & operator>>(istream& is,CMatrix & m)
 {
     is>>m.m_nRow>>m.m_nCol;
     m.Create(m.m_nRow,m.m_nCol);
     for(int i=0;i<m.m_nRow*m.m_nCol;i++)
     {
         is>>m.m_pData[i];
     }
     return is;
 }
 ostream & operator<<(ostream& os,const CMatrix &m)
 {
     os<<m.m_nRow<<" "<<m.m_nCol<<endl;
     double * pData = m.m_pData;
     for(int i=0;i<m.m_nRow;i++)
     {
         for(int j=0;j<m.m_nCol;j++)
         {
             os<<*pData++<<" ";
         }
         os<<endl;
     }
     return os;
 }

4.CComplex.cpp

 #include "CComplex.h"
 #include <math.h>
 #include <stdio.h>
 double Modulus(const SComplex & sc)
 {
     return sqrt(sc.m_dReal*sc.m_dReal+sc.m_dImag*sc.m_dImag);
 }
 void Output(const SComplex & sc)
 {
     printf("%f %f\n",sc.m_dReal,sc.m_dImag);
 }
 void Input(SComplex & sc)
 {
     scanf("%lf%lf",&sc.m_dReal,&sc.m_dImag);
 }
 double CComplex::Modulus()
 {
     return sqrt(m_dReal*m_dReal+m_dImag*m_dImag);
 }
 void CComplex::Output()
 {
     printf("%f %f\n",m_dReal,m_dImag);
 }
 void CComplex::Input()
 {
     scanf("%lf%lf",&m_dReal,&m_dImag);
 }

 istream& operator>>(istream& is, CComplex& cc)
 {
     is>>cc.m_dReal>>cc.m_dImag;
     return is;
 }
 ostream& operator<<(ostream& cout, const CComplex& cc)
 {
     cout<<cc.m_dReal<<" "<<cc.m_dImag<<endl;
     return cout;
 }

5.main.cpp

#include <iostream>
 #include "CComplex.h"
 #include <stdio.h>
 #include "CMatrix.h"
 using namespace std;
 int main(int argc, char** argv) {

     double pData[10]={2,3,4,5};
     CMatrix m1,m2(2,5,pData), m3("d:\\1.txt"),m4(m2);
     cout << "Input matrix row:  column:  wide:" << endl;
     cin>>m1;
     m2.Set(1,3,10);
      cout<<"m1="<<m1<<"\nm2="<<m2<<"\nm3="<<m3<<"\nm4="<<m4;
    m4=m3;
     m4[2]=m4+1;
     if(m4==m3)
     {
         cout<<"Error !"<<endl;
     }
     m4 += m3;
     cout<<"sum of m4 = "<<(double)m4<<endl;
     return 0;
 }

3, Test:

 

summary

1.C + + defines all functions and variables in the class in. H (header file), and then implements all these methods one by one in another. cpp file. This experiment also realizes the creation of two-dimensional matrix in this way, and realizes the application through the overloading of various operators;

2. Scope symbol ":" the name of the class is usually preceded by the name of the member of the class. In order to avoid different classes having members with the same name, C + + uses the scope method to distinguish. In this code, there are different types of constructors, such as constructors without parameters (CMatrix::CMatrix():m_nRow(0),m_nCol(0),m_pData(0)) , constructor with row, column, data pointer and other parameters, and parameters with default values (bool CMatrix::Create(int nRow,int nCol,double *pData));

3C + + code takes the class name plus "~" as the destructor, such as CMatrix::~CMatrix(); the function of the constructor is to construct the object with specific values when the object is created and initialize the object to a specific state; contrary to the constructor, when the object ends its life cycle, such as when the function of the object has been called, the system automatically executes the destructor.

4. The friend function of a class is defined outside the class, but has access to all private members and protected members of the class Member. When we need to define how the class inputs and outputs to the outside, we need to access private variables and define them as friend functions. If we want to declare a function as a friend of a class, we need to use the keyword friend before the function prototype in the class definition. Example:   friend istream & operator>>(istream& is,CMatrix & m);

(Note: the examples used in the above summary are included in the code. The code is written by the C + + teacher. This paper explains the function structure, and you can learn the corresponding content of C + + by typing the code by hand.)

Posted by OmarHaydoor on Wed, 13 Oct 2021 06:07:00 -0700