国外网站建设品牌,宁波人流,开发一款软件要多少钱,山东电商网站建设#x1f383;个人专栏#xff1a; #x1f42c; 算法设计与分析#xff1a;算法设计与分析_IT闫的博客-CSDN博客 #x1f433;Java基础#xff1a;Java基础_IT闫的博客-CSDN博客 #x1f40b;c语言#xff1a;c语言_IT闫的博客-CSDN博客 #x1f41f;MySQL#xff1a… 个人专栏 算法设计与分析算法设计与分析_IT闫的博客-CSDN博客 Java基础Java基础_IT闫的博客-CSDN博客 c语言c语言_IT闫的博客-CSDN博客 MySQL数据结构_IT闫的博客-CSDN博客 数据结构数据结构_IT闫的博客-CSDN博客 CC_IT闫的博客-CSDN博客 C51单片机C51单片机STC89C516_IT闫的博客-CSDN博客 基于HTML5的网页设计及应用基于HTML5的网页设计及应用_IT闫的博客-CSDN博客 pythonpython_IT闫的博客-CSDN博客 欢迎收看希望对大家有用 目录
第一题 第二题 第三题 答案
第一题
第二题
第三题 第一题 设计一个复数类Complex包含成员变量实部real和虚部imag运算符重载/-实现两个复数加、减的功能。在主函数里创建两个复数对象分别求两个复数的和与差在主函数里显示运算结果。效果如图 第二题 现有一学生类定义 class Student{ …….. private: int _id; string _name; char *_addr; }; 实现学生类的赋值运算符重载主函数调用赋值运算符并输出对象信息。 第三题 编写一个程序声明一个2行2列矩阵类Matrix重载运算符“”使之能用于矩阵的加法运算。重载流插入运算符“”和流提取运算符“”,使之能用于该矩阵的输入与输出。从键盘输入两个矩阵a和b求两个矩阵之和并输出。效果如图 答案
第一题
#include iostreamusing namespace std;class Complex {private:double real;double imag;public:Complex(double x0,double y0):real(x),imag(y) {}void show() const; //输出数据friend Complex operator(const Complex a1, const Complex a2) ; //重载为类的友元函数friend Complex operator-(const Complex a1, const Complex a2);//重载为类的友元函数};/******************************************************************/void Complex::show() const {if(imag0)coutrealimagiendl;elsecoutrealimagiendl;}Complex operator(const Complex a1,const Complex a2) {return Complex(a1.reala2.real,a1.imaga2.imag);}Complex operator-(const Complex a1,const Complex a2) {return Complex(a1.real-a2.real,a1.imag-a2.imag);}/******************************************************************/int main() {Complex c1(5.1,3.4);Complex c2(3.6,5.3);Complex c;coutc1:;c1.show();coutc2:;c2.show();cc1c2;cout c1c2:;c.show();cc1-c2;coutc1-c2:;c.show();return 0;}
第二题
#include iostream#include cstringusing namespace std;class Student {private:int _id;string _name;char *_addr;public:Student(int id,string name,const char *addr);Student();Student operator(Student stu);void show();};Student::Student() {}Student::Student(int id,string name,const char *addr) {/******************************************/ _idid;_namename;this-_addrnew char[strlen(addr)1];if(_addr) strcpy(_addr,addr);/******************************************/ }Student Student::operator(Student stu) {//注意深拷贝/******************************************/ _idstu._id;_namestu._name;_addrnew char[strlen(stu._addr)1];if(_addr) strcpy(_addr,stu._addr);return *this;/******************************************/ }void Student::show() {cout学号:_idendl;cout姓名:_nameendl;cout住址:_addrendl;}int main() {Student stu2,stu1(10001,zhangsan,luoyang);stu1.show();stu2stu1;stu2.show();return 0;}
第三题
#include iostream#include iomanipusing namespace std;class Matrix {private:int a[2][2];friend istream operator(istream is,Matrix _m);friend ostream operator(ostream os,Matrix _m);friend Matrix operator(const Matrix mat1,const Matrix mat2);};ostream operator(ostream os,Matrix _m) {//每个数4列 setw(4)for(int i0; i2; i) {for(int j0; j2; j)ossetw(4)_m.a[i][j];osendl;}return os;}/****************************************************/istream operator(istream is,Matrix _m) {for(int i0; i2; i)for(int j0; j2; j)is_m.a[i][j];return is;}Matrix operator(const Matrix mat1,const Matrix mat2) {Matrix mat;for(int i0; i2; i)for(int j0; j2; j) {mat.a[i][j]mat1.a[i][j]mat2.a[i][j];}return mat;}/****************************************************/int main() {Matrix m1,m2,m;coutinput matrix m1:endl;cinm1;coutinput matrix m2:endl;cinm2;mm1m2;coutoutput matrix m:endl;coutm;return 0;}