设有复数类COMPLEX,在复数类中重载乘法运算符。 下列哪项是运算符重载的正确的声明格式?
A、COMPLEX *(COMPLEX c1, COMPLEX c2);
B、COMPLEX * (COMPLEX c2);
C、COMPLEX operator * (COMPLEX c1, COMPLEX c2);
D、COMPLEX operator * (COMPLEX c2);
A、COMPLEX *(COMPLEX c1, COMPLEX c2);
B、COMPLEX * (COMPLEX c2);
C、COMPLEX operator * (COMPLEX c1, COMPLEX c2);
D、COMPLEX operator * (COMPLEX c2);
【说明】
以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
【C++程序】
ifndef H_MiniComplex
define H_MiniComplex
include <iostream>
using namespace std;
class MiniComplex{
public: //重载流插入和提取运算符
(1) ostream&operator<<(ostream &osObject,const MiniComplex&complex){
osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")";
return osObject;
}
(2) istream&operator>>(istream&isObject, MiniComplex&complex){
char ch;
isObject >>complex.realPart>>ch>>complex.imagPart>>ch;
return isObject;
}
MiniComplex(double real=0,double imag=0); //构造函数
MiniComplex operator+(const MiniComplex&otherComplex)const; //重载运算符+
MiniComplex operator-(const MiniComplex&otherComplex)const; //重载运算符-
MiniComplex operator*(const MiniComplex&otherComplex)const; //重载运算符*
MiniComplex operator/(const MiniComplex&otherComplex)const; //重载运算符/
bool perator==(const MiniComplex&otherComplex)const; //重载运算符==
private :
double (3);
double imagPart;
};
end if
include "MiniComplex.h"
bool MiniComplex::operator==(const MiniComplex&otherComplex)const{
return(realPart==otherComplex.realPart&&imagPart==ortherComplex.imagPart);
}
MiniComplex::MiniComplex(double real,double imag){
realPart== real; imagPart==imagPart;
}
MiniComplex MiniComplex::operator+(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = realPart+ortherComplex. realPart;
temp.imagPart = imagPart +ortherComplex. imagPart;
return temp;
}
(4)
{ MiniComplex temp;
temp.realPart= realPart-ortherComplex. realPart;
temp.imagPart = imagPart-ortherComplex. imagPart;
return temp;
}
MiniComplex MiniComplex::operator*(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart);
temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart);
return temp;
}
MiniComplex MiniComplex::operator/(const MiniComplex&otherComplex)const{
MiniComplex temp;
float tt;
tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart);
temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt;
temp.imagPart =((imagPart *ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt;
return temp;
}
include <iostream>
include <MiniComplex.h>
using namespace std;
int main(){
MiniComplex numl(23, 34),num2(56, 35);
cout<<"Initial Value of num1="<<num1<<"\n Initial Value of num2="<<num2<<end1;
cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符
cout<<num1<<"-"<<num2<<"="<<num
A、虚函数与一般成员函数在基类中没什么区别
B、虚函数不能被调用,而一般成员函数可以
C、虚函数不能定义函数体,而一般成员函数可以
D、虚函数只能定义一个,而一般成员函数可以多个
A、使用基类指针调用派生类的成员函数
B、使用派生类指针调用基类的成员函数
C、使基类指针指向派生类对象
D、使派生类指针指向基类对象
A、使用派生类的对象调用的仍然是基类的这个函数
B、无法使用派生类的对象调用这个函数
C、无法使用基类的对象调用这个函数
D、不允许定义派生类的对象
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!