您的位置:首页 > 编程语言 > C语言/C++

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

2015-03-07 09:45 615 查看
贺老师的教学链接 本课讲解

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: