303a.cpp

来自「C++实训教程」· C++ 代码 · 共 36 行

CPP
36
字号
/*
	303a.cpp
	Written By S.Y.Feng
	demo 构造函数和析构函数
*/
#include <iostream.h>

class Point // define Point class
{
	int x;   // x and y are private by default
	int y;
  public:
   Point(){ x = 0; y = 0 ;}
   Point(int a,int b=0)  { x = a; y= b; }
	int GetX() {return x;}// public member functions
	int GetY() {return y;}
    ~Point() {cout << "Destruct obj.\n" ;}
};

int main()
{
	Point p1(1,11),p2(200,-200);  //两个对象
	cout << "对象p1的 x ="<< p1.GetX()
		 << "  y = "      << p1.GetY()	<< endl;
	cout << "对象p2的 : "
		<< "x is "   << p2.GetX()
		<< "  y is " << p2.GetY() 	<< endl;
	return 0;
}
/* ok
对象p1的 x =1  y = 11
对象p2的 : x is 200  y is -200
Destruct obj.
Destruct obj.
*/

⌨️ 快捷键说明

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