1.
Class {
Public:
A () {func (0 );};
Virtual void func (INT data) {printf ("A1: % d \ n", data );}
Virtual void func (INT data) const {printf ("A2: % d \ n", data );}
Void func (char * Str) {printf ("A3 :( % s) \ n", STR );}
};
Class B: Public {
Public:
Void func () {printf ("B1: % s \ n ","");}
Void func (INT data) {printf ("B2: % d \ n", data );}
Void func (char * Str) {printf ("B3 :( % s) \ n", STR );}
};
Int main ()
{
A * pA;
B;
Const A * PCA;
Pa = & B;
Pa-> func (1 );
Pa-> func ("test ");
A (). func (1 );
PCA = & B;
PCA-> func (2 );
Return 0;
}
Running result:
A1: 0
B2: 1
A3 :( test)
A1: 0
A1: 1
A2: 2
1) base class Pointer Points to the object of the derived class: the pointer can only call the function defined by the base class, but if the function is a virtual function, it calls its own member function of the derived class. (B2: 1)
2) if a pointer from a derived class is directed to a base class object, you must perform obvious transformation operations in advance, but this approach is very dangerous.
2.
Template
Void func (const Int & T)
{
Cout <t + 100 <Endl;
}
Template
Void func (const T & T)
{
Cout <t;
}
Int main ()
{
Func (10.3 );
Func (1000 );
Return 0;
}
Program running result:
10.3
1000
If the above function is changed
Void func (const Int & T)
{
Cout <t + 100 <Endl;
}
Template
Void func (const T & T)
{
Cout <t <Endl;
}
Int main ()
{
Func (10.3 );
Func (1000 );
Return 0;
}
The running result of the program is:
10.3
1100
If you use a function in a non-template form, you cannot add the template keyword before it.
3. Correction:
Class Klass
{
Public:
Klass (){}
PRIVATE:
~ Klass (){}
Void func (int n ){
Cout <"Klass !! "<Endl;
}
Public:
Void test ()
{
Func (100 );
}
};
Int main ()
{
Klass K;
K. Test ();
Return 0;
}
After running, the program displays error c2248: 'klass ::~ Klass ': cannot access private member declared in class 'klass'
The property of the Destructor must be public.
However, if you change Klass K to Klass * PK; PK = new Klass; PK-> test (); the program passes, but Klass cannot be released.