C + + Inheritance introduction _c language

Source: Internet
Author: User
Tags sprintf

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:

Copy Code code as follows:

Class Base
{... }
Class Derived:public Base
{... }

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
Copy Code code as follows:

#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 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

Copy Code code as follows:

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
Copy Code code as follows:

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 ' bas E
Derived_class.cpp:70:error:in passing argument 2 of ' void Show2 (std::string&, const derived&) ' 2nd validates all 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.

Copy Code code as follows:

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";
}
};
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 this context verifies that private type members cannot be inherited (public,private,protected) Note: private, Protected omit not to do 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.
Copy Code code as follows:

#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 () + = "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;
}


Here the server complains

Derived12.cpp:13:error: ' std::string base::testprotected () ' is protected
Derived12.cpp:62:error:within This context 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.

Copy Code code as follows:

#include <iostream>
2 #include <string>
3 class Base
4 {
5 Public:
6 std::string testpublic ()
7 {
8 return std::string ("It is public base");
9}
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 members have not been 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; The user is inherited and cannot be used
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 be 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;

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.