第1題.?
#include<iostream>using?namespace?std;
class?ClsComplex{
private:
double?real;
double?img;
public:
ClsComplex(){
real?=?0;
img?=?0;
}
ClsComplex(double?r,?double?i){
real?=?r;
img?=?i;
}
void?show(){
cout<<real<<'?'<<img<<endl;
}
}; 第二題:
#include<iostream>using?namespace?std;
class?ClsComplex{
private:
double?real;
double?img;
public:
ClsComplex(){
real?=?0;
img?=?0;
}
ClsComplex(double?r,?double?i){
real?=?r;
img?=?i;
}
void?show(){
cout<<real<<'?'<<img<<endl;
}
friend?double?abs(ClsComplex?a,?ClsComplex?b);
};
double?abs(ClsComplex?a?,?ClsComplex?b){
return?sqrt((a.real?-?b.real)*(a.real-b.real)?+?(a.img?-?b.img)?*(a.img?-?b.img));
}
void?main(){
ClsComplex?c1,?c2(3,4);
cout<<abs(c1,?c2);
}
第三題:
#include<iostream>
using?namespace?std;
class?Shape{
public:
virtual?float?peri()=0;?
};
class?Rect:public?Shape{
private:
float?width;
float?length;
public:
float?peri(){
return?2*(width?+?length);
}
Rect(float?w,?float?l){
width?=?w;
length?=?l;
}
};
class?Circle:?public?Shape{
private:?
float?r;
public:
float?peri(){
return?2*(3.1415926?*?r);
}
};
void?main(){
Rect?r(1,?1);
Shape?*p=&r;
cout<<?p->peri();
}