c++快速入门 对象指针,对象成员指针

#include <iostream>
using namespace std;

//对象成员指针就是一个类里面的类是一个指针
//对象指针:类指针,创建的是一个类类型的指针
class coordinate
{
public:
    coordinate(int xx,int yy);
    ~coordinate();
    int x;
    int y;
};

coordinate::coordinate(int xx,int yy)
{
    x = xx;
    y = yy;
    cout<<"coordinate()"<<x<<' '<<y<<endl;
}

coordinate::~coordinate()
{
    cout<< "~coordinate() "<<x<<' '<< y <<endl;
}


class line
{
public:
    line(int x1, int y1,int x2,int y2);
    ~line();

private:
    coordinate *a = NULL;//对象成员指针
    coordinate *b = NULL;//对象成员指针
};

line::line(int x1, int y1,int x2,int y2)
{
    a = new coordinate(x1,y1);
    b = new coordinate(x2,y2);
    cout<<"line()"<<endl;
}

line::~line()
{
    delete a;
    a = NULL;
    delete b;
    b = NULL;
    cout<<"~line()"<<endl;
}

int main(void)
{
    line x(1,2,3,4);//"释放顺序与对象成员不同"

    /*
    coordinate *p = new coordinate(4,5); //对象指针
    cout<<p->x<<' '<<(*p).y<<endl;
    delete p;
    */

    cout<<sizeof(line)<<endl;//"line的大小是两个指针的大小"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值