类实例介绍

访问对象的共有成员和用指针访问对象成员
#include<iostream>
using namespace std;
class Tclass        //定义Tclass类;
{
    public:
    int x,y;
    void print()
    {
        cout<<x<<","<<y<<endl;
    }
};
int add(Tclass *ptf)
{
    return (ptf->x+ptf->y);     //指针访问形式;
}
int main()
{
    Tclass test,*pt=&test;        //说明一个对象test和对象指针pt;
    pt->x=100;                    //通过指针访问数据成员;
    pt->y=200;
    pt->print();                 //通过指针访问成员函数;
    test.x=150;                   
    test.y=450;                  //访问公有段数据成员;
    test.print();
    cout<<"x+y="<<add(&test)<<endl;     //把对象地址传给指针参数;
}


猜你喜欢

转载自blog.csdn.net/Flora_SM/article/details/80032138