文章目录
前言
记录第一次的C++实验
一、代码展示
Main.CPP
#include<iostream>
using namespace std;
#include"CMatrix.h"
int main()
{
double pData[10] = { 2,3,4,5 };
CMatrix m1, m2(2, 5, pData);
cin >> m1;
m2.Set(1, 3, 10);
cout << m1 << m2;
CMatrix ms[4] = { CMatrix(),CMatrix(2,5,pData),CMatrix(ms[1]),CMatrix("C:\\1.txt") };
cout << ms[1] << ms[2];
if (ms[1] != ms[2])
{
cout << "Error occur!" << endl;
}
ms[1] += ms[2];
ms[1][1] = 100;
ms[1](1, 1) = 50;
cout << ms[1];
cout << "sum of m1=" << double(ms[1]);
double d = 1.2;
int i = int(d);
return 0;
}
CMatric.cpp
#include"CMatrix.h"
#include<fstream>
#include<assert.h>
CMatrix::CMatrix()
{
m_nRow = 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));
}
return true;
}
void CMatrix::Release() {
if (m_pData)
{
delete[]m_pData;
m_pData = NULL;
}
m_nRow = m_nCol = 0;
}
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;
}
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 + 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;
}
CMatrix.h
#pragma once
#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;
}
二、结果展示
总结
一、构造函数:主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。构造函数的功能主要用于在类的对象创建时定义初始化的状态
1..构造函数特点:
(1).命名必须与类名相同
(2).没有返回值,也不能用void修饰
(3).构造函数不能被直接调用,必须通过new运算符在创建对象时才会自动调用;而一般的方法是在程序执行到它的时候被调用的;
2.C++的构造函数定义格式为:
class <类名>
{
public:
<类名>(参数表);
//...(还可以声明其它成员函数)
};
<类名>::<函数名>(参数表)
{
//函数体
}
二、析构函数:当对象结束其生命周期,如对象所在的函数已调用完毕时,系统自动执行析构函数。析构函数往往用来做“清理善后” 的工作(例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。析构函数名也应与类名相同,只是在函数名前面加一个位取反符~,例如~stud( ),以区别于构造函数。它不能带任何参数,也没有返回值(包括void类型)。只能有一个析构函数,不能重载。
C++当中的析构函数格式如下:<类名>::~<类名>(){},即不执行任何操作。
三、友元函数:可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend
C++中友元函数定义格式:friend<返回类型><函数名>(<参数列表>);
四、运算符重载
算术运算符重载:+, -, +=, -=
关系运算符重载:>, <, ==
下标操作符:[], ()
强制类型转换: double
赋值运算符:=,尤其注意当m1=m1特殊情况的处理
输入和输出运输符:<<, >>
参考资料:
1.C++-实验一 CMatrix类设计_我超爱Debug的博客-优快云博客
2.百度