c/c++ Inheritance and Polymorphism Conversion Rules from Subclasses to Parents

Keywords: C++ github

Question 1: Subclass B can inherit parent A in three ways (public, protected, private). In which way can user code transform object of subclass B into object of parent A?

Only when subclass B inherits parent class A in public mode, can the object of subclass B be converted into parent class A in user code.

The reasons are as follows:

  • The class Pro_derv and the class Pri_derv of the following examples inherit Base in the way of protected and private respectively. So in the class Pro_derv and the class Pri_derv objects, the members of the original Base class pub_mem() are not attributes of public, but attributes of protected and private respectively. The members of the protected and private attributes are not accessible to user programs.
  • When Pro_derv and Pri_derv-like objects are created, members pub_mem() are already attributes of protected and private, respectively. So user programs like Pro_derv and Pri_derv cannot access the member pub_mem() of protected or private attributes.
  • When converting objects of class Pro_derv and Pri_derv into objects of parent Base class, from the perspective of class Base, pub_mem() is a member of public, so user programs of class Base can access pub_mem(), but from the perspective of class Pro_derv and class Pri_derv, pub_mem() is no longer a member of public, so user programs of class Pro_derv and class Pri derv can not be used. Access member pub_mem(). At this point, the compiler is confused with this contradiction, so the compiler simply won't let you compile through...

class Base{
public:
  void pub_mem();
protected:
  int prot_mem;
private:
  char pri_mem;
};

class Pub_derv : public Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pro_derv : protected Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pri_derv : private Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
};


int main(){
  Pub_derv pub;
  Pro_derv pro;
  Pri_derv pri;
  
  Base& b1 = pub;
  Base& b2 = pro;//error
  Base& b3 = pri;//error
}

github

Question 2: Subclass B can inherit parent A in three ways (public, protected, private). Which way can we inherit subclass B's member function and friend (non-user code) of subclass B to convert the object of subclass B into the object of parent A?

No matter how the subclass B inherits the parent class A, the object of subclass B can be transformed into the object of the parent class A in the member function of subclass B and the friend element of subclass B (non-user code).

The reasons are as follows:

  • No matter how subclass B inherits parent class A, in the member function of subclass B and the friend (non-user code) of subclass B, the attributes of the members of parent class A do not change, so after transformation, the public members of parent class A can be accessed with the object of parent class A.
class Base{
public:
  void pub_mem();
protected:
  int prot_mem;
private:
  char pri_mem;
};

class Pub_derv : public Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pro_derv : protected Base{
  friend void pro_fri(Pro_derv&);
  int f(){
    Base& b = *this;//No matter how the subclass B inherits the parent class A, the object of subclass B can be transformed into the object of the parent class A in the member function of subclass B and the friend element (non-user code) of subclass B.
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pri_derv : private Base{
  friend void pri_fri(Pri_derv&);
  int f(){
    Base& b = *this;//No matter how the subclass B inherits the parent class A, the object of subclass B can be transformed into the object of the parent class A in the member function of subclass B and the friend element (non-user code) of subclass B.
    pub_mem();
    return prot_mem;
  }
};

void pro_fri(Pro_derv& pro){
  Base& b = pro;//No matter how the subclass B inherits the parent class A, the object of subclass B can be transformed into the object of the parent class A in the member function of subclass B and the friend element (non-user code) of subclass B.
}

void pri_fri(Pri_derv& pro){
  Base& b = pro;//No matter how the subclass B inherits the parent class A, the object of subclass B can be transformed into the object of the parent class A in the member function of subclass B and the friend element (non-user code) of subclass B.
}
int main(){
  Pub_derv pub;
  Pro_derv pro;
  Pri_derv pri;
  
  Base& b1 = pub;
  //Base& b2 = pro;
  //Base& b3 = pri;
}

github

Question 3: Subclass B can inherit parent class A in three ways (public, protected, private). Which way can we inherit subclass B's member functions of subclass C and its friend elements of subclass B's subclass C to convert the object of subclass B into the object of parent class A?

Only when subclass B inherits parent class A in public or protected mode, can the object of subclass B be converted into the object of parent class A in the member function of subclass C of subclass B and the friend element of subclass C of subclass B.

class Base{
public:
  void pub_mem();
protected:
  int prot_mem;
private:
  char pri_mem;
};

class Pub_derv : public Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pro_derv : protected Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pri_derv : private Base{
  int f(){
    pub_mem();
    return prot_mem;
  }
};


class Pub_Pub_derv : private Pub_derv{
  friend void pubpubfri(Pub_Pub_derv&);
  int f(){
    Base& b = *this;//Only when subclass B inherits parent class A in public or protected way, can the object of subclass B be transformed into the object of parent class A in the member function of subclass C of subclass B and the friend element of subclass C of subclass B.
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pro_Pro_derv : private Pro_derv{
  friend void proprofri(Pro_derv&);
  int f(){
    Base& b = *this;//Only when subclass B inherits parent class A in public or protected way, can the object of subclass B be transformed into the object of parent class A in the member function of subclass C of subclass B and the friend element of subclass C of subclass B.
    pub_mem();
    return prot_mem;
  }
  //char g(){return pri_mem;}//error
};

class Pri_Pri_derv : private Pri_derv{
  friend void priprifri(Pri_derv&);
  int f(){
    //Base* B = this; //error, only when subclass B inherits parent A in public or protected mode, can the object of subclass B be converted into the object of parent A only in the member function of subclass C of subclass B and the friend of subclass C of subclass B.
    //pub_mem();
    //return prot_mem;
  }
};

void pubpubfri(Pub_derv& pd){
  Base& b = pd;//Only when subclass B inherits parent class A in public or protected way, can the object of subclass B be transformed into the object of parent class A in the member function of subclass C of subclass B and the friend element of subclass C of subclass B.
}
void proprofri(Pro_derv& pd){
  Base& b = pd;//Only when subclass B inherits parent class A in public or protected way, can the object of subclass B be transformed into the object of parent class A in the member function of subclass C of subclass B and the friend element of subclass C of subclass B.
}
void priprifri(Pri_derv& pd){
  //Base &b = pd; //error, only when subclass B inherits parent class A in public or protected way, can the object of subclass B be converted into the object of parent class A only in the member function of subclass C of subclass B and the friend of subclass C of subclass B.
}
int main(){
  Pub_derv pub;
  Pro_derv pro;
  Pri_derv pri;
  
  Base& b1 = pub;
  //Base& b2 = pro;
  //Base& b3 = pri;
}

github

c/c++ Learning Mutual Assistance QQ Group: 877684253

I am writing to xiaoshitou 5854

Posted by bretticus on Wed, 08 May 2019 18:03:38 -0700