C++语言基础 例程 对象成员的引用

贺老师的教学链接  本课讲解


通过对象名和成员运算符访问对象中的成员

#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1;        
    t1.set_time( );    
    t1.show_time( );  
    Time t2;      
    t2.set_time( );  
    t2.show_time( );  
    return 0;
}

void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}


通过指向对象的指针访问对象中的成员
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1, *p;  
    p=&t1;
    t1.set_time( ); 
    (*p).show_time( );   
    p->show_time( );  
    return 0;
}

void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}


通过对象的引用变量访问对象中的成员
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );   
    void show_time( );  
private:           
    int hour;
    int minute;
    int sec;
};
int main( )
{
    Time t1;  
    Time &t2=t1;
    t1.set_time( );   
    t2.show_time( );
    return 0;
}


void Time::set_time( ) 
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}


void Time::show_time( )      
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}

访问私有数据成员的常用方法
(1)通过公共函数访问私有成员
#include <iostream>
using namespace std;
class Test
{
private:
    int x, y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void printXY(void)
    {
        cout<<"x="<<x<<'\t'<<"y="<<y<<endl;
    }
} ;
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    p1.printXY( );
    return 0;
}


(2)(惯例)利用set和get函数访问私有数据成员
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
 public:
    void setX(int a) {x=a;}
    void setY(int b) {y=b;}
    int getX(void) {return x;} //返回x值
    int getY(void) {return y;} //返回y值
};
int main()
{
    Test p1,p2;
    p1.setX(3);p1.setY(5);
    int a,b;
    a=p1.getX( ); b=p1.getY(); 
    cout<<a<<'\t'<<b<<endl; 
}


利用指针将私有数据成员的值提取到类外
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void getXY(int *px, int *py)
    {
        *px=x;    //提取x,y值
        *py=y;
    }
};
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    p1.getXY(&a,&b);  //将 a=x, b=y
    cout<<a<<'\t'<<b<<endl;
    return 0;
}


利用引用将私有数据成员的值提取到类外
#include <iostream>
using namespace std;
class Test
{
private:
    int x,y;
public:
    void setX(int a)
    {
        x=a;
    }
    void setY(int b)
    {
        y=b;
    }
    void getXY(int &px, int &py) //引用
    {
        px=x;    //提取x,y值
        py=y;
    }
};
int main()
{
    Test p1;
    p1.setX(3);
    p1.setY(5);
    int a,b;
    p1.getXY(a, b); //将 a=x, b=y
    cout<<a<<'\t'<<b<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值