524.cpp

来自「c++课程习题集的源代码」· C++ 代码 · 共 57 行

CPP
57
字号
#include<iostream.h>
#include<iomanip.h>
 const float PI=3.14159;	
 class circle	//circle类的定义 
{
 public:	//外部接口
  circle(float x=1);	//构造函数
  circle(circle &p);	//构造函数
  ~circle();//析构函数
  void display(void);
  void change(float s);//缩放
 private:	//私有数据成员
  float *r;
};
// 类的实现
 circle::circle(float x)
{ 
 r=new float(x); 
 cout<<"circle构造函数被调用"<<endl;
}	
 circle::circle(circle &s)
{
 r=new float; 
 *r=*(s.r); 
 cout<<"circle拷贝构造函数被调用"<<endl;
}
 circle::~circle()
{
 delete r;
 cout<<"circle析构函数被调用"<<endl;
}
 void circle::display(void)
{
 cout<<"半径:"<<*r<<endl;
}
 void circle::change(float s)
{
 *r=*r*s;
}
//类的应用
 int main ()
{
 float x;
 cout<<"请输入圆的半径:"<<endl;	//提示用户输入半径
 cin>>x;
 circle t(x),*p;  	//定义circle 对象
 p=new circle(t);
 t.display();
 p->display();
 t.change(2); 
 p->change(0.5); 
 t.display();
 p->display();
 delete p;
 return(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?