#include <iostream>
using namespace std;
// class Test
// {
// private:
// int &a;
// public:
// Test(int &b) : a(b)
// {
// cout << "constructor called" << endl;
// }
// ~Test()
// {
// cout << "destructor called" << endl;
// }
// void Modify(int value)
// {
// a = value;
// }
// };
// int main()
// {
// int b = 3;
// Test test(b);
// cout << "b=" << b << endl;
// test.Modify(4);
// cout << "b=" << b << endl;
// return 0;
// }
//https://www.jb51.net/article/232248.htm#_label0
// class Test
// {
// public:
// Test(int data = 100) :mb(data), ma(mb) { }
// void show() { cout << "ma:" << ma << "mb:" << mb << endl; }
// private:
// int ma;
// int mb;
// };
// int main()
// {
// Test t;
// t.show();
// return 0;
// }
class point
{
protected:
int m_x, m_y;
public:
point()
{
cout << this <<" point DEF CTOR called!!" << endl;
}
// point(int m = 0, int n = 0)
point(int m, int n)
{
m_x = m;
m_y = n;
cout << this <<" point PARAM CTOR called!!" << endl;
}
point(point &p)
{
m_x = p.GetX();
m_y = p.GetY();
cout << this <<" point copy CTOR called!" << endl;
}
~point()
{
cout << this << " point DTOR called" << endl;
}
int GetX()
{
return m_x;
}
int GetY()
{
return m_y;
}
};
class point3d
{
private:
point m_p;
int m_z;
public:
//函数形参是类的对象
point3d(point p, int k) //值传递,产生拷贝
{
m_p = p; //这里是对m_p的赋值
m_z = k;
}
// 相当于 point m_p(i,j)这样对m_p初始化
point3d(int i, int j, int k) :m_p(i, j)
{
m_z = k;
}
void Print()
{
cout << m_p.GetX() << ", " << m_p.GetY() << ", " << m_z << endl;
}
};
void test()
{
point tt;
}
int main()
{
point p2d(1, 2); //先定义一个2D坐标
point3d p3d(1, 2, 3); //不调用copy constructor
// point3d p3d(p2d, 3); //调用copy constructor ;point p = p2d
// test(); //调用完即刻执行析构函数
// point tt; //在最后命令结束后,main结束前,调用析构函数
p3d.Print();
}