用C++语言定义一个类,实现加减乘除运算
2个回答
展开全部
#include <iostream.h>
class CMy_Math
{
public:
CMy_Math();
~CMy_Math();
public:
double add(double a,double b); //加法
double div(double a,double b);//除法
double subtract(double a,double b);//减法
double mult(double a,double b); //乘法
};
CMy_Math::CMy_Math()
{
}
CMy_Math::~CMy_Math()
{
}
double CMy_Math::add(double a,double b)
{
return a+b;
}
double CMy_Math::subtract(double a,double b)
{
return a-b;
}
double CMy_Math::mult(double a,double b)
{
return a*b;
}
double CMy_Math::div(double a,double b)
{
if ( 0.0 != b)
//b不能为0.0
return a/b;
}
int main(double argc, char* argv[])
{
CMy_Math my;
double sum=0;
sum= my.add(10,5);
cout<<"10+5 = "<<sum<<endl;
sum= my.subtract(10,5);
cout<<"10-5 = "<<sum<<endl;
sum= my.subtract(10,5);
cout<<"10*5 = "<<sum<<endl;
sum= my.div(10,5);
cout<<"10/5 = "<<sum<<endl;
return 0;
}
class CMy_Math
{
public:
CMy_Math();
~CMy_Math();
public:
double add(double a,double b); //加法
double div(double a,double b);//除法
double subtract(double a,double b);//减法
double mult(double a,double b); //乘法
};
CMy_Math::CMy_Math()
{
}
CMy_Math::~CMy_Math()
{
}
double CMy_Math::add(double a,double b)
{
return a+b;
}
double CMy_Math::subtract(double a,double b)
{
return a-b;
}
double CMy_Math::mult(double a,double b)
{
return a*b;
}
double CMy_Math::div(double a,double b)
{
if ( 0.0 != b)
//b不能为0.0
return a/b;
}
int main(double argc, char* argv[])
{
CMy_Math my;
double sum=0;
sum= my.add(10,5);
cout<<"10+5 = "<<sum<<endl;
sum= my.subtract(10,5);
cout<<"10-5 = "<<sum<<endl;
sum= my.subtract(10,5);
cout<<"10*5 = "<<sum<<endl;
sum= my.div(10,5);
cout<<"10/5 = "<<sum<<endl;
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
??????
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询