⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6_7.cpp

📁 c++书籍的源代码
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
 const float PI=3.14159;
 class circle;//前向引用声明
 class point	//point类的定义
{
 public:	//外部接口
  point(int a=0, int b=0);	//构造函数
  ~point();
  void display(void);
  friend circle;//友元类
 private:	//私有数据
  int x;
  int y;
};
 //point类的实现
 point::point(int a,int b)
{
  x=a;
  y=b;
  cout<<"point构造函数被调用"<<endl;
}
point::~point()
{
  cout<<"point对象消亡"<<endl;
}
 void point::display(void)
{
  cout<<"x="<<x<<" y="<<y<<endl;
}
 class circle	//circle类的定义 
{
 public:	//外部接口
  circle(int a=0,int b=0,float c=1);	//构造函数
  ~circle();//析构函数
  void display(void);
  float get_cum();	//计算圆周长
  float get_area();	//计算圆面积
  friend void display(circle &s);//友元函数
 private:	//私有数据成员
    point p;  //对象成员 
	float r;
};
// 类的实现
 circle::circle(int a,int b,float c):p(a,b)
{ 
  r=c;
  cout<<"circle构造函数被调用"<<endl;
}	
 circle::~circle()
{
  cout<<"circle对象消亡"<<endl;
}
 void circle::display(void)
{
  cout<<"x="<<p.x<<" y="<<p.y<<endl;
  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(); 
 display(t);
 cout<<"周长:"<<setw(6)<<setprecision(3)<<t.get_cum()
	 <<" 面积:"<<setw(6)<<setprecision(3)<<t.get_area()<<endl;
 return(0);
}
 void display(circle &s)
{
  s.p.display();  
  cout<<"r="<<s.r<<endl; 
}

⌨️ 快捷键说明

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