1、 public: cMatrix(int r,int c) row=r;col=c; element=new doublerow*col; double& operator() (int x,int y) return elementcol*(x-1)+y-1; operator() (int x,int y) const CMatrix() delete element; private: double *element; int row,col;void main() CMatrix m(5,8); for(int i=;i5;i+) m(i,1)=i+5; for(i=0; coutm(
2、i,1)”,”;endl;运行结果:5,6,7,8,9,38、定义一个复数类,通过重载运算符:=、+=、+、-、*、/,直接实现两个复数之间的各种运算。编写一个完整的程序(包括测试各种运算符的程序部分)。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)I,而两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。#include using namespace std;class complexpublic: complex(int i=0, int j=0):real(i), ima
3、ge(j) complex(const complex &a): real(a.real), image(a.image) complex& operator=(const complex &a) real = a.real; image = a.image; return *this; operator+=(const complex & real = real + a.real; image = image + a.image; void display() cout ( real , image ) endl; friend complex operator+(const complex
4、&, const complex&); friend complex operator-(const complex& friend complex operator*(const complex& friend complex operator/(const complex&private: int real; int image;complex operator+(const complex &a, const complex &b) complex r(a); r.real += b.real; r.image += b.image; return r;complex operator-
5、(const complex & complex r; r.real = r.real - b.real; r.image = r.image - b.image;complex operator *(const complex &c1, const complex &c2) complex t; t.real=c1.real * c2.real - c1.image * c2.image; t.image = c1.image*c2.real +c1.real* c2.image; return t;complex operator/(const complex &c2) t.real =
6、(c1.real*c2.real + c1.image*c2.image) / (c2.real*c2.real+c2.image*c2.image); t.image = (c2.image*c2.real - c1.real*c2.image) / (c2.real*c2.real + c2.image*c2.image);int main() complex a(32,6); complex b(23,63); complex c; c=a+b; c.display(); a+=b; a.display(); c=a-b; c=a*b; c=a/b; return 0;39、定义一个学生
7、类,数据成员包括:姓名、学号、C+、英语和数学成绩。重载运算符“”,实现学生对象的直接输入输出。增加转换函数,实现姓名的转换。设计一个完整的程序,验证成员函数和重载运算符的正确性。class student student(char *n=):number(201),cpp(0.0),eng(0.0),math(0.0) void set(char *nm,char *num,float _cpp,float ma,float _eng) name=nm; mumber=num; cpp=_cpp; math=ma; eng=_eng; friend ostream& operatorin,
8、student &b); char name20; char number20; float cpp; float math; float eng;ostream& out a.name a.number a.cpp a.math a.eng a.name a.number a.cpp a.math a.eng; student a; a.set(linda,34,64,64); a; student b; cin b;40、定义一个平面直角坐标系上的一个点的类CPoint,重载“+”和“-”运算符,并区分这两种运算符的前置和后置运算,构造一个完整的程序。class CPoint CPoint
9、(int i=0, int j=0): x(i), y(j) CPoint& operator+(); /前置 CPoint operator+(int); /后置 operator-(); CPoint operator-(int); void display(); int x; int y;CPoint& CPoint:operator +() +x; +y;operator -() -x; -y;CPoint CPoint:operator +(int) CPoint ret(*this); return ret;operator -(int)void CPoint:display()
10、x y CPoint dot1(2,5); +dot1; dot1.display(); CPoint dot2(1,2); -dot2; dot2.display(); CPoint dot3(2,2); dot3+; dot3.display();41、在上题的基础上,为其定义友元函数实现重载运算符“+”。 friend CPoint operator+(const CPoint &a, const CPoint &CPoint operator+(const CPoint & CPoint t(a); t.x += b.x; t.y += b.y; CPoint dot4; dot4 = dot1 + dot2; dot4.display();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1