C++私人資料成員提取到類外的方法總結__C++

來源:互聯網
上載者:User

今天接觸到了C++的引用使用方法,C++中的資料成員(也可以成長為對象的屬性,當然成員函數就叫做對屬性施加的行為)分為public,protected和private三種咯。private資料成員是不能被類外的函數進行操作的(友元除外),今把我做學到和接觸的類的私人資料成員提取的類外的方法進行總結,由於水平有限,熱烈歡迎各位朋友補充:

#include <iostream>using namespace std;class Test{public:    void set(int,int);    void display();    //將對象私人資料成員取出方式一,引用    void getxy(int &a,int &b)    {        a=x;        b=y;    }//將對象私人資料成員取出方式一,指標    void getxy1(int *a,int *b)    {        *a=x;        *b=y;    }    //將對象私人資料成員取出方式一,return    int getx()    {return x;    }    int gety()    {return y;    }private:    int x;    int y;};void Test:: set(int a,int b){    x=a;    y=b;}void Test:: display(){    cout<<"x="<<x<<endl;    cout<<"y="<<y<<endl;}int main(){   Test test1;   test1.set(1,2);   test1.display();   int a,b;   test1.getxy(a,b);   cout<<"a="<<a<<'\t'<<"b="<<b<<endl;   int c,d;   test1.getxy1(&c,&d);   cout<<"c="<<a<<'\t'<<"d="<<b<<endl;int x,y;   cout<<"x="<<test1.getx()<<'\t'<<"y="<<test1.gety()<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.