重庆邮电大学计算机学院C++上机试验报告_重庆邮电大学实验报告

其他范文 时间:2020-02-28 15:32:30 收藏本文下载本文
【www.daodoc.com - 其他范文】

重庆邮电大学计算机学院C++上机试验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“重庆邮电大学实验报告”。

C++集中上机实验日志

实验6—1

一、问题描述

定义一个字符串类String,其数据成员有指向字符串的指针elems,成员函数包括构造函数、析构函数、判断字符串是否为空的operator!()。编程测试类String的功能。

二、实验输出

如图所示:

三、实验思路以及方法

判断字符串是否为空即是对字符串进行非运算,即重载operator!()。逻辑非运算是单目运算符,按照运算符重载方针,应该重载为类的成员函数。由于逻辑非运算结果只有两种:真、假,因此operator!()的返回值类型为bool型。四心得体会

开始没有判断elems的空指针问题,遇到一点麻烦,改过之后就ok了,本实验让我们学习了“运算符重载类的成员函数”,对以后的学习C++有了很大了帮助。代码实现

#include #include using namespace std;cla String { public: String(const char *e=NULL);~String();bool operator!()const;private: char *elems;};String::String(const char *e){ if(e==NULL){ elems=NULL;return;} else { elems=new char[strlen(e)];strcpy(elems,e);return;} } bool String::operator!()const { if(elems==NULL)return true;else return false;} String::~String(){ if(elems!=NULL)delete[]elems;} int main(){ String str;if(!str)cout

实验6-3

一、问题重述

对于具有相同行列数的矩阵进行加、减、乘、转置、赋值运算。

二、实验输出 结果如图所示:

三、实现思路及方法

定义矩阵类Matrix,他的数据成员有:行line、列col、指向矩阵元素的指针int*elems。他的成员函数有:构造函数、析构函数、复制构造函数、一组读取和修改属性的get、set方法、显示矩阵元素的方法print()。还需要对Matrix类的重载运算符:

Matrix operator+(const Matrix &a,const Matrix &b);Matrix operator-(const Matrix &a,const Matrix &b);Matrix operator*(const Matrix &a,const Matrix &b);Matrix operator=(const Matrix &m);

Matrix operator~()const;

四、心得体会

这次对友元的初次使用,感觉到很陌生,在对数组处理的时候有很大的错误,对空间的申请和判断是否为空上面存在很大的问题,但是也学到了很多东西,比如说:友元可以是一个全局函数,也可以是一个类的成员函数,还可以是一个类,如果友元是函数,则称为友元函数,如果友元是一个类,则称为友元类,友元的所有成员函数都是友元函数,可以访问被访问类的所有成员。代码实现

#include #include #include using namespace std;cla Matrix;Matrix operator+(const Matrix &a,const Matrix &b);Matrix operator-(const Matrix &a,const Matrix &b);Matrix operator*(const Matrix &a,const Matrix &b);cla Matrix { friend Matrix operator+(const Matrix &a,const Matrix &b);friend Matrix operator-(const Matrix &a,const Matrix &b);friend Matrix operator*(const Matrix &a,const Matrix &b);public: Matrix(int l,int c);Matrix(const Matrix &m);~Matrix();void setLine(int l);void setCol(int c);void setElems();int getLine()const;int getCol()const;void print()const;Matrix operator=(const Matrix &m);Matrix operator~()const;

private: int line;int col;int *elems;};Matrix operator+(const Matrix &a,const Matrix &b){ if(a.line!= b.line || a.col!= b.col){ cerr

Matrix operator-(const Matrix &a,const Matrix &b){ if(a.line!= b.line || a.col!= b.col){ cerr

Matrix operator*(const Matrix &a,const Matrix &b){ if(a.col!= b.line){ cerr

int line = a.line,col = b.col;int i,j,k;Matrix temp(line,col);

for(i = 0;i

for(i = 0;i

for(j = 0;j

temp.elems[i * col + k] += a.elems[i * col + j] * b.elems[j * col + k];

return temp;} Matrix::Matrix(int l,int c){ setLine(l);setCol(c);elems = new int[line * col];if(elems == NULL){ cerr

Matrix::~Matrix(){ delete []elems;}

Matrix::Matrix(const Matrix &m){ setLine(m.line);setCol(m.col);elems = new int[line * col];if(elems == NULL){ cerr

void Matrix::setLine(int l){ line = l;}

void Matrix::setCol(int c){ col = c;}

void Matrix::setElems(){ for(int i = 0;i > elems[i];}

int Matrix::getLine()const { return line;}

int Matrix::getCol()const { return col;}

void Matrix::print()const { for(int i = 0;i

cout

Matrix Matrix::operator=(const Matrix &m){ if(this!= &m){ setLine(m.line);

setCol(m.col);delete []elems;elems = new int[line * col];if(elems == NULL){

cerr

exit(EXIT_FAILURE);} for(int i = 0;i

elems[i] = m.elems[i];} return *this;}

Matrix Matrix::operator~()const { Matrix temp(col,line);for(int i = 0;i

temp.elems[j * line + i] = elems[i * col + j];return temp;} int main(){ Matrix a(3,3),b(3,3);cout

cout

b.setElems();

Matrix c(3,3);

cout

c=a*b;

c.print();

cout

(~c).print();return 0;}

下载重庆邮电大学计算机学院C++上机试验报告word格式文档
下载重庆邮电大学计算机学院C++上机试验报告.doc
将本文档下载到自己电脑,方便修改和收藏。
点此处下载文档

文档为doc格式

    热门文章
      整站推荐
        点击下载本文