Interesting C + + topics

Source: Internet
Author: User

1#include <iostream>2#include <stdlib.h>3 using namespacestd;4 5 classA6 { 7  public: 8     int_a;9 A ()Ten     {  one_a =1;  a     }  -     voidPrint () -     {  theprintf"%d\n", _a);  -     }  - };  -  + classB: publicA - {  +  public:  a     int_a; at B () -     {  -_a =2;  -     }  - };  -  in intMain () - {  to b b; + B.print (); -printf"%d\n", b._a);  the     return 0;  *}

The result of the Operation Is:

Subclasses overwrite the variables of the parent class

1#include <iostream>2#include <stdlib.h>3 using namespacestd;4 5 class Base 6 { 7 Private: 8     intm_i;9     intm_j;Ten  public:  one     Base(inti): m_j (i), m_i (m_j) {} a     Base(): M_j (0), m_i (m_j) {} -     intGet_i () {returnm_i;}  -     intGet_j () {returnm_j;}  the };  -  - intMain () - {  +     BaseObj98);  -Cout<<obj.get_i () <<Endl +<<obj.get_j () <<endl; a  at     return 0;  -}

The result of the Operation Is:

The order in which the constructor initializes the variables is performed in the order in which the member variables are Declared.

Interesting C + + topics

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.