I finally had a holiday. What can I do? I thought of several blogs this morning and mentioned that I used the parent class pointer to access sub-class functions .. This morning, let's verify it.
[Cpp]
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Void name ()
{
Cout <"Base Class name" <endl;
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
Base * BaseClassPointer;
BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
// This is equivalent to converting the subclass to the parent class type, and then calling the name function of the parent class
Return 0;
}
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Void name ()
{
Cout <"Base Class name" <endl;
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
Base * BaseClassPointer;
BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
// This is equivalent to converting the subclass to the parent class type, and then calling the name function of the parent class
Return 0;
}
Running result
[Cpp]
Initial Base Class
Initial Derived Class // initialize the Deerived Class
Derived1 Class name // call the name () of the Derived Class ()
Base Class name // when forced conversion occurs, the Base Class name () is called ()
Initial Base Class
Initial Derived Class // initialize the Deerived Class
Derived1 Class name // call the name () of the Derived Class ()
Base Class name // when forced conversion occurs, the Base Class name () is called ()
Without using a virtual function, use the pointer of the base class to access the function of the subclass. This is equivalent to forcibly converting objects of the Derived1 type to the Base type. Therefore, the function finally calls the name () function in the Base class.
Using a virtual function, you can use the pointer of the base class to access the function of the subclass. The Code is as follows:
[Cpp]
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Virtual void name () // implemented using virtual functions
{
Cout <"Base Class name" <endl;
}
Virtual ~ Base ()
{
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
~ Derived1 ()
{
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
Base * BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
Return 0;
}
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Virtual void name () // implemented using virtual functions
{
Cout <"Base Class name" <endl;
}
Virtual ~ Base ()
{
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
~ Derived1 ()
{
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
Base * BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
Return 0;
}
Running result:
[Cpp]
Initial Base Class
Initial Derived Class // initialize two parts of the Derived Class
Derived1 Class name
Derived1 Class name // The name () of the Derived Class is called ()
Process returned 0 (0x0) execution time: 0.018 s
Press any key to continue.
Initial Base Class
Initial Derived Class // initialize two parts of the Derived Class
Derived1 Class name
Derived1 Class name // The name () of the Derived Class is called ()
Process returned 0 (0x0) execution time: 0.018 s
Press any key to continue.
In class inheritance, if a base class Pointer Points to a derived class, when the base class pointer is deleted, if it is not necessarily a virtual function, the derived part of the derived class cannot be destructed.
Therefore, if you design a class that may be a base class, it is best to set one of its destructor as a virtual function, otherwise it may cause memory leakage.