
C++几个编程题
1.建立球体类sphere,sphere的构造函数要为球体的半径R(double型)赋初值,用类sphere计算球的体积V,类sphere中还包含一成员函数vol(),用...
1.建立球体类sphere,sphere的构造函数要为球体的半径R(double型)赋初值,用类sphere计算球的体积V,类sphere中还包含一成员函数vol(),用来显示球体对象的体积,请写出完整的程序用来计算某球体的体积并输出,
2.定义一个实现两个浮点数的四则运算的计算器类calculator,其数据成员包括两个运算数和运算结果,几个成员函数分别实现运算数的加减乘除以及运算结果并输出,要求在主函数中输入两个运算数,利用calculator类分别输出他们的加减乘除的运算结果。
希望高手相助 展开
2.定义一个实现两个浮点数的四则运算的计算器类calculator,其数据成员包括两个运算数和运算结果,几个成员函数分别实现运算数的加减乘除以及运算结果并输出,要求在主函数中输入两个运算数,利用calculator类分别输出他们的加减乘除的运算结果。
希望高手相助 展开
2个回答
展开全部
#include<iostream>
#include<stdio.h>
using namespace std;
#define PI 3.1415926
class sphere
{
double R;
public:
sphere(double r)
{
R=r;
}
double vol()
{
return 4/3*PI*R*R*R;
}
void Show()
{
cout<<"半径为"<<R<<"的球体的体积是:"<<vol()<<endl;
}
};
class calculator
{
float x,y;
public:
calculator(float a,float b)
{
x=a,y=b;
}
void add()
{
printf("%f+%f=%f\n",x,y,x+y);
}
void sub()
{
printf("%f-%f=%f\n",x,y,x-y);
}
void mult()
{
printf("%f*%f=%f\n",x,y,x*y);
}
void dev()
{
printf("%f/%f=%f\n",x,y,x/y);
}
void show()
{
add();
sub();
mult();
dev();
}
};
void main()
{
sphere s(3.2);
calculator c(2.3,3.6);
s.Show();
c.show();
}
#include<stdio.h>
using namespace std;
#define PI 3.1415926
class sphere
{
double R;
public:
sphere(double r)
{
R=r;
}
double vol()
{
return 4/3*PI*R*R*R;
}
void Show()
{
cout<<"半径为"<<R<<"的球体的体积是:"<<vol()<<endl;
}
};
class calculator
{
float x,y;
public:
calculator(float a,float b)
{
x=a,y=b;
}
void add()
{
printf("%f+%f=%f\n",x,y,x+y);
}
void sub()
{
printf("%f-%f=%f\n",x,y,x-y);
}
void mult()
{
printf("%f*%f=%f\n",x,y,x*y);
}
void dev()
{
printf("%f/%f=%f\n",x,y,x/y);
}
void show()
{
add();
sub();
mult();
dev();
}
};
void main()
{
sphere s(3.2);
calculator c(2.3,3.6);
s.Show();
c.show();
}
展开全部
楼主..第1个我写出来了 我先把第一个贴上
#include <iostream>
using namespace std;
class sphere
{
public:
sphere();
sphere(double R):Radius(R){}//初始化构造函数
double vol();
private:
double Radius;
};
sphere::sphere()//定义无参构造函数
{
Radius=10;//半径我取的10
}
double sphere::vol()
{
return(4*3.14*Radius*Radius*Radius/3);
}
int main()
{
sphere s;//定义一个sphere类的对象
cout<<"the volume is "<<s.vol();
return 0;
}
#include <iostream>
using namespace std;
class sphere
{
public:
sphere();
sphere(double R):Radius(R){}//初始化构造函数
double vol();
private:
double Radius;
};
sphere::sphere()//定义无参构造函数
{
Radius=10;//半径我取的10
}
double sphere::vol()
{
return(4*3.14*Radius*Radius*Radius/3);
}
int main()
{
sphere s;//定义一个sphere类的对象
cout<<"the volume is "<<s.vol();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询