Introduction to C++ inheritance

  • 2020-04-01 21:28:32
  • OfStack

Then the individual member function options can be virtual or non-virtual or pure virtual. This article only makes some key points of verification.

Public inheritance, for example:


 class base
  { ... } 
 class derived:public base
  { ... } 

If you write it this way, the compiler will understand that an object of type derived is also an object of type base, but an object of type base is not an object of type derived. This is important. So the type of function parameter base is suitable for derived, and the type parameter base is not suitable for base. The following is the verification code. A function with an argument of base should be passed into the derived function successfully, while a function with an argument of base should be passed into the derived function successfully

 #include <iostream>
  #include <stdio.h>

  class base
  {
      public:
      base()
      :baseName(""),baseData(0)
      {}

     base(std::string bn,int bd)
     :baseName(bn),baseData(bd)
     {}

     std::string getBaseName() const
     {
         return baseName;
     }

     int getBaseData()const
     {
         return baseData;
     }

     private:
         std::string baseName;
         int baseData;
 };

 class derived:public base
 {
     public:
         derived():base(),derivedName("")
         {}
         derived(std::string bn,int bd,std::string dn)
         :base(bn,bd),derivedName(dn)
         {}
         std::string getDerivedName() const
         {
             return derivedName;
         }
     private:
         std::string derivedName;
 };

 void show(std::string& info,const base& b)
 {
     info.append("Name is ");
     info.append(b.getBaseName());
     info.append(", baseData is ");
     char buffer[10];
     sprintf(buffer,"%d",b.getBaseData());
         info.append(buffer);
 }

 int main(int argc,char* argv[])
 {
     base b("test",10);
     std::string s;
     show(s,b);
     std::cout<<s<<std::endl;
     derived d("btest",5,"dtest");
     std::string ss;
     show(ss,d);
     std::cout<<ss<<std::endl;
     return 0;
 }

The running result is:

Base :baseName is test, baseData is 10
Base :baseName is btest, baseData is 5

Now change the code to change the function parameter to derived


void show2(std::string& info,const derived& d)
{
    info.append("Name is ");
    info.append(d.getBaseName());
    info.append(", baseData is ");
    char buffer[10];
    sprintf(buffer,"%d",d.getBaseData());
    info.append(buffer);
}

Call show (ss, d); The compiler reported an error

 derived_class.cpp: In function `int main(int, char**)':
 derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base'
 derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)' The second point validates the various forms of inheritance, starting with a table 

Inheritance mode \ member type public protected private public public protected Unable to inherit protected protected protected Unable to inherit private private private Unable to inherit

Here to explain, here only express base class member, is public, protected, private inheritance, three ways for public, in the original base class protectedc, private member in a derived class type for the content on the form


class base
{
    public:
        std::string testPublic()
        {
            return std::string("this is public base");
        }
    protected:
        std::string testProtected()
       {
           return std::string("this is protected base");
       }
   private:
       std::string testPrivate()
       {
           return std::string("this is private base");
       }
};
class derivedPublic:public base
{
   public:
       std::string testPubPublic()
       {
           return testPublic()+= "in derived";
       }
       std::string testProPublic()
       {    
           return testProtected()+= "in derived";
       }
       std::string testPriPublic()                   
       {    
           return testPrivate()+= "in derived";
       }
};
int main(int argc,char* argv[])
 { 
   derivedPublic dpub;
   std::cout << dpub.testPublic() << std::endl; 
 } 

The following error indicates that testPrivate() is not a derived private function but a private function of base
Derived11.cpp :16: error: 'STD ::string base::testPrivate()' is private
Derived11.cpp :36: error: within this context this way verifies that members of the private type cannot be inherited (public,private,protected)
Now, as long as we verify that testProtected can be inherited by the third-level class, but cannot be directly called by the third-level class, then we can say that it is public inheritance and then the inheritance type is protected, while the base class is a member of the public type, then it can be inherited and called directly.

 #include <iostream>
 #include <string>

 class base
 {
     public:
         std::string testPublic()
         {
             return std::string("this is public base");
        }
    protected:
        std::string testProtected()
        {
            return std::string("this is protected base");
        }
    private:
        std::string testPrivate()
        {
            return std::string("this is private base");
        }
};
class derivedPublic:public base
{
    public:
        std::string testPubPublic()
        {
            return testPublic()+= "in derived";
        }

        std::string testProPublic()
        {    
            return testProtected()+= "in derived";
        }

//        std::string testPriPublic()                   
//        {    
//            return testPrivate()+= "in derived";
//        }
};
class deepDerived:public derivedPublic
{
    public:
        std::string deepProtected()
        {
            return testProtected() +="in deep";
        }

        std::string deepPublic()
        {
            return testPublic() +="indeep";
        }
};
int main(int argc,char* argv[])
{
    derivedPublic dpub;
    std::cout << dpub.testProtected() << std::endl; 
    deepDerived deepdpub;
    std::cout<<deepdpub.testPublic() <<std::endl;
    std::cout<<deepdpub.testProtected() <<std::endl;
    std::cout<<deepdpub.deepProtected() <<std::endl;
    std::cout<<deepdpub.deepPublic() <<std::endl;
}

The server is reporting an error

Derived12.cpp :13: error: 'STD ::string base::testProtected()' is protected
Derived12.cpp: 62:error: within this context thus validates that one is public and the other is protected. Protected cannot be called directly, but can be called by public members if it is inherited.
If you are interested in validating this section, you can look at the following code.


 #include <iostream>
 2 #include <string>
 3 class base
 4 {
 5     public:
 6         std::string testPublic()
 7         {
 8             return std::string("this is public base");
 9         }
     protected:
         std::string testProtected()
         {
             return std::string("this is protected base");
         }
     private:
         std::string testPrivate()
         {
             return std::string("this is private base");
         }
 };

 class derivedPublic:public base
 {
     public:
         std::string testPubPublic()
         {
             return testPublic()+= "in derived";
         }

         std::string testProPublic()
         {    
             return testProtected()+= "in derived";
         }

 //        std::string testPriPublic()                   // Private members are not inherited 
 //        {    
 //            return testPrivate()+= "in derived";
 //        }
 };

 class deepDerived:public derivedPublic
 {
     public:
         std::string test()
         {
             return testPublic() +="in 3";
         }
 };

 class derivedProtected:protected base
 {
     public:
         std::string testPubProtected()
         {
             return testPublic()+= "in derived";
         }

         std::string testProProtected()
         {    
             return testProtected()+= "in derived";
         }
 };

 class deepDerived2:public derivedProtected
 {
     public:
         std::string test()
         {
             return testPublic() +="in 3";
         }
 };

 class derivedPrivate:private base
 {
     public:
         std::string testPubPirvate()
         {
             return testPublic()+= "in derived";
         }

         std::string testProPrivate()
         {    
             return testProtected()+= "in derived";
         }

 };

 //class deepDerived3:public derivedPrivate
 //{
 //    public:
 //        std::string test()
 //        {
 //            return testPublic() +="in 3";
 //        }
 //};

 int main(int argc,char* argv[])
 {
     derivedPublic dpub;
    //derivedProtected dpro;
    //derivedPrivate dpri;
    std::cout<<dpub.testPublic()<<std::endl;       //
    //std::cout<<dpub.testProtected()<<std::endl;    // Users inherited are also unusable 
    //cout<<dpub.testPrivate()<<std::endl;         // Base classes are private functions 
    std::cout<<dpub.testPubPublic()<<std::endl;
    std::cout<<dpub.testProPublic()<<std::endl;
    //std::cout<<dpub.testPriPrivate()<<std::endl; // Not inherited 

    deepDerived dd;
    std::cout<<dd.test()<<std::endl;

    derivedProtected dpro;
    //std::cout<<dpro.testPublic()<<std::endl;        // become protected type 
    std::cout<<dpro.testPubProtected()<<std::endl;
    std::cout<<dpro.testProProtected()<<std::endl;

    deepDerived2 dd2;
    std::cout<<dd2.test()<<std::endl;

    derivedPrivate dpri;
    std::cout<<dpri.testPubPirvate()<<std::endl;
    std::cout<<dpri.testProPrivate()<<std::endl;

//    deepDerived3 dd3;
//    std::cout<<dd3.test()<<std::endl;
 } 


Related articles: