南邮关于c++多态性的上机题,写的时候还是遇到了很多问题的。
1
定义复数类Complex,有实部、虚部两个私有成员变量,在该类中定义多个重载的构造函数、定义析构函数和输出函数print,复数的输出形如12-3i,在类中重载+、−、*、/、++(分前++和后++)。在主函数(直接用实验教材P210代码)中定义复数类的对象,实现复数的各种算术运算,通过重载实现静态多态性。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(float r=0,float i=0);
void print();
friend Complex operator + (const Complex &a,const Complex &b);
friend Complex operator - (const Complex &a,const Complex &b);
friend Complex operator ++ (Complex &a);
Complex operator ++ (int);
Complex operator * (const Complex &b);
Complex operator / (const Complex &b);
};
Complex::Complex(float r,float i)
{
real=r;
imag=i;
}
void Complex::print()
{
cout<<real;
if(imag!=0)
{
if(imag>0)cout<<"+";cout<<imag<<"i";
}
cout<<endl;
}
Complex operator + (const Complex &a,const Complex &b)
{
Complex temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
Complex operator - (const Complex &a,const Complex &b)
{
Complex temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
Complex operator ++(Complex &a)
{
++a.real;
++a.imag;
return a;
}
Complex Complex::operator ++ (int)
{
Complex temp(*this);
real++;
imag++;
return temp;
}
Complex Complex::operator * (const Complex &b)
{
Complex temp;
temp.real=this->real*b.real-this->imag*b.imag;
temp.imag=this->real*b.imag+this->imag*b.real;
return temp;
}
Complex Complex::operator / (const Complex &b)
{
Complex temp;
temp.real=(this->real*b.real+this->imag*b.imag)/(b.real*b.real+b.imag*b.imag);
temp.imag=(this->imag*b.real-this->real*b.imag)/(b.real*b.real+b.imag*b.imag);
return temp;
}
int main()
{
Complex A1(2.3,4.6),A2(3.6,2.8);
Complex A3,A4,A5,A6;
A3=A1+A2;
A4=A1-A2;
A5=A1*A2;
A6=A1/A2;
cout<<"A1=";
A1.print();
cout<<endl<<"A2=";
A2.print();
cout<<endl<<"A3=A1+A2=";
A3.print();
cout<<endl<<"A4=A1-A2=";
A4.print();
cout<<endl<<"A5=A1*A2=";
A5.print();
cout<<endl<<"A6=A1/A2=";
A6.print();
A3=++A1;
cout<<endl<<"after A3=++A1";
cout<<"A1=";
A1.print();
cout<<"A3=";
A3.print();
A4=A2++;
cout<<endl<<"after A4=A2++";
cout<<"A2=";
A2.print();
cout<<"A4=";
A4.print();
return 0;
}
2
设计一个矩阵类,要求矩阵类中重载运算符加(+)和赋值(=),主函数定义类对象并调用重载的运算符。
提示: (1) 本题考虑可加(即加与被加矩阵的行列数必须分别相等)和可赋值(等号左值的行列数变为右值的行列数)情况,其他情况输出“ program terminated! ”
(2) 要求分别输入矩阵 am 和 bm 的行列数,各矩阵元素,分别计算 cm=am+bm;am=bm; 并进行输出
(3) 定义相应的构造函数和析构函数
(4) 类中的成员变量应当有三个:int row,co请动态空间,存放row*col个整数
(5) 程l;分别表示矩阵的行数和列数,另外还需要定义一个一级指针或二级指针用来申序最前面的文件包含请用下面代码:
#include
#include <stdlib.h>
using namespace std;
(6)main()已给出,请直接复制使用1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21int main()
{
int row_a,col_a,row_b,col_b;
cout<<"请输入am矩阵的行数和列数:"<<endl;
cin>>row_a>>col_a;
Matrix am(row_a,col_a);
cout<<"请输入bm矩阵的行数和列数:"<<endl;
cin>>row_b>>col_b;
Matrix bm(row_b,col_b),cm;
cout<<"am:"<<endl;
am.disp();
cout<<"bm:"<<endl;
bm.disp();
cm=am+bm;
cout<<"cm=am+bm:"<<endl;
cm.disp();
am=bm;
cout<<"am=bm:"<<endl;
am.disp();
return 0;
}
(7)类的成员函数disp的代码已给出,请直接复制使用:1
2
3
4
5
6
7
8
9
10void Matrix::disp()
{
for(int i=0;i<row;i++)
{
cout<<'\t';
for(int j=0;j<col;j++)
cout<<*(m+i*col+j)<<'\t';
cout<<endl;
}
}
最后代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using namespace std;
class Matrix
{
private:
int *m;
int row;
int col;
public:
Matrix(int r=0,int c=0)
{
row=r;
col=c;
m= new int[row*col];
if(row!=0&&col!=0)
{
cout<<"请输入该矩阵元素:" <<endl;
for (int i=0;i<r*c;i++)
{
cin>>*(m+i);
}
}
}
~Matrix()
{
delete [] m;
}
void disp()
{
for(int i=0;i<row;i++)
{
cout<<'\t';
for(int j=0;j<col;j++)
cout<<*(m+i*col+j)<<'\t';
cout<<endl;
}
}
friend Matrix operator + ( const Matrix &A, const Matrix &B);
Matrix & operator = (const Matrix &B)
{
if((this->row==0)&&(this->col==0))//用来判断cm=am+bm这种情形
{
this->row=B.row;
this->col=B.col;
m= new int[row*col];
for(int i=0;i<(B.row*B.col);i++)
{
this->m[i]=B.m[i];
}
}
if(((this->row==B.row)&&(this->col==B.col)))//判断am=bm这种情形
{
delete [] m;
for(int i=0;i<(B.row*B.col);i++)
{
this->m[i]=B.m[i];
}
}
else
{
cout<<"program terminated!"<<endl;
exit(0);
}
}
};
Matrix operator +(const Matrix& A,const Matrix& B)
{
Matrix C;
if((A.row==B.row)&&(A.col==B.col))
{
C.row=A.row;
C.col=A.col;
C.m=new int[(C.row*C.col)];
for(int i=0;i<(A.row*A.col);i++)
{
C.m[i]=A.m[i]+B.m[i];
}
}
else
{
cout<<"program terminated!"<<endl;
exit(0);
}
return C;
}
int main()
{
int row_a,col_a,row_b,col_b;
cout<<"请输入am矩阵的行数和列数:"<<endl;
cin>>row_a>>col_a;
Matrix am(row_a,col_a);
cout<<"请输入bm矩阵的行数和列数:"<<endl;
cin>>row_b>>col_b;
Matrix bm(row_b,col_b),cm;
cout<<"am:"<<endl;
am.disp();
cout<<"bm:"<<endl;
bm.disp();
cm=am+bm;
cout<<"cm=am+bm:"<<endl;
cm.disp();
am=bm;
cout<<"am=bm:"<<endl;
am.disp();
return 0;
}