C + + inheritance detailed and instance code _c language

Source: Internet
Author: User
Tags inheritance sprintf

C + + inheritance can be a single inheritance or multiple inheritance, and each inherited connection can be public,protected,private or virtual or non-virtual. 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, such as the following:

1 class Base
2 {... }
3 Class Derived:public Base
4 {... }

If you write this, 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 very important. Then the function parameter is base type applicable to derived, and the formal parameter is derived not applicable to base. Here is the validation code, a function with a parameter of base, the incoming derived should be executed successfully, instead, a function with a parameter of derived

#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 Basenam
  E
  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::s
    Tring 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 results of the operation are:

Base:basename is test, Basedata is 10
Base:basename is Btest, Basedata is 5

The following changes the code to change the function parameters into 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), compiler Error

1 derived_class.cpp:In function ' int main (int, char**) ':
2 derived_class.cpp:84:error:invalid initialization of RE Ference of type ' const derived& ' from expression of type ' base '
3 derived_class.cpp:70:error:in passing Argumen T 2 of ' void Show2 (std::string&, const derived&) '

2nd, the verification of various forms of inheritance, first give the table

Inheritance mode \ member Type Public Protected Private
Public Public Protected Cannot inherit
Protected Protected Protected Cannot inherit
Private Private Private Cannot inherit

To explain here, only the members of the base class are expressed, and after being public,protected,private in three ways, the members in the original base class are public,protectedc,private in the inheriting class as the contents of the table.

Class Base
{public
  :
    std::string testpublic ()
    {return
      std::string (' This was public base ');
    }
  Protected:
    std::string testprotected ()
    {return
      std::string (' is protected base ');
    }
  Private:
    std::string testprivate ()
    {return
      std::string (' 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; 
}

Report the following error, stating that Testprivate () is not derived private function but base's private function

Derived11.cpp:16:error: ' std::string base::testprivate () ' is private Derived11.cpp:36:error:within the this context

This verifies that private type members cannot be inherited (public,private,protected) Note: private,protected Omit proof

As long as the validation testprotected can be inherited by the third-tier inheriting class, but cannot be called directly by the third-tier class, the inheritance type is protected after public inheritance, and the base class is a public type member and can be inherited and directly invoked.

#include <iostream> #include <string> class Base {public:std::string Testpublic () {return
    Std::string ("This are public base");
    } protected:std::string testprotected () {return std::string (' is protected base ');
    } private:std::string Testprivate () {return std::string (' 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 () + = "I
    n 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; }

Here the server complains

Derived12.cpp:13:error: ' std::string base::testprotected () ' is protected
Derived12.cpp:62:error:within this Context

This verifies that one is public and that a protected,protected is not directly invoked, but is inherited and can be invoked by a public member.
As has been shown below, the detailed steps are omitted if you are interested in this part of the validation, you can look at the following code.

#include <iostream> #include <string> class Base {public:std::string Testpublic () {return
    Std::string ("This are public base");
    } protected:std::string testprotected () {return std::string (' is protected base ');
    } private:std::string Testprivate () {return std::string (' 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 member is 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 Deriv
    Ed ";
    } 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;     The user is inherited and cannot use//cout<<dpub.testprivate () <<std::endl;
  Base classes are private functions std::cout<<dpub.testpubpublic () <<std::endl; Std::cout&lT;<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; }

The above is to C + + J Inheritance of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.