C++
第1个回答 2010-10-29
#include<iostream>
using namespace std;
class compute
{
public:
compute(float a,float b){val1=a,val2=b;};
~compute();
private:
float val1,val2;
public:
float add();
float sub();
float multi();
float div();
};
float compute::add()
{
return val1+val2;
}
float compute::sub()
{
return val1-val2;
}
float compute::multi()
{
return val1*val2;
}
float compute::div()
{
if (0==val2)
{
cout<<"div zero,not support!"<<endl;
return -1111111;
}
else
return val1/val2;
}
void main()
{
float num1,num2;
cout<<"please input two numbers: "<<endl;
cin>>num1>>num2;
compute *cmp=new compute(num1,num2);
cout<<"sum is: "<<cmp->add()<<endl;
cout<<"sub is: "<<cmp->sub()<<endl;
cout<<"multi is: "<<cmp->multi()<<endl;
cout<<"div is: "<<cmp->div()<<endl;
return;
}本回答被提问者采纳
using namespace std;
class compute
{
public:
compute(float a,float b){val1=a,val2=b;};
~compute();
private:
float val1,val2;
public:
float add();
float sub();
float multi();
float div();
};
float compute::add()
{
return val1+val2;
}
float compute::sub()
{
return val1-val2;
}
float compute::multi()
{
return val1*val2;
}
float compute::div()
{
if (0==val2)
{
cout<<"div zero,not support!"<<endl;
return -1111111;
}
else
return val1/val2;
}
void main()
{
float num1,num2;
cout<<"please input two numbers: "<<endl;
cin>>num1>>num2;
compute *cmp=new compute(num1,num2);
cout<<"sum is: "<<cmp->add()<<endl;
cout<<"sub is: "<<cmp->sub()<<endl;
cout<<"multi is: "<<cmp->multi()<<endl;
cout<<"div is: "<<cmp->div()<<endl;
return;
}本回答被提问者采纳