Public, private, protected

Source: Internet
Author: User

Class Parent <br/>{< br/> Public: <br/> parent (INT Var =-1) <br/>{< br/> m_npub = var; <br/> m_nptd = var; <br/> m_nprt = var; <br/>}< br/> Public: <br/> int m_npub; <br/> protected: <br/> int m_nptd; <br/> PRIVATE: <br/> int m_nprt; <br/> }; </P> <p> // public inheritance <br/> class Child1: Public parent <br/>{< br/> Public: <br/> int getpub () {return m_npub ;}; <br/> int getptd () {return m_nptd ;}; <br/> // int getprt () {return m_nprt ;}; <br/>}; <br/> // protection inheritance <br/> class child2: protected parent <br/>{< br/> public: <br/> int getpub () {return m_npub ;}; <br/> int getptd () {return m_nptd ;}; <br/> // int getprt () {return m_nprt ;}; <br/> protected: <br/> PRIVATE: <br/>}; <br/> // Private inheritance <br/> class Child3: private parent <br/>{< br/> Public: <br/> int getpub () {return m_npub ;}; <br/> int getptd () {return m_nptd ;}; <br/> // int getprt () {return m_nprt ;}; <br/> protected: <br/> PRIVATE: <br/> }; <br/> int main () <br/> {<br/>/* B temp = play (5); */<br/> Child1 CD1; <br/> child2 CD2; <br/> Child3 CD3; </P> <p> // Public <br/> cd1.m _ npub = 10; <br/> // cd1.m _ nptd = 5; <br/> // cd1.m _ nprt = 1; </P> <p> cd1.getpub (); <br/> cd1.getptd (); </P> <p> // Protection <br/> // cd2.m _ npub = 4; <br/> // cd2.m _ nptd = 3; <br/> cd2.getpub (); <br/> cd2.getptd (); </P> <p> // Private inheritance <br/> // cd3.m _ npub = 1; <br/> // cd3.m _ nptd = 2; <br/> // cd3.m _ nprt = 5; <br/> cd3.getptd (); <br/> cd3.getpub (); </P> <p> return 0; <br/>}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.