A thorough study of the definition and access methods of the parent-class member variables and subclass member variables in C + + __c++

Source: Internet
Author: User

When doing engineering with C + +, inheritance is one of the most common features of C + + because it makes engineering reusability very high. But now there is a problem, for example, when we use the MFC library, we tend to inherit a class inside the library, but we do not look at the implementation code inside the parent class, so we can define the member variable in the same way as the member variables inside the parent class. But what happens is that we don't care much about it, because a lot of people think it doesn't matter, they just want to define it, and they don't care about its principles and fundamental things. Today it is said that such problems will have an impact on the program. First look at a piece of code.

#include <iostream>

using namespace std;

Defines a parent class with the I, J two member variables (protected indicates that the member variable can only be accessed in the parent subclass) class
parent
{
 protected:
 int i;
 int J;
};

Defines a subclass of child, which has inherited the parent class, which has a member variable like the parent class 
Child:public parent
{
protected:
 int I;
  //f () function is to print I, J
 void F ()
 {
  cout << "parent::i =" << parent::i << endl;
  
   cout << "child::i =" << child::i << Endl;
  cout << "parent::j =" << parent::j << Endl;
  
 }

Public:
 The constructor of the//subclass child 
 (int i, int j)
 {
  parent::i = i;       The I assignment in the parent class is the parameter I

  child::i = j;        The I assignment in the handle class is the parameter I

  parent::j = i + j;   The J assignment in the parent class is the parameter J
  
  F ();                 Bar with the F () function
 }
}; 

int main ()
{
 //Defines an object of a child type, with parameters 1, 2 child  
 C1 (1, 3);

 return 0;
}


  


The above program prints the result as

PARENT::I = 1
CHILD::I = 3
Parent::j = 4

There is no doubt that we can use the "::" scope to identify the same member variables in the class and the parent class, but often when we access the member variables in the class, we basically do not use the scope of the resolution, what will happen when not in use. Now we're going to look at a piece of code

#include <iostream>


using namespace std;


Defines a
class Parent
{
protected:
int i;
int J;
};


Class Child:public Parent
{
protected:
int i;
Int J;
void f ()
{
cout << "parent::i =" << parent::i << Endl;
cout << "parent::j =" << parent::j << Endl;
cout << "child::i =" << child::i << Endl;
cout << "child::j =" << child::j << Endl;
}


Public: Child
(int a, int b)
{
i = A;
j = B;

f ();
}
; 


int main ()
{child
C1 (1, 3);
return 0;
}


 

 

Run result is

 

parent::i = -858993460
parent::j = -858993460
child::i = 1
child::j = 3

So by default, the compiler uses the variable defined in this class without the scope resolution

For example: i = A;j = b;

The assignment here represents the

Child::i and

Child::j. If you really want to manipulate the I and J of the parent class, then you need the scope resolution "::" to determine. So when we define a variable, it doesn't matter if it has the same name as the parent, and it doesn't affect the program because the member variables we normally define in the class are meant to be used in this class.

Summary: When a subclass uses this variable as a member variable with the same name as the parent class, the subclass's variables are used by default when the scope resolution is not used. You can understand this: when you access the member variables in the class, first see if they have, they have direct use, if they do not, then see Dad (Parent) There is no, the parent class if there is a parent class (if this variable in the parent class if protected or public, If the member variable in the parent class is private, the member variable is inherited to the subclass, and the subclass is still inaccessible, the parent class will complain if there is no program.


So when we are programming, if there are two identical member variables in the subclass and the parent class, to access the variables in the parent class, you must use the scope resolution and if you do not want to use the scope resolution, let the two names not conflict when you design the class.











Related Article

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.