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