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

📄 48.cpp

📁 c++课程习题集的源代码
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
 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 rect	//矩形类的定义 
{
 public: //外部接口
  rect(int x,int y,float d1,float d2);//构造函数
  rect(rect &s);//构造函数
  ~rect();//析构函数
  void display(void);
  void move(int d1,int d2);	//移动
  void change(float s1,float s2);//缩放
 private:	//私有数据成员
  point p;   
  float a;
  float b;
};
// 类的实现
 rect::rect(int x,int y,float d1,float d2):p(x,y)
{ 
 a=d1;
 b=d2;
 cout<<"rect构造函数被调用"<<endl;
}	
 rect::rect(rect &s):p(s.p)
{
 a=s.a;
 b=s.b;
 cout<<"rect拷贝构造函数被调用"<<endl;
}
 rect::~rect()
{
 cout<<"rect对象消亡"<<endl;
}
 void rect::display(void)
{
 p.display();
 cout<<"a="<<a<<",b="<<b<<endl;
}
 void rect::move(int d1,int d2)//移动
{
 p.move(d1,d2);
}
 void rect::change(float s1,float s2)//缩放 
{
 a*=s1;
 b*=s2;
}
//类的应用
 int main ()
{
 int a,b;
 float c,d;
 cout<<"请输入矩形左上角顶点的坐标:"<<endl;
 cin>>a>>b;
 cout<<"请输入矩形的长和宽:"<<endl;
 cin>>c>>d;
 rect t(a,b,c,d);  	//定义rect 对象
 t.display(); 
 t.move(1,1);
 t.display(); 
 t.change(2,3);
 t.display(); 
 return(0);
}

⌨️ 快捷键说明

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