當前位置:編程學習大全網 - 編程軟體 - c++習題編程 1.定義和實現壹個復數類ClsComplex,並完成如下的功能要求:

c++習題編程 1.定義和實現壹個復數類ClsComplex,並完成如下的功能要求:

第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();

}

  • 上一篇:良渚玉琮怎麽讀
  • 下一篇:QT英文全稱
  • copyright 2024編程學習大全網