📄 4_3.cpp
字号:
#include<iostream.h>
#include<iomanip.h>
const float PI=3.14159;
class point //point类的定义
{
public: //外部接口
point(int a=0, int b=0); //构造函数
point(point &p); //拷贝构造函数
~point();
void display(void);
void move(int xx,int yy);
private: //私有数据
int x;
int y;
};
//类的实现
point::point(int a,int b)
{
x=a;
y=b;
cout<<"point构造函数被调用"<<endl;
}
point::~point()
{
cout<<"point对象消亡"<<endl;
}
point::point(point &p)
{
x=p.x;
y=p.y;
cout<<"point拷贝构造函数被调用"<<endl;
}
void point::display(void)
{
cout<<"x="<<x<<" y="<<y<<endl;
}
void point::move(int xx,int yy)
{
x+=xx;
y+=yy;
}
class circle //circle类的定义
{
public: //外部接口
circle(int a=0,int b=0,float c=1); //构造函数
circle(circle &s); //拷贝构造函数
~circle();//析构函数
void display(void);
float get_cum(); //计算圆周长
float get_area(); //计算圆面积
private: //私有数据成员
point p;
float r;
};
// 类的实现
circle::circle(int a,int b,float c):p(a,b)
{
if(c<=0)
r=1;
else
r=c;
cout<<"circle构造函数被调用"<<endl;
}
circle::circle(circle &s):p(s.p)
{
r=s.r;
cout<<"circle拷贝构造函数被调用"<<endl;
}
circle::~circle()
{
cout<<"circle对象消亡"<<endl;
}
void circle::display(void)
{
p.display();
cout<<"r="<<r<<endl;
}
float circle::get_cum() // 计算圆的周长
{
return (2*PI*r);
}
float circle::get_area()// 计算圆的面积
{
return (PI*r*r);
}
//类的应用
int main ()
{
int a,b;
float c;
cout<<"请输入圆心的坐标:"; //提示用户输入圆心坐标
cin>>a>>b;
cout<<"请输入圆的半径:"; //提示用户输入半径
cin>>c;
circle t(a,b,c); //定义circle 对象
t.display();
cout<<"周长:"<<setw(6)<<setprecision(3)<<t.get_cum()
<<" 面积:"<<setw(6)<<setprecision(3)<<t.get_area()<<endl;
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -