早教吧作业答案频道 -->其他-->
C++编程:编写一个关于圆形的程序.模仿编程:classRectangle{private:intlength,width;public:Rectangle(){length=0;width=0;}Rectangle(inta,intb){length=a;width=b;}Rectangle(Rectangle&r){length=r.length;width=
题目详情
C++编程:编写一个关于圆形的程序.
模仿编程:
class Rectangle
{
private :
int length,width ;
public :
Rectangle()
{ length = 0; width = 0; }
Rectangle( int a,int b )
{ length = a; width = b; }
Rectangle( Rectangle &r )
{
length = r.length ; width = r.width ;
}
void Set( int a,int b )
{ length = a; width = b; }
……
} ;
void main()
{
Rectangle rect1 ;
rect1.Set( 5,10 ) ;
……
Rectangle rect2( 2*5,2*10 ) ;
……
Rectangle rect3( rect1 ) ;
……
}
编写一个关于圆形的程序.要求用C++类的方法定义一个圆的类,其中有:
①1个私有数据成员(半径)
②3个公有函数成员(设置半径、计算面积、计算周长)
③3个构造函数(分别为不带参数的构造函数、带1个参数的构造函数和1个拷贝构造函数)
主程序使用这个类来创建圆对象,要求:
①定义一个圆对象c1,从键盘输入一个值x并将其设定为c1的半径,计算并显示c1的面积和周长
②再定义一个圆对象c2,并将半径初始化为2x,计算并显示c2的面积和周长
③再定义一个圆对象c3,并用c1初始化c3,计算并显示c3的面积和周长
模仿编程:
class Rectangle
{
private :
int length,width ;
public :
Rectangle()
{ length = 0; width = 0; }
Rectangle( int a,int b )
{ length = a; width = b; }
Rectangle( Rectangle &r )
{
length = r.length ; width = r.width ;
}
void Set( int a,int b )
{ length = a; width = b; }
……
} ;
void main()
{
Rectangle rect1 ;
rect1.Set( 5,10 ) ;
……
Rectangle rect2( 2*5,2*10 ) ;
……
Rectangle rect3( rect1 ) ;
……
}
编写一个关于圆形的程序.要求用C++类的方法定义一个圆的类,其中有:
①1个私有数据成员(半径)
②3个公有函数成员(设置半径、计算面积、计算周长)
③3个构造函数(分别为不带参数的构造函数、带1个参数的构造函数和1个拷贝构造函数)
主程序使用这个类来创建圆对象,要求:
①定义一个圆对象c1,从键盘输入一个值x并将其设定为c1的半径,计算并显示c1的面积和周长
②再定义一个圆对象c2,并将半径初始化为2x,计算并显示c2的面积和周长
③再定义一个圆对象c3,并用c1初始化c3,计算并显示c3的面积和周长
▼优质解答
答案和解析
#include
using namespace std;
#define PI 3.141592635
class Circle
{
public:
Circle() : _r(0.f){}
Circle(double r) : _r(r){}
Circle(const Circle& circle)
{
this->_r = circle._r;
}
void setRadius(double r)
{
this->_r = r;
}
double Area()
{
return PI * this->_r * this->_r;
}
double Circumference()
{
return 2 * PI * this->_r;
}
private:
double _r;
};
int main()
{
Circle c1;
double x;
cin >> x;
c1.setRadius(x);
cout << "c1面积:" << c1.Area() << endl;
cout << "c1周长:" << c1.Circumference() << endl;
Circle c2(2 * x);
cout << "c2面积:" << c2.Area() << endl;
cout << "c2周长:" << c2.Circumference() << endl;
Circle c3(c1);
cout << "c3面积:" << c3.Area() << endl;
cout << "c3周长:" << c3.Circumference() << endl;
return 0;
}
using namespace std;
#define PI 3.141592635
class Circle
{
public:
Circle() : _r(0.f){}
Circle(double r) : _r(r){}
Circle(const Circle& circle)
{
this->_r = circle._r;
}
void setRadius(double r)
{
this->_r = r;
}
double Area()
{
return PI * this->_r * this->_r;
}
double Circumference()
{
return 2 * PI * this->_r;
}
private:
double _r;
};
int main()
{
Circle c1;
double x;
cin >> x;
c1.setRadius(x);
cout << "c1面积:" << c1.Area() << endl;
cout << "c1周长:" << c1.Circumference() << endl;
Circle c2(2 * x);
cout << "c2面积:" << c2.Area() << endl;
cout << "c2周长:" << c2.Circumference() << endl;
Circle c3(c1);
cout << "c3面积:" << c3.Area() << endl;
cout << "c3周长:" << c3.Circumference() << endl;
return 0;
}
看了C++编程:编写一个关于圆形的...的网友还看了以下:
C++语言的几道题目-求助1、试举一个数据结构例子,叙述其逻辑结构、存储结构、运算三个方面的内容2 2020-05-17 …
SAT数学题求翻译及解题过程Rectangle ABCD lies in the xy-coord 2020-06-27 …
4、编写一个Java应用程序,包括类Rectangle、Volume和主类(名称为自己姓名的拼音) 2020-07-17 …
用java编写矩形类上机题1:定义一个名为Rectangle矩形类,其数据成员为矩形的左下角和右上 2020-07-17 …
c++编程题虚函数:编写一个程序,计算Circle(圆形)、Square(正方形)、Rectang 2020-08-04 …
python问题求解答classRectangle:"""Arectanglewithawidtha 2020-11-01 …
C++编程:编写一个关于圆形的程序.模仿编程:classRectangle{private:intl 2020-11-01 …
python3问题求完成classRectangle:"""Arectanglewithawidth 2020-11-01 …
求python3问题完成啊classRectangle:"""Arectanglewithawidt 2020-11-01 …
java中Rectangle类的setRect和setBounds有什么区别么?试了一下效果一样那不 2020-11-27 …