Then, each member function option can be virtual, non-virtual, or pure virtual. This article only verifies some key points.
Public inheritance, for example:
Copy codeThe Code is as follows: class base
{...}
Class derived: public base
{...}
In this case, the compiler will understand that objects of the type derived are also objects of the Type base, but objects of the Type base are not objects of the type derived. This is important. The function parameter is of the base type and applies to derived. The parameter is of the derived type and does not apply to base. The following is the verification code. A base parameter function should be passed into the derived and executed successfully. On the contrary, a function with the derived parameter is used.Copy codeThe Code is 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 running result is:
Base: baseName is test, baseData is 10
Base: baseName is btest, baseData is 5
Modify the code below to change the function parameter to derived.
Copy codeThe Code is 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); the compiler reports an errorCopy codeThe Code is 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 'base'
Derived_class.cpp: 70: error: in passing argument 2 of 'void show2 (std: string &, const derived &) 'Second: Verify the inheritance of various forms. The table is given first.
Inheritance Method \ member type |
Public |
Protected |
Private |
Public |
Public |
Protected |
Unable to inherit |
Protected |
Protected |
Protected |
Unable to inherit |
Private |
Private |
Private |
Unable to inherit |
Here, we will explain that only the members of the base class can be inherited by the public, protected, and private methods. After the base class is public, protectedc, the private member type in the inheritance class is the content in the table.
Copy codeThe Code is as follows: 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 is reported, indicating that testPrivate () is not a derived private function but a base private function.
Derived11.cpp: 16: error: 'std: string base: testPrivate () 'is private
Derived11.cpp: 36: error: within this context verifies that private Members cannot be inherited (public, private, protected). Note: private and protected are omitted without proof.
As long as testProtected can be inherited by the third-level inheritance class, but cannot be directly called by the third-level class, it means that the inheritance type is protected after the public inheritance, if the base class is a Public member, it can be inherited and directly called.Copy codeThe Code is as follows: # 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;
}
Here the server reports an error
Derived12.cpp: 13: error: 'std: string base: testProtected () 'is protected
Derived12.cpp: 62: error: within this context verifies that one is public and the other is protected. protected cannot be called directly, but after being inherited, it can be called by public members.
The steps below have proved to be omitted. If you are interested in this part of verification, you can refer to the following code.
Copy codeThe Code is as follows: # 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 () // The 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 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; // The base class is a private function.
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; // Changes to the 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;
}